-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathQuickSortDemo.java
61 lines (47 loc) · 1.32 KB
/
QuickSortDemo.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
package ds_005_sorting;
public class QuickSortDemo {
public static void main(String[] args) {
// int[] array = { 2, 1, 3, 4, 5, 7, 6 };
// int[] array = { 5, 3, 4, 4, 8, 6, 7 };
// int[] array = { 5, 3, 4, 4, 8, 6, 7, 1 };
// int[] array = { 5, 4, 3, 2, 1 };
int[] array = { 1 ,2, -5, 3, 2 } ;
// int[] array = { 1, 2, 3, 4, 5 };
// int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// int[] array = { 5, 1, 2, 3, 4 };
// int[] array = { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// int[] array = { 5, 4, 3, 2, 1 };
// int[] array = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
printArray(array);
selectionSort(array);
printArray(array);
}
/*
* Worst Case, Best Case, Random Case are all equal = O(N^2)
*/
private static void selectionSort(int[] array) {
int times = 0;
for(int i = 0; i < array.length; i++) {
int smallest = i;
for(int j = i+1; j < array.length; j++) {
if(array[j] < array[smallest]) {
smallest = j;
}
times++;
}
if(smallest != i) {
int temp = array[i];
array[i] = array[smallest];
array[smallest] = temp;
}
}
System.out.println("Times : " + times);
}
private static void printArray(int[] array) {
System.out.print("Array: ");
for(int i = 0; i < array.length; i++) {
System.out.printf("%2d ", array[i]);
}
System.out.print("\n");
}
}