-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingAlgo.java
More file actions
232 lines (179 loc) · 6.01 KB
/
Copy pathSortingAlgo.java
File metadata and controls
232 lines (179 loc) · 6.01 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import java.util.Scanner;
public class SortingAlgo {
/*This will iterate over each element in the list.
* it will look at each element and check if it is greater
* than the next elemenet in line. If it is it will swap the
* values. This makes sure that after each iteration the
* biggest value is the last item in the array.
* @param array The array that is going to be sorted.*/
public void bubbleSort(int[] array){
int tempVal;
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
tempVal = array[j];
array[j] = array[j + 1];
array[j + 1] = tempVal;
}
}
}
}
/*This will divide the array by a midpoint until
* there is only one element in each side. It will
* then sort each of the levels
* @param array The array that's going to be sorted
* @param i the starting value of what will be sorted.
* @param k the ending value of what will be sorted.*/
public void mergeSort(int [] array, int i, int k){
//J is the midpoint
int j;
if (i < k){
j = (i + k) / 2;
//Recursively sort left and right partitions
mergeSort(array,i,j);
mergeSort(array,j+1,k);
//Merge left and right
merge(array,i,j,k);
}
}
/*Helper function for the merge sort. This is the
* method that actually does the sorting.
* @param i the start value of what will be sorted
* @param k the end value of what will be sorted
* @param j the midpoint*/
public void merge(int[] array,int i, int j, int k){
int mergedSize = k - i + 1;
int mergedNumbers [] = new int[mergedSize];
int mergePos;
int leftPos;
int rightPos;
mergePos = 0;
leftPos = i;
rightPos = j + 1;
while (leftPos <= j && rightPos <= k){
if (array[leftPos] < array[rightPos]){
mergedNumbers[mergePos] = array[leftPos];
++leftPos;
}else{
mergedNumbers[mergePos] = array[rightPos];
++rightPos;
}
++mergePos;
}
while (leftPos <= j){
mergedNumbers[mergePos] = array[leftPos];
++leftPos;
++mergePos;
}
while (rightPos <= k){
mergedNumbers[mergePos] = array[rightPos];
++rightPos;
++mergePos;
}
//Copy merge numbers back to numbers
for (mergePos = 0; mergePos < mergedSize;++mergePos){
array[i+ mergePos] = mergedNumbers[mergePos];
}
}
/*This will iterate over every item
* in the list as well. It will look if
* the element behind the current element is
* less than. If it is it will make a swap.
* It will keep moving backwards until the
* element is in it's sorted position or it
* is at the first element in the array.
* @param array The array that will be sorted*/
public void insertionSort(int[] array){
int i;
int j;
int temp;
for (i = 1; i < array.length; ++i){
j = i;
while (j > 0 && array[j] < array[j-1]){
temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
--j;
}
}
}
/*This will sort the array by paritioning both
* sides.
* @param array The array that will be sorted
* @param i the start value of what will be sorted
* @param k the end value of what will be sorted.*/
public void quickSort(int[] array,int i, int k){
int j;
if(i >= k){
return;
}
j = partition(array,i,k);
quickSort(array,i,j);
quickSort(array, j + 1,k);
}
/*This will partition the array. It will
* look at a pivot and move everything less than
*the pivot to the left, and everything right of
* the pivot to the right. It will then keep breaking it
* down until there are only two elements.
* @param array The array that is being sorted
* @param i the start value of what will be sorted.
* @param k the end value of what will be sorted*/
public int partition(int[] array, int i, int k){
int l;
int h;
int midpoint;
int pivot;
int temp;
boolean done;
/* Pick middle element as pivot */
midpoint = i + (k - i) / 2;
pivot = array[midpoint];
done = false;
l = i;
h = k;
while (!done) {
/* Increment l while numbers[l] < pivot */
while (array[l] < pivot) {
++l;
}
/* Decrement h while pivot < numbers[h] */
while (pivot < array[h]) {
--h;
}
/* If there are zero or one items remaining,
all numbers are partitioned. Return h */
if (l >= h) {
done = true;
}
else {
/* Swap numbers[l] and numbers[h],
update l and h */
temp = array[l];
array[l] = array[h];
array[h] = temp;
++l;
--h;
}
}
return h;
}
/*This iterates of every item in the array
* and looks for the smallest value. It will
* then swap that value with the position of i.
* @param array The array that will be sorted*/
public void selectionSort(int[] array){
for (int i = 0; i < array.length-1; i++){
int min = i;
for (int j = min + 1; j < array.length;j++){
if(array[j] < array[min]){
min = j;
}
}
int temp;
temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
}