-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathselectionSort.java
63 lines (51 loc) · 1007 Bytes
/
selectionSort.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
56
57
58
59
60
61
62
63
class ArraySel {
private long[] a;
private int nElems;
public ArraySel(int max) {
a = new long[max];
nElems = 0;
}
public void insert(long value) {
a[nElems] = value;
nElems++;
}
public void display() {
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " "); // display it
System.out.println("");
}
public void selectionSort() {
int out, in, min;
for(out=0; out<nElems-1; out++) {
min = out;
for(in=out+1; in<nElems; in++)
if(a[in] < a[min] )
min = in;
swap(out, min);
}
}
private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
}
class selectionSort {
public static void main(String[] args) {
int maxSize = 100;
ArraySel arr = new ArraySel(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
arr.selectionSort();
arr.display();
}
}