Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class SortLibrary {
- public static void main(String[] args) {
- // Test arrays you can use to check your sorts.
- // They represent common arrangements: random, already sorted, reversed, mostly sorted
- int[] random = new int[]{33,94,9,40,77,82,47,15,51,64,76,28,2,85,11};
- int[] alreadySorted = new int[]{2,9,11,15,28,33,40,47,51,64,76,77,82,85,94};
- int[] reversed = new int[]{94,85,82,77,76,64,51,47,40,33,28,15,11,9,2};
- int[] mostlySorted = new int[]{2,85,11,15,28,33,47,40,51,64,76,77,82,9,94};
- // int[] longerArray = ArrayImporter.readArrayFile("smallArray.txt"); //***beware repeating elements!
- int[] myCustomTest = new int[]{5,3,69,73,11,17,1,74,34,86}; //***use this one
- // ***Enter your array to sort here
- int[] arrayToSort = myCustomTest; // arrayToSort will point to the array you choose
- int[] copyOfArrayToSort = Arrays.copyOf(arrayToSort, arrayToSort.length);
- long startTime1 = System.currentTimeMillis();
- mergeSort(arrayToSort); // Call your sort method -- Remember array is modified in the method, not returned!
- long stopTime1 = System.currentTimeMillis();
- long startTime2 = System.currentTimeMillis();
- Arrays.sort(copyOfArrayToSort); // call java.util.Array's sort method for comparison
- long stopTime2 = System.currentTimeMillis();
- if(arrayToSort.length < 50) {
- System.out.println("Result after sort: " + Arrays.toString(arrayToSort));
- System.out.println("Result should be: " + Arrays.toString(copyOfArrayToSort));
- }
- System.out.println("Sorts match? " + Arrays.equals(arrayToSort, copyOfArrayToSort));
- System.out.println("Time 1: " + (stopTime1 - startTime1) + " ms");
- System.out.println("Time 2: " + (stopTime2 - startTime2) + " ms");
- }
- // runner
- public static void mergeSort(int[] a) {
- mergeSort(a, 0, a.length );
- }
- // *** You must use recursion here, but feel free to alter parameters if you want!
- public static void mergeSort(int[] a, int lo, int hi) {
- // base case?
- /**
- if (lo>= hi) {
- return;
- }
- */
- if (lo < hi) {
- int mid = (lo+hi)/2;
- // recursive call
- mergeSort(a, mid + 1, hi);
- mergeSort(a, lo, mid);
- merge(a, new int[a.length], lo, mid, hi);
- }
- }
- // *** We verified this works, feel free to use as-is (or modify params, etc)
- private static void merge(int[] a, int[] aux, int lo, int mid, int hi) {
- if (mid >= a.length)
- return;
- if (hi > a.length)
- hi = a.length;
- int pointer1 = lo, pointer2 = mid;
- for (int i = pointer1; i < hi; i++) {
- if (pointer1 == mid)
- aux[i] = a[pointer2++];
- else if (pointer2 == hi)
- aux[i] = a[pointer1++];
- else if (a[pointer2] < a[pointer1])
- aux[i] = a[pointer2++];
- else
- aux[i] = a[pointer1++];
- }
- for (int j = lo; j < hi; j++) // lo and hi still point to original indices
- a[j] = aux[j];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement