Advertisement
mirandali

BP BROS????

Mar 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. import java.util.Arrays;
  2. public class SortLibrary {
  3.  
  4.     public static void main(String[] args) {
  5.         // Test arrays you can use to check your sorts.
  6.         // They represent common arrangements: random, already sorted, reversed, mostly sorted
  7.         int[] random = new int[]{33,94,9,40,77,82,47,15,51,64,76,28,2,85,11};
  8.         int[] alreadySorted = new int[]{2,9,11,15,28,33,40,47,51,64,76,77,82,85,94};
  9.         int[] reversed = new int[]{94,85,82,77,76,64,51,47,40,33,28,15,11,9,2};
  10.         int[] mostlySorted = new int[]{2,85,11,15,28,33,47,40,51,64,76,77,82,9,94};
  11.         //  int[] longerArray = ArrayImporter.readArrayFile("smallArray.txt");  //***beware repeating elements!
  12.         int[] myCustomTest = new int[]{5,3,69,73,11,17,1,74,34,86}; //***use this one  
  13.  
  14.         // ***Enter your array to sort here
  15.         int[] arrayToSort = myCustomTest; // arrayToSort will point to the array you choose
  16.         int[] copyOfArrayToSort = Arrays.copyOf(arrayToSort, arrayToSort.length);
  17.  
  18.  
  19.         long startTime1 = System.currentTimeMillis();
  20.         mergeSort(arrayToSort);     // Call your sort method -- Remember array is modified in the method, not returned!
  21.         long stopTime1 = System.currentTimeMillis();
  22.  
  23.         long startTime2 = System.currentTimeMillis();
  24.         Arrays.sort(copyOfArrayToSort); // call java.util.Array's sort method for comparison
  25.         long stopTime2 = System.currentTimeMillis();
  26.  
  27.         if(arrayToSort.length < 50) {
  28.             System.out.println("Result after sort: " + Arrays.toString(arrayToSort));
  29.             System.out.println("Result should be: " + Arrays.toString(copyOfArrayToSort));
  30.         }
  31.  
  32.         System.out.println("Sorts match? " + Arrays.equals(arrayToSort, copyOfArrayToSort));
  33.         System.out.println("Time 1: " + (stopTime1 - startTime1) + " ms");
  34.         System.out.println("Time 2: " + (stopTime2 - startTime2) + " ms");
  35.     }
  36.  
  37.  
  38.     // runner
  39.     public static void mergeSort(int[] a) {
  40.         mergeSort(a, 0, a.length );        
  41.     }  
  42.  
  43.     // *** You must use recursion here, but feel free to alter parameters if you want!
  44.     public static void mergeSort(int[] a, int lo, int hi) {
  45.  
  46.        
  47.         // base case?
  48.         /**
  49.         if (lo>= hi) {
  50.             return;
  51.         }
  52.         */
  53.        
  54.         if (lo < hi) {
  55.             int mid = (lo+hi)/2;
  56.  
  57.             // recursive call
  58.            
  59.             mergeSort(a, mid + 1, hi);
  60.             mergeSort(a, lo,  mid);
  61.            
  62.             merge(a, new int[a.length], lo, mid, hi);
  63.         }
  64.        
  65.        
  66.     }
  67.  
  68.     // *** We verified this works, feel free to use as-is (or modify params, etc)
  69.     private static void merge(int[] a, int[] aux, int lo, int mid, int hi) {
  70.         if (mid >= a.length)
  71.             return;
  72.         if (hi > a.length)
  73.             hi = a.length;
  74.  
  75.         int pointer1 = lo, pointer2 = mid;
  76.  
  77.         for (int i = pointer1; i < hi; i++) {
  78.             if (pointer1 == mid)
  79.                 aux[i] = a[pointer2++];
  80.             else if (pointer2 == hi)
  81.                 aux[i] = a[pointer1++];
  82.             else if (a[pointer2] < a[pointer1])
  83.                 aux[i] = a[pointer2++];
  84.             else
  85.                 aux[i] = a[pointer1++];
  86.         }
  87.  
  88.         for (int j = lo; j < hi; j++) // lo and hi still point to original indices
  89.             a[j] = aux[j];
  90.     }  
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement