-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection-sort.java
52 lines (32 loc) · 1.14 KB
/
selection-sort.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Main{
public static void selectionSort(int arr[]){
// One by one move boundary of unsorted subarray
for(int i=0; i<arr.length-1; i++){
// Find the minimum element in unsorted array
int minPos=i;
for(int j=i+1; j<arr.length; j++){
if(arr[minPos]>arr[j]){
minPos=j;
}
}
// Swap the found minimum element with the first
// element
int temp=arr[minPos];
arr[minPos]=arr[i];
arr[i]=temp;
}
}
// Prints the array
public static void printArray(int arr[]){
for(int i=0; i<arr.length; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
//derive code to call the above methods
public static void main(String[] args) {
int arr[]={5, 4, 1, 3, 2};
selectionSort(arr);
printArray(arr);
}
}