-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
32 lines (30 loc) · 1.05 KB
/
Copy pathSelectionSort.java
File metadata and controls
32 lines (30 loc) · 1.05 KB
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
package sorting;
public class SelectionSort {
public static void main(String[] args) {
int[] a = {8,5,4,7,60, 31};
System.out.println("*************: Before Sorting :**************");
for(int i : a){
System.out.print(i +", ");
}
int n = a.length;
for(int i=0;i< n;i++){
//let assume current index(i) has the minimum element
int min_index = i;
//find minimum element from remaining unsorted array and update min_index.
for(int j = i+1;j<n;j++){
if(a[j] < a[min_index]){
min_index = j;
}
}
//once we found minimum element index then swap min_index element with current element(ith position element)
int temp = a[i];
a[i] = a[min_index];
a[min_index] = temp;
}
System.out.println();
System.out.println("*************: After Sorting :**************");
for(int i : a){
System.out.print(i+", ");
}
}
}