-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.java
More file actions
106 lines (90 loc) · 3.29 KB
/
Sorting.java
File metadata and controls
106 lines (90 loc) · 3.29 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
public class Sorting {
/**
* Implement the mergesort function, which should sort the array of
* integers in place
*
* You will probably want to use helper functions here, as described in the lecture recordings
* (ex. merge(), a helper mergesort function)
* @param arr
*/
public static void mergeSort(CompareInt[] arr) {
//TODO
int i = 0;
int j = arr.length-1;
mergeSort1(arr, i, j);
}
public static void mergeSort1(CompareInt[] arr, int i, int j){
if(i<j){
int mid = i+ (j-i)/2;
mergeSort1(arr, i, mid);
mergeSort1(arr, mid+1, j);
CompareInt[] newArr = new CompareInt[(mid - i+1)];
int y=i;
for(int x =0; x<mid-i+1;){
newArr[x++] = arr[y++];
}
int leftPt, rightPt, newArrPter;
rightPt = mid+1;
newArrPter = 0;
leftPt = i;
while (newArrPter <= mid-i && rightPt <=j){
if(newArr[newArrPter].compareTo(arr[rightPt])<=0){
arr[leftPt++] = newArr[newArrPter++];
}
else{
arr[leftPt++] = arr[rightPt++];
}
}
while(newArrPter <= mid -i) {
arr[leftPt++] = newArr[newArrPter++];
}
}
}
/**
* Implement the quickSelect
*
* Again, you will probably want to use helper functions here
* (ex. partition(), a helper quickselect function)
*/
public static CompareInt quickSelect(int k, CompareInt[] arr) {
//TODO
return recursive(arr, 0, arr.length-1, k);
}
public static CompareInt recursive(CompareInt[] array, int left, int right, int n) {
if (left == right) { // If the list contains only one element,
return array[left]; // return that element
}
// select a pivotIndex between left and right
int pivotIndex = randomPivot(left, right);
pivotIndex = partition(array, left, right, pivotIndex);
// The pivot is in its final sorted position
if (n == pivotIndex) {
return array[n];
} else if (n < pivotIndex) {
return recursive(array, left, pivotIndex - 1, n);
} else {
return recursive(array, pivotIndex + 1, right, n);
}
}
public static int partition(CompareInt[] array, int left, int right, int pivotIndex) {
CompareInt pivotValue = array[pivotIndex];
swap(array, pivotIndex, right); // move pivot to end
int storeIndex = left;
for(int i = left; i < right; i++) {
if(array[i].compareTo(pivotValue)<0) {
swap(array, storeIndex, i);
storeIndex++;
}
}
swap(array, right, storeIndex); // Move pivot to its final place
return storeIndex;
}
public static void swap(CompareInt[] array, int a, int b) {
CompareInt tmp = array[a];
array[a] = array[b];
array[b] = tmp;
}
public static int randomPivot(int left, int right) {
return left + (int) Math.floor(Math.random() * (right - left + 1));
}
}