Skip to content

Instantly share code, notes, and snippets.

@yukinoato
Created March 23, 2012 07:48
Show Gist options
  • Select an option

  • Save yukinoato/2168049 to your computer and use it in GitHub Desktop.

Select an option

Save yukinoato/2168049 to your computer and use it in GitHub Desktop.
MergeSorter in loop version
import java.util.Random;
public class ArrayUtil {
public static Random generator = new Random();
/**
* Creates an array filled with random values.
* @param length the length of the array
* @param n the number of possible random values
* @return an array filled with length numbers between 0 and n-1
*/
public static int[] randomIntArray(int length, int n){
int[] a = new int[length];
for(int i=0; i<a.length; i++)
a[i] = generator.nextInt(n);
return a;
}
}
/**
* This class sorts an array, using the merge sort algorithm.
*/
public class LoopMergeSorter {
private int[] a;
/**
* Constructs a loop merge sorter.
* @param anArray the array to sort
*/
public LoopMergeSorter(int[] anArray){
a = anArray;
}
/**
* Sorts the array managed by the merge sorter.
*/
public void sort(){
// i is the value of numbers in the two arrays to merge each round
for (int i=1; i<a.length; i=i*2){
int[] first = new int[i];
int[] second = new int[i];
// j is the value of array a to start copy
for(int j=0; j<a.length-i; j=j+i*2){
// Copy the first array values from array a from j to j+i
for(int k=0; k<first.length; k++){
first[k] = a[j+k];
}
// Copy the second array values from array a from j+i to j+i*2
for(int k=0; k<second.length; k++){
second[k] = a[j+first.length+k];
}
merge(first, second,j);
}
}
}
/**
* Merges two sorted arrays into the array managed by this merge sorter
* @param first the first sorted array
* @param second the second sorted array
* @param count the current array number
*/
public void merge(int[] first, int[] second, int count){
int iFirst = 0; // Next element to consider in the first array
int iSecond = 0; // Next element to consider in the second array
int j = count; // Next open position in a
// As long as neither iFirst nor iSecond past the end, move
// the smaller element into a
while(iFirst < first.length && iSecond < second.length){
if(first[iFirst] < second[iSecond]){
a[j] = first[iFirst];
iFirst++;
}
else{
a[j] = second[iSecond];
iSecond++;
}
j++;
}
// Note that only one of the two loops below copies entries
// Copy any remaining entries of the first array
while(iFirst < first.length){
a[j] = first[iFirst];
iFirst++; j++;
}
//Copy any remaining entries of the second half
while(iSecond < second.length){
a[j] = second[iSecond];
iSecond++; j++;
}
}
}
import java.util.Arrays;
public class LoopMergeSorterTester {
public static void main(String[] args){
int[] a = ArrayUtil.randomIntArray(16, 100);
System.out.println(Arrays.toString(a));
LoopMergeSorter sorter = new LoopMergeSorter(a);
sorter.sort();
System.out.println(Arrays.toString(a));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment