-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathintro_sort.java
158 lines (131 loc) · 3.91 KB
/
intro_sort.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
147
148
149
150
151
152
153
154
155
156
157
158
// Java implementation of intro_sort algorithm
import java.util.Scanner;
public class intro_sort {
private int a[];
private int n;
intro_sort(int n) {
a = new int[n];
this.n = 0;
}
private void dataAppend(int temp) {
a[n] = temp;
n++;
}
private void swap(int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private void maxHeap(int i, int heapN, int begin) {
int temp = a[begin + i - 1];
int child;
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;
}
private void heapify(int begin, int end, int heapN) {
for (int i = (heapN) / 2; i >= 1; i--)
maxHeap(i, heapN, begin);
}
// Heap Sort
private void heapSort(int begin, int end) {
int heapN = end - begin;
this.heapify(begin, end, heapN);
for (int i = heapN; i >= 1; i--) {
// Move current root to end
swap(begin, begin + i);
maxHeap(1, i, begin);
}
}
// Insertion Sort
private void insertionSort(int left, int right) {
for (int i = left; i <= right; i++) {
int key = a[i];
int j = i;
while (j > left && a[j - 1] > key) {
a[j] = a[j - 1];
j--;
}
a[j] = key;
}
}
private int findPivot(int a1, int b1, int c1) {
int max = Math.max(Math.max(a[a1], a[b1]), a[c1]);
int min = Math.min(Math.min(a[a1], a[b1]), a[c1]);
int median = max ^ min ^ a[a1] ^ a[b1] ^ a[c1];
if (median == a[a1])
return a1;
if (median == a[b1])
return b1;
return c1;
}
private int partition(int low, int high) {
// pivot
int pivot = a[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (a[j] <= pivot) {
i++;
swap(i, j);
}
}
swap(i + 1, high);
return (i + 1);
}
private void sortDataUtil(int begin, int end, int depthLimit) {
if (end - begin > 16) {
if (depthLimit == 0) {
this.heapSort(begin, end);
return;
}
depthLimit = depthLimit - 1;
int pivot = findPivot(begin,
begin + ((end - begin) / 2) + 1,
end);
swap(pivot, end);
int p = 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);
}
}
private void sortData() {
// Initialise the depthLimit
int depthLimit = (int) (2 * Math.floor(Math.log(n) /
Math.log(2)));
this.sortDataUtil(0, n - 1, depthLimit);
}
private void printData() {
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = s.nextInt();
int[] arr = new int[n];
System.out.print("Enter the elements: ");
for (int i = 0; i < n; i++)
arr[i] = s.nextInt();
// function call
intro_sort is = new intro_sort(n);
for (int i = 0; i < n; i++) {
is.dataAppend(arr[i]);
}
is.sortData();
System.out.print("Sorted array: ");
is.printData();
}
}