Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a88113f

Browse files
add modularMergeSort
1 parent 680c8e8 commit a88113f

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

‎src/main/java/ir/sk/algorithm/others/Sort.java‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public static void insertionSort(int[] array) {
115115

116116
/* Move elements of arr[0..i-1], that are
117117
greater than key, to one position ahead
118-
of their current position */
118+
of their current position
119+
searching and shift at once */
119120
while (j >= 0 && array[j] > array[i]) {
120121
// using rotate by condition(array[j] > key), no swap, since shifting has better performance than swap
121122
array[j + 1] = array[j];
@@ -331,6 +332,49 @@ else if (aux[j] < aux[i])
331332
}
332333

333334

335+
////////////////////////////// Merge Sort ////////////////////////
336+
public static int[] modularMergeSort(int[] array) {
337+
return modularMergeSort(array, 0, array.length - 1);
338+
}
339+
private static int[] modularMergeSort(int[] array, int start, int end) {
340+
if (start == end)
341+
return new int[] {array[start]};
342+
int mid = start + (end - start) / 2;
343+
int[] leftArray = modularMergeSort(array, start, mid);
344+
int[] rightArray = modularMergeSort(array, mid + 1, end);
345+
return myMerge(leftArray, rightArray);
346+
}
347+
348+
private static int[] myMerge(int[] leftArray, int[] rightArray) {
349+
int length = leftArray.length + rightArray.length;
350+
int[] mergedArray = new int[length];
351+
352+
int i = 0, j = 0, k = 0;
353+
for (; k < length - 1 && i < leftArray.length && j < rightArray.length; k++) {
354+
if (leftArray[i] <= rightArray[j]) {
355+
mergedArray[k] = leftArray[i];
356+
i++;
357+
} else {
358+
mergedArray[k] = rightArray[j];
359+
j++;
360+
}
361+
}
362+
363+
if (i == leftArray.length) {
364+
for (; j < rightArray.length; j++) {
365+
mergedArray[k] = rightArray[j];
366+
k++;
367+
}
368+
} else if (j == rightArray.length) {
369+
for (; i < leftArray.length; i++) {
370+
mergedArray[k] = leftArray[i];
371+
k++;
372+
}
373+
}
374+
return mergedArray;
375+
}
376+
////////////////////////////// Merge Sort ////////////////////////
377+
334378
/**
335379
* In quick sort we pick a random element and partition the array, such that all numbers that are less than the
336380
* partitioning element come before all elements that are greater than it. The partitioning can be performed

‎src/test/java/ir/sk/algorithm/others/SortTest.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void insertionSort() throws Exception {
5757
}
5858

5959
@Test
60-
public void ModularInsertionSortTest() {
60+
public void modularInsertionSortTest() {
6161
long start = System.currentTimeMillis();
6262
Sort.modularInsertionSort(actual);
6363
long end = System.currentTimeMillis();
@@ -111,6 +111,15 @@ public void mergeSortIterative() {
111111
assertArrayEquals(expected, actual);
112112
}
113113

114+
@Test
115+
public void modularMergeSortTest() {
116+
long start = System.currentTimeMillis();
117+
actual = Sort.modularMergeSort(actual);
118+
long end = System.currentTimeMillis();
119+
System.out.println("Logic modularMergeSort took: " + (end - start) + "milliseconds");
120+
assertArrayEquals(expected, actual);
121+
}
122+
114123
@Test
115124
public void heapSort() {
116125
long start = System.currentTimeMillis();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /