- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathintro_sort.java
More file actions
Latest commit
158 lines (131 loc) · 3.91 KB
/
Copy pathintro_sort.java
File metadata and controls
158 lines (131 loc) · 3.91 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
// Java implementation of intro_sort algorithm
importjava.util.Scanner;
publicclassintro_sort {
privateinta[];
privateintn;
intro_sort(intn) {
a = newint[n];
this.n = 0;
}
privatevoiddataAppend(inttemp) {
a[n] = temp;
n++;
}
privatevoidswap(inti, intj) {
inttemp = a[i];
a[i] = a[j];
a[j] = temp;
}
privatevoidmaxHeap(inti, intheapN, intbegin) {
inttemp = a[begin + i - 1];
intchild;
while (i <= heapN / 2) {
child = 2 * i;
if (child < heapN
&& a[begin + child - 1] < a[begin + child])
child++;
if (temp >= a[begin + child - 1])
break;
a[begin + i - 1] = a[begin + child - 1];
i = child;
}
a[begin + i - 1] = temp;
}
privatevoidheapify(intbegin, intend, intheapN) {
for (inti = (heapN) / 2; i >= 1; i--)
maxHeap(i, heapN, begin);
}
// Heap Sort
privatevoidheapSort(intbegin, intend) {
intheapN = end - begin;
this.heapify(begin, end, heapN);
for (inti = heapN; i >= 1; i--) {
// Move current root to end
swap(begin, begin + i);
maxHeap(1, i, begin);
}
}
// Insertion Sort
privatevoidinsertionSort(intleft, intright) {
for (inti = left; i <= right; i++) {
intkey = a[i];
intj = i;
while (j > left && a[j - 1] > key) {
a[j] = a[j - 1];
j--;
}
a[j] = key;
}
}
privateintfindPivot(inta1, intb1, intc1) {
intmax = Math.max(Math.max(a[a1], a[b1]), a[c1]);
intmin = Math.min(Math.min(a[a1], a[b1]), a[c1]);
intmedian = max ^ min ^ a[a1] ^ a[b1] ^ a[c1];
if (median == a[a1])
returna1;
if (median == a[b1])
returnb1;
returnc1;
}
privateintpartition(intlow, inthigh) {
// pivot
intpivot = a[high];
inti = (low - 1);
for (intj = low; j <= high - 1; j++) {
if (a[j] <= pivot) {
i++;
swap(i, j);
}
}
swap(i + 1, high);
return (i + 1);
}
privatevoidsortDataUtil(intbegin, intend, intdepthLimit) {
if (end - begin > 16) {
if (depthLimit == 0) {
this.heapSort(begin, end);
return;
}
depthLimit = depthLimit - 1;
intpivot = findPivot(begin,
begin + ((end - begin) / 2) + 1,
end);
swap(pivot, end);
intp = partition(begin, end);
sortDataUtil(begin, p - 1, depthLimit);
sortDataUtil(p + 1, end, depthLimit);
}
else {
// if the data set is small,
// call insertion sort
insertionSort(begin, end);
}
}
privatevoidsortData() {
// Initialise the depthLimit
intdepthLimit = (int) (2 * Math.floor(Math.log(n) /
Math.log(2)));
this.sortDataUtil(0, n - 1, depthLimit);
}
privatevoidprintData() {
for (inti = 0; i < n; i++)
System.out.print(a[i] + " ");
}
publicstaticvoidmain(Stringargs[]) {
Scanners = newScanner(System.in);
System.out.print("Enter the number of elements: ");
intn = s.nextInt();
int[] arr = newint[n];
System.out.print("Enter the elements: ");
for (inti = 0; i < n; i++)
arr[i] = s.nextInt();
// function call
intro_sortis = newintro_sort(n);
for (inti = 0; i < n; i++) {
is.dataAppend(arr[i]);
}
is.sortData();
System.out.print("Sorted array: ");
is.printData();
}
}