-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectSort.java
More file actions
36 lines (28 loc) · 819 Bytes
/
Copy pathSelectSort.java
File metadata and controls
36 lines (28 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Sort;
import java.util.Arrays;
public class SelectSort {
public static int[] selectSort(int[] nums){
if(nums.length == 0 || nums.length == 1){
return nums;
}
for(int i=0; i<nums.length; i++){
int midIndex = i;
int minValue = nums[i];
for(int j=i; j< nums.length; j++){
if(nums[j] < minValue){
midIndex = j;
minValue = nums[j];
}
}
int temp = nums[i];
nums[i] = minValue;
nums[midIndex] = temp;
}
return nums;
}
public static void main(String[] args){
int[] nums = {4,3,2,5,7,6};
int[] res = selectSort(nums);
System.out.println(Arrays.toString(res));
}
}