-
Notifications
You must be signed in to change notification settings - Fork 28
/
SelectionSortDemo.java
95 lines (73 loc) · 2.08 KB
/
SelectionSortDemo.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ds_005_sorting;
public class SelectionSortDemo {
public static void main(String[] args) {
// int[] array = { 4, 9, 3, 7, 8, 2, 1} ;
int[] array = { 3, 8, 2, 1, 5, 4, 6, 7 };
// 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);
System.out.println("************");
quickSort(array);
System.out.println("************");
printArray(array);
}
/*
* Worst Case = O(N^2)
* Average Case = O(N*LogN)
* Best Case = O(N*LogN)
*
* Space Required = O(N)
* Stack Space must be considered as well as Array Space
*/
private static void quickSort(int[] array) {
quickSort(array, 0, array.length-1);
}
private static void quickSort(int[] array, int start, int end) {
if(start >= end) {
return;
}
int[] tempArray = new int[(end-start)+1];
int pivotIndex = start;
// int pivotIndex = end; // *1
int left = 0;
int right = end-start;
for(int i = start+1; i <= end; i++) {
// for(int i = start; i <= (end-1); i++) { // *2
if( array[i] < array[pivotIndex] ) {
tempArray[left++] = array[i];
} else {
tempArray[right--] = array[i];
}
}
// left == right
tempArray[left] = array[pivotIndex];
pivotIndex = start + left;
for(int i = start, j = 0; i <= end; i++, j++) {
array[i] = tempArray[j];
}
if( (pivotIndex-1) != end) {
quickSort(array, start, pivotIndex-1);
}
if( (pivotIndex+1) != start) {
quickSort(array, pivotIndex+1, end);
}
}
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");
}
private static void printArray(int[] array, int start, int end) {
System.out.print("Array: ");
for(int i = start; i <= end; i++) {
System.out.printf("%2d ", array[i]);
}
System.out.print("\n");
}
}