-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorts.java
146 lines (145 loc) · 4.57 KB
/
Sorts.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
import java.util.ArrayList;
import java.util.List;
/**
* Created by Paul on 5/6/2015.
*/
public class Sorts {
// helper methods and fields
private static Comparable[] aux;
private static void merge(Comparable[] a, int lo, int mid, int hi){
int i = lo, j = mid + 1;
for(int k = lo; k <= hi; ++k){
aux[k] = a[k];
}
for(int k = lo; k <= hi; ++k){
if(i > mid) a[k] = aux[j++];
else if(j > hi) a[k] = aux[i++];
else if(less(aux[j], aux[i])) a[k] = aux[j++];
else a[k] = aux[i++];
}
}
private static void mergeSortHelper(Comparable[] a, int lo, int hi){
if(hi <= lo) return;
int mid = lo + (hi - lo) / 2;
mergeSortHelper(a, lo, mid);
mergeSortHelper(a, mid + 1, hi);
merge(a, lo, mid, hi);
}
private static void exch(Comparable[] a, int i, int j){
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static void exch(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static boolean less(Comparable a, Comparable b){
return a.compareTo(b) < 0;
}
public static boolean isSorted(Comparable[] a){
for(int i = 1; i < a.length; ++i){
if(less(a[i], a[i - 1])) return false;
}
return true;
}
public static void show(Comparable[] a){
for(int i = 0; i < a.length; ++i){
System.out.print(a[i] + " ");
}
System.out.println();
}
// helper methods end
public static void insertionSort(Comparable[] a){
for(int i = 1; i < a.length; ++i){
for(int j = i; j > 0 && less(a[j], a[j - 1]); --j){
exch(a, j, j - 1);
}
}
}
public static void selectionSort(Comparable[] a){
for(int i = 0; i < a.length - 1; ++i){
int min = i;
for(int j = i + 1; j < a.length; ++j){
if(less(a[j], a[min])) min = j;
}
exch(a, i, min);
}
}
public static void bubbleSort(Comparable[] a){
for(int i = a.length; i > 0; --i){
for(int j = 1; j < i; ++j){
if(less(a[j], a[j - 1])) exch(a, j, j - 1);
}
}
}
public static void mergeSort(Comparable[] a){
aux = new Comparable[a.length];
mergeSortHelper(a, 0, a.length - 1);
}
public static void quickSort(Comparable[] a){
}
public static void shellSort(Comparable[] a){
int h = 1;
while(h < a.length / 3) h = 3 * h + 1;
while(h >= 1){
for(int i = h; i < a.length; ++i){
for(int j = i; j >= h && less(a[j], a[j - h]); j -= h)
exch(a, j, j - 1);
}
h /= 3;
}
}
public static void shellSortMemoized(Comparable[] a){
List<Integer> list = new ArrayList<>();
for(int h = 1; h < a.length / 3; h = h * 3 + 1){
list.add(h);
}
for(int i = list.size() - 1; i >= 0; --i){
int h = list.get(i);
for(int j = h; j < a.length; ++j){
for(int k = j; k >= h && less(a[k], a[k - h]); k -= h){
exch(a, k, k - h);
}
}
}
}
public static void insertionSortPrimitive(int[] a){
for(int i = 1; i < a.length; ++i){
for(int j = i; j > 0 && a[j] < a[j - 1]; --j){
exch(a, j, j - 1);
}
}
}
public static void shellSortDiffInc(Integer[] a){
List<Integer> list = new ArrayList<>();
int k9 = 0;
int k4 = 2;
int elem = 0;
for(int k = 0; elem < a.length / 3; ++k){
if(k % 2 == 0){
elem = (int)(9 * Math.pow(4, k9) - 9 * Math.pow(2, k9) + 1);
list.add(elem);
k9++;
}else{
elem = (int)(Math.pow(4, k4) - 3 * Math.pow(2, k4) + 1);
list.add(elem);
k4++;
}
}
//System.out.println("Increment sequence: ");
for(int i = 0; i < list.size(); ++i){
System.out.print(list.get(i) + " ");
}
System.out.println();
for(int i = list.size() - 1; i >= 0; --i){
int h = list.get(i);
for(int k = h; k < a.length; ++k) {
for (int j = k; j >= h && less(a[j], a[j - h]); j -= h) {
exch(a, j, j - h);
}
}
}
}
}