-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.java
163 lines (123 loc) · 3.44 KB
/
sorting.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Snippets of this code are adapted from
// Mongan, Giguere & Kindler's Programming Interviews Exposed 3rd Ed
/**************
SELECTION SORT
Best, Average and Worst: O(n^2)
**************/
// Sort an array using a recursive selection sort.
public void selectionSortRecursive (int[] data) {
selectionSortRecursive(data, 0);
}
private void selectionSortRecursive (int[] data. int start) {
if (start < data.length - 1) {
swap(data, start, findMinimumIndex(data, start));
selectionSortRecursive(data, start+1);
}
}
private int findMinimumIndex (int[] data, int start) {
int minPos = start;
for (int i = start +1; i < data.length; i++) {
if (data[i] < data[minPos])
minPos = i;
}
return minPos;
}
private void swap(int[] data, int index1, int index2) {
if (index1 != index2) {
int tmp = data[index1];
data[index1] = data[index2];
data[index2] = tmp;
}
}
/**************
INSERTION SORT
Best: O(n)
Average and Worst: O(n^2)
**************/
// Sort an array using a simple insertion sort
public void insertionSort(int[] data) {
for (int which = 1; which < data.length; ++which) {
int val = data[which];
for (int i = 0; i < which; ++i) {
if (data[i] > val) {
System.arraycopy(data, i, data, i+1, which-i);
data[i] = val;
break;
}
}
}
}
/**************
QUICKSORT
Best and Average: O(nlog(n))
Worst: O(n^2)
**************/
// Sort an array using a simple but inefficient quicksort
public int[] quicksortSimple(int[] data) {
if (data.length < 2)
return data;
int pivotIndex = data.length / 2;
int pivotValue = data[pivotIndex];
int leftCount = 0;
// Count how many are less than the pivot
for (int i = 0; i < data.length; ++i)
if (data[i] < pivotValue) ++leftCount;
// Allocate the arrays and create the subsets
int[] left = new int[leftCount];
int[] right = new int[data.length - leftCount - 1];
int l = 0;
int r = 0;
for (int i = 0; i < data.length; ++i) {
if (i== pivotIndex) continue;
int val = data[i];
if(val < pivotValue)
left[l++] = val;
else
right[r++] = val;
}
// Sort the subsets
left = quicksortSimple(left);
right = quicksortSimple(right);
// Combine the sorted arrays and the pivot back into the original array
System.arraycopy(left, 0, data, 0, left.length);
data[left.length] = pivotValue;
System.arraycopy(right, 0, data, left.length+1, right.length);
return data;
}
/**************
MERGESORT
Best, Average and Worst: O(nlog(n))
*Requires O(n) additional memory
**************/
// Sort an array using a simple but inefficient merge sort.
public int[] mergesortSimple(int[] data) {
if (data.length < 2)
return data;
// Split the arrat into two subarrays of approximately equal size
int mid = data.legth / 2;
int[] left = new int[mid];
int[] right = new int[data.length - mid];
System.arraycopy(data, 0, left, 0, left.length);
System.arraycopy(data, mid, right, 0, right.length);
// Sort each subarray, then merge the result.
mergesortSimple(left);
mergesortSimple(right);
return merge(data, left, right);
}
private int[] merge(int[] dest, int[] left, int[] right) {
int dind = 0;
int lind = 0;
int rind = 0;
// Merge arrats while there are elements in both
while (lind < left.length && rind < right.length) {
if (left[lind] <= right[rind])
dest[dind++] = left[lind++];
else
dest[dind++] = right[rind++];
}
while (lind < left.length)
dest[dind++] = left[lind++];
while (rind < right.length)
dest[dind++] = right[rind++];
return dest;
}