-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingClass.java
More file actions
121 lines (105 loc) · 3.7 KB
/
Copy pathSortingClass.java
File metadata and controls
121 lines (105 loc) · 3.7 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
public class SortingClass {
///// HEAP SORT /////
public static void heapSort(int[] A){
int n = A.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(A, n, i);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
int temp = A[0];
A[0] = A[i];
A[i] = temp;
// call max heapify on the reduced heap
heapify(A, i, 0);
}
}
private static void heapify(int A[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && A[l] > A[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && A[r] > A[largest])
largest = r;
// If largest is not root
if (largest != i)
{
int swap = A[i];
A[i] = A[largest];
A[largest] = swap;
// Recursively heapify the affected sub-tree
heapify(A, n, largest);
}
}
///// DUAL PIVOT QUICK SORT /////
static void dualPivotQuickSort(int[] A, int left, int right) {
if (right > left) {
// Choose outermost elements as pivots
if (A[left] > A[right]) swap(A, left, right);
int p = A[left], q = A[right];
// Partition A according to invariant below
int l = left + 1, g = right - 1, k = l;
while (k <= g) {
if (A[k] < p) {
swap(A, k, l);
++l;
} else if (A[k] >= q) {
while (A[g] > q && k < g) --g;
swap(A, k, g);
--g;
if (A[k] < p) {
swap(A, k, l);
++l;
}
}
++k;
}
--l; ++g;
// Swap pivots to final place
swap(A, left, l); swap(A, right, g);
// Recursively sort partitions
dualPivotQuickSort(A, left, l - 1);
dualPivotQuickSort(A, l + 1, g - 1);
dualPivotQuickSort(A, g + 1, right);
}
}
static void swap(int[] A, int i, int j) {
final int tmp = A[i]; A[i] = A[j]; A[j] = tmp;
}
///// SHELL SORT /////
/* function to sort arr using shellSort */
static int shellSort(int A[])
{
int n = A.length;
// Start with a big gap, then reduce the gap
for (int gap = n/2; gap > 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already
// in gapped order keep adding one more element
// until the entire array is gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap
// sorted save a[i] in temp and make a hole at
// position i
int temp = A[i];
// shift earlier gap-sorted elements up until
// the correct location for a[i] is found
int j;
for (j = i; j >= gap && A[j - gap] > temp; j -= gap)
A[j] = A[j - gap];
// put temp (the original a[i]) in its correct
// location
A[j] = temp;
}
}
return 0;
}
}