Created
October 12, 2014 18:21
-
-
Save imduffy15/ae9798cbd533dd281f63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Main { | |
| public static void main(String[] args) { | |
| final int MAXRANDOM = 130; | |
| int[] arrayToBeSorted = new int[1000]; | |
| fillArrayWithRandomIntegers(arrayToBeSorted, MAXRANDOM); | |
| int[] arrayToBeSortedWithMergeSort = new int[1000]; | |
| System.arraycopy(arrayToBeSorted, 0, arrayToBeSortedWithMergeSort, 0, arrayToBeSorted.length); | |
| int[] arrayToBeSortedWithClassifying = new int[1000]; | |
| System.arraycopy(arrayToBeSorted, 0, arrayToBeSortedWithClassifying, 0, arrayToBeSorted.length); | |
| timedMergeSort(arrayToBeSortedWithMergeSort); | |
| timedClassifyingSort(arrayToBeSortedWithClassifying, MAXRANDOM); | |
| } | |
| private static void fillArrayWithRandomIntegers(int[] array, int maxRandom) { | |
| for(int i=0; i < array.length; i++) { | |
| array[i] = (int)(Math.random()*maxRandom); | |
| } | |
| } | |
| private static void timedMergeSort(int[] array) { | |
| Mergesort mergesort = new Mergesort(); | |
| long startTime = System.nanoTime(); | |
| mergesort.sort(array); | |
| long endTime = System.nanoTime(); | |
| long duration = (endTime - startTime); | |
| System.out.println("Merge sort took: " + duration); | |
| // System.out.println("merge sort: "); | |
| // printArray(array); | |
| } | |
| private static void timedClassifyingSort(int[] array, int maxRandom) { | |
| long startTime = System.nanoTime(); | |
| classifyingSort(array, maxRandom); | |
| long endTime = System.nanoTime(); | |
| long duration = (endTime - startTime); | |
| System.out.println("Classifying took: " + duration); | |
| } | |
| private static void classifyingSort(int[] array, int maxRandom) { | |
| int[] counters = new int[maxRandom]; | |
| for(int i=0; i < array.length; i++) { | |
| counters[array[i]]++; | |
| } | |
| array = new int[array.length]; | |
| int currentIndex = 0; | |
| for(int i=0; i < counters.length; i++) { | |
| int amount = counters[i]; | |
| for(int j=0; j < amount; j++) { | |
| array[currentIndex] = i; | |
| currentIndex++; | |
| } | |
| } | |
| // System.out.println("classify sort: "); | |
| // printArray(array); | |
| } | |
| private static void printArray(int[] array) { | |
| for(int i : array) { | |
| System.out.print(i + ", "); | |
| } | |
| System.out.println(); | |
| } | |
| } |
This file contains hidden or 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
| public class Mergesort { | |
| private int[] numbers; | |
| private int[] helper; | |
| private int number; | |
| public void sort(int[] values) { | |
| this.numbers = values; | |
| number = values.length; | |
| this.helper = new int[number]; | |
| mergesort(0, number - 1); | |
| } | |
| private void mergesort(int low, int high) { | |
| // check if low is smaller then high, if not then the array is sorted | |
| if (low < high) { | |
| // Get the index of the element which is in the middle | |
| int middle = low + (high - low) / 2; | |
| // Sort the left side of the array | |
| mergesort(low, middle); | |
| // Sort the right side of the array | |
| mergesort(middle + 1, high); | |
| // Combine them both | |
| merge(low, middle, high); | |
| } | |
| } | |
| private void merge(int low, int middle, int high) { | |
| // Copy both parts into the helper array | |
| for (int i = low; i <= high; i++) { | |
| helper[i] = numbers[i]; | |
| } | |
| int i = low; | |
| int j = middle + 1; | |
| int k = low; | |
| // Copy the smallest values from either the left or the right side back | |
| // to the original array | |
| while (i <= middle && j <= high) { | |
| if (helper[i] <= helper[j]) { | |
| numbers[k] = helper[i]; | |
| i++; | |
| } else { | |
| numbers[k] = helper[j]; | |
| j++; | |
| } | |
| k++; | |
| } | |
| // Copy the rest of the left side of the array into the target array | |
| while (i <= middle) { | |
| numbers[k] = helper[i]; | |
| k++; | |
| i++; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment