Last active
April 19, 2018 12:46
-
-
Save steghio/bfbfa6f727175433247294e75fa39991 to your computer and use it in GitHub Desktop.
Java implementation of the mergesort sorting algorithm for int arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Java implementation of the mergesort sorting algorithm for int arrays |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.groglogs.mergesort; | |
public class Mergesort { | |
//need to pass the res array as input since we are pass by value! | |
public static int[] merge(int[] res, int[] left, int[] right){ | |
int left_idx = 0, right_idx = 0, res_idx = 0; | |
if(left == null || right == null || left.length == 0 || right.length == 0) return null; | |
//scan both left and right until we reach either end | |
//CAUTION: the indexes are the easiest part to screw up! | |
//DOUBLE CAUTION: we must loop BOTH arrays at the same time! We don't know how are they sorted! | |
while(left_idx < left.length && right_idx < right.length){ | |
//order the elements while keeping track of the indexes | |
//if left is smaller than right, copy left, increase left, increase result | |
if(left[left_idx] <= right[right_idx]){ | |
res[res_idx] = left[left_idx]; | |
left_idx++; | |
res_idx++; | |
} | |
//otherwise, copy right, increase right, increase result | |
else{ | |
res[res_idx] = right[right_idx]; | |
right_idx++; | |
res_idx++; | |
} | |
} | |
//we are still not finished, maybe we have some elements still in left or right, copy them as well | |
while(left_idx < left.length){ | |
res[res_idx] = left[left_idx]; | |
res_idx++; | |
left_idx++; | |
} | |
while(right_idx < right.length){ | |
res[res_idx] = right[right_idx]; | |
res_idx++; | |
right_idx++; | |
} | |
return res; | |
} | |
public static int[] sort(int[] list){ | |
int[] left, right; | |
if(list == null || list.length == 0) return null; | |
//our base case, a list of a single element is already sorted | |
if(list.length == 1) return list; | |
//split array in two halves | |
//fun fact: Math.floor returns a double | |
left = new int[list.length - (int)Math.floor(list.length / 2)]; | |
right = new int[list.length - left.length]; | |
//copy the elements in the two arrays left and right | |
for(int i = 0; i < left.length; i++){ | |
left[i] = list[i]; | |
} | |
for(int i = left.length, j = 0; i < list.length; i++, j++){ | |
right[j] = list[i]; | |
} | |
//sort both parts separately | |
sort(left); | |
sort(right); | |
//then put everything together at each step | |
return merge(list, left, right); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.groglogs.test.mergesort; | |
import org.junit.Test; | |
import com.blogspot.groglogs.mergesort.Mergesort; | |
import static org.junit.Assert.assertArrayEquals; | |
import static org.junit.Assert.assertEquals; | |
public class MergesortJTests { | |
int [] input, output; | |
@Test | |
//null input, expected [] | |
public void nullEmpty() { | |
input = null; | |
output = null; | |
//null | |
assertEquals("null = null", output, Mergesort.sort(input)); | |
} | |
@Test | |
//single element, expected the element | |
public void singleElement() { | |
input = new int[]{1}; | |
output = new int[]{1}; | |
assertArrayEquals("single 1 = 1", output, Mergesort.sort(input)); | |
} | |
@Test | |
//list length multiple of 2 | |
public void lengthEven(){ | |
input = new int[]{1, 2}; | |
output = new int[]{1, 2}; | |
assertArrayEquals("even 1,2 = 1,2", output, Mergesort.sort(input)); | |
input = new int[]{2, 1}; | |
output = new int[]{1, 2}; | |
assertArrayEquals("even 2,1 = 1,2", output, Mergesort.sort(input)); | |
input = new int[]{3, 7, 9, 2}; | |
output = new int[]{2, 3, 7, 9}; | |
assertArrayEquals("even 3,7,9,2 = 2,3,7,9", output, Mergesort.sort(input)); | |
input = new int[]{3, 3, 3, 1}; | |
output = new int[]{1, 3, 3, 3}; | |
assertArrayEquals("even 3,3,3,1 = 1,3,3,3", output, Mergesort.sort(input)); | |
input = new int[]{9, 8, 4, 1}; | |
output = new int[]{1, 4, 8, 9}; | |
assertArrayEquals("even 9,8,4,1 = 1,4,8,9", output, Mergesort.sort(input)); | |
} | |
@Test | |
//list length not multiple of 2 | |
public void lengthOdd(){ | |
input = new int[]{1, 2, 3}; | |
output = new int[]{1, 2, 3}; | |
assertArrayEquals("odd 1,2,3 = 1,2,3", output, Mergesort.sort(input)); | |
input = new int[]{2, 3, 1}; | |
output = new int[]{1, 2, 3}; | |
assertArrayEquals("odd 2,3,1 = 1,2,3", output, Mergesort.sort(input)); | |
input = new int[]{6, 1, 8, 2, 3}; | |
output = new int[]{1, 2, 3, 6, 8}; | |
assertArrayEquals("odd 6,1,8,2,3 = 1,2,3,6,8", output, Mergesort.sort(input)); | |
input = new int[]{1, 1, 1, 5, 5}; | |
output = new int[]{1, 1, 1, 5, 5}; | |
assertArrayEquals("odd 1,1,1,5,5 = 1,1,1,5,5", output, Mergesort.sort(input)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full description at: http://groglogs.blogspot.ch/2017/03/java-mergesort-sorting-algorithm-for.html