forked from sachith-1/helloAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort_hybrid.java
More file actions
68 lines (59 loc) · 1.9 KB
/
quick_sort_hybrid.java
File metadata and controls
68 lines (59 loc) · 1.9 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
//This is a hybrid advanced quick sort algorithm implemented by using quicksort and insertion sort
import java.io.*;
class GFG {
private static int partition(int arr[], int high, int low){
int pivot = arr[high];
int i = low;
int j = low;
while (i <= high) {
if (arr[i] > pivot)
i++;
else {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j++;
}
}
return j - 1;
}
private static void insertionSort(int a[], int high, int low){
for (int i = low + 1; i <= high; i++) {
for (int j = i - 1; j >= low; j--) {
if (a[j] > a[j + 1]) {
// Swap
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
else
break;
}
}
}
public static void hybridAdvancedQuickSort(int arr[], int low, int high){
while (low < high) {
if (high - low < 10) {
insertionSort(arr, high, low);
break;
}else {
int pivot = partition(arr, high, low);
if (pivot - low < pivot - high) {
hybridAdvancedQuickSort(arr, low, pivot - 1);
low = pivot + 1;
}else {
hybridAdvancedQuickSort(arr, pivot + 1, high);
high = pivot - 1;
}
}
}
}
// Main method
public static void main(String[] args){
int arr[] = { 24, 97, 40, 67, 88, 85, 15, 66, 53, 44, 26, 48, 16, 52, 45, 23, 90, 18, 49, 80, 23 };
hybridAdvancedQuickSort(arr, 0, arr.length - 1);
for (int i : arr)
System.out.print(i + " ");
}
}