-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHybridSort.java
More file actions
83 lines (74 loc) · 2.53 KB
/
HybridSort.java
File metadata and controls
83 lines (74 loc) · 2.53 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
import java.util.Arrays;
class Sorters {
static Integer[] Sorted;
Sorters(Integer[] array) {
Sorted = new Integer[array.length];
}
public Integer[] InsertionSort(Integer[] array) {
int j;
int temp;
int idx = 0;
for (int i = 1; i < array.length; i++) {
temp = array[i];
j = i - 1;
while (j >= 0 && array[j] > temp)
array[j + 1] = array[j--];
array[j + 1] = temp;
}
for (Integer number : array)
Sorted[idx++] = number;
return Sorted;
}
static void merge(Integer[] array, int left, int mid, int right) {
int i = left;
int j = (mid + 1);
int k = left;
while (i <= mid && j <= right) {
if (array[i] < array[j])
Sorted[k++] = array[i++];
else
Sorted[k++] = array[j++];
}
for (; i <= mid; i++)
Sorted[k++] = array[i];
for (; j <= right; j++)
Sorted[k++] = array[j];
for (k = left; k <= right; k++)
array[k] = Sorted[k];
}
public Integer[] MergeSort(Integer[] array, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
MergeSort(array, left, mid);
MergeSort(array, mid + 1, right);
merge(array, left, mid, right);
}
return Sorted;
}
}
public class HybridSort {
public Integer[] sort(Integer[] array, Integer thresholdValue, Sorters sort) {
Integer SIZE = array.length;
System.out.println("Len of array : N = " + SIZE + ", K = " + thresholdValue + " "
+ (SIZE <= thresholdValue ? "sort.InsertionSort(array)" : "sort.MergeSort(array, 0, SIZE - 1)"));
return SIZE <= thresholdValue ? sort.InsertionSort(array) : sort.MergeSort(array, 0, SIZE - 1);
}
public static void main(String[] args) {
HybridSort hs = new HybridSort();
Integer[][] lists = { { 5, 4, 4, 5, 47, 5, 4, 8, 4, 6, 4, 4, 5, 8 },
{ 64, 654, 684, 351, 6541, 21, 643, 216, 54, 321,
65 },
{ 8, 5, 4, 2, 6, 12, 6, 2, 6, 1, 6, 1, 6, 2, 5, 0,
45, 2, 5, 5 },
{ 9, 54, 74, 2, 14, 5, 4, 4, 0, 5, 8015, 05, 5 }, { 64, 58, 4, 56, 46, 13, 5, 4, 3, 58, 7, 4 } };
Integer[] kValConfiguration = { lists[0].length / 2,
lists[1].length * 2, lists[2].length * 2,
lists[3].length / 3, lists[4].length * 2 };
Integer cnf = 0;
for (Integer[] list : lists) {
Sorters sort = new Sorters(list);
Integer THRESHOLD = kValConfiguration[cnf++];
System.out.println(Arrays.toString(hs.sort(list, THRESHOLD, sort)));
}
}
}