-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.java
More file actions
195 lines (157 loc) · 4.85 KB
/
sorting.java
File metadata and controls
195 lines (157 loc) · 4.85 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
public class sorting {
/*
************************
**** INSERTION SORT ****
************************
*/
public void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
/*
************************
**** SELECTION SORT ****
************************
* Figure out the minimum element from all the elements, then position it at the
* start.
*/
public void selectionSort(int[] arr, int n) {
int current = 0;
int min = Integer.MAX_VALUE;
int minIndex = 0;
while (current < n - 1) {
min = arr[current];
minIndex = current;
for (int i = current + 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
minIndex = i;
}
}
if (minIndex != current) {
arr[minIndex] = arr[current];
arr[current] = min;
}
current++;
}
}
/*
************************
****** MERGE SORT ******
************************
*
* Merge Sort is a Divide and Conquer algorithm.
* Divide the n element sequence into two sub-sequences of n/2.
* Conquer: Sort the two sub-sequences recursively, by breaking them again until
* we have single elements.
* Combine: Merge the subsequences
*
* Merge Sort is not an inplace sorting algorithm.
* Time Complexity: O(n logn)
* Merge Sort is input independent.
*/
public void mergeSort(int[] arr) {
mergeSort(arr, 0, arr.length - 1);
}
private void mergeSort(int[] arr, int start, int end) {
int mid = start + (end - start) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
private void merge(int[] arr, int start, int mid, int end) {
// The sizes of the temporary arrays
int n1 = mid - start + 1;
int n2 = end - mid;
// create two temporaray new arrays
int[] arr1 = new int[n1];
int[] arr2 = new int[n2];
for (int i = 0; i < n1; i++) {
arr1[i] = arr[start + i];
}
for (int i = 0; i < n2; i++) {
arr2[i] = arr[start + mid + i];
}
int i = 0, j = 0;
int k = start;
while (i < n1 && j < n2) {
if (arr1[i] > arr2[j]) {
arr[k] = arr2[j];
j++;
} else {
arr[k] = arr1[i];
i++;
}
k++;
}
while (i < n1) {
arr[k] = arr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = arr[j];
j++;
k++;
}
}
/*
************************
****** QUICK SORT ******
************************
* This is a Divide and Conquer Algorithm
* Partition the array into two subarrays based on a pivot element X. Such that
* Left-subArray <= X and Right-subArray >= X
* The elements in the subarrays should not necessarily be sorted.
*
* Here we are taking the first element as pivot.
*
* The best case scenario will be that the pivot is partitioning the array into
* two equal subarrays. The average case run time of quickSort is much closer to
* the best case than the worst case.
*
* If the partition algorithm can ensure some fractional split of the input
* which may result in breaking the input into smaller chunks, then we can get a
* case which is not as bad as the worst case.
* Hence we have to add a picot which is not maximum or minimum. Adding
* randomization to the algorithm is better to ensure good performance over all
* inputs.
*/
public void quickSort(int[] arr) {
quickSort(arr, 0, arr.length - 1);
}
private void quickSort(int[] arr, int start, int end) {
if (start < end) {
int pivot = partition(arr, start, end);
quickSort(arr, start, pivot - 1);
quickSort(arr, pivot + 1, end);
}
}
private int partition(int[] arr, int start, int end) {
int i = start;
int pivot = start;
for (int j = i + 1; j <= end; j++) {
if (arr[pivot] > arr[j]) {
i++;
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
int temp = arr[i];
arr[i] = arr[pivot];
arr[pivot] = temp;
return i;
}
// Heap Sort
// Bin Sort
// Bucket Sort
// Radix Sort
}