-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSortTest.java
55 lines (50 loc) · 1.52 KB
/
SelectionSortTest.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
53
54
55
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package name;
import java.util.Arrays;
/**
*
* @author HP
*/
public class SelectionSortTest {
Integer[] a;
public SelectionSortTest(Integer[] a){
this.a = a;
}
public void selectionSort(Integer[] a){
int n = a.length;
for(int i=0; i < n; i++){
int min_idx = i; //i hithgnnw ey thm shorter kiyl
for(int j = i; j < n; j++){
if(a[j] < a[min_idx]){
min_idx = j;
}
System.out.println(
"i = "
+(i)
+"; j = "
+(j)
+"; cur_min = "
+a[min_idx]
+"; "
+ Arrays.deepToString(a)
);
}
swap(i, min_idx);
}
}
public void swap(int i,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
Integer[] a = {76,6,187,92,21,23,5,19,8,8143};
SelectionSortTest sorter = new SelectionSortTest(a);
System.out.println("Before Sorting: "+ Arrays.deepToString(a));
sorter.selectionSort(a);
System.out.println("After Sorting: "+ Arrays.deepToString(a));
}
}