Merge Sort Through Example AP Computer Science A Mr by fuf15836

VIEWS: 8 PAGES: 4

									Merge Sort Through Example
AP Computer Science A
Mr Fahrenbacher



                                  7   2   9   4   3   8   6   1




                             10   5   7   6   1   4   8   3   2   9
Analysis


                             Bubble Sort        Selection Sort       Insertion Sort          Merge Sort

           50000          6283                3087                  459                 22

       100000             25014               12155                 1827                32

       150000             55508               27027                 4297                40

       200000             99147               48285                 7526                52

       250000             155076              74838                 11611               57

       300000             221177              108743                16912               69

                      Bubble               Selection             Insertion            Merge

      230000




 153333.333




   76666.667




               0
                   2007          50000      100000      150000            200000      250000      300000



                    Stages

           Comparisons Per Stage

           Assignments Per Stage

            Operations Per Stage

              Total Operations
public class MergeSort
{
	   private static int[] storage;
	
	   public static void sort(int[] array) {
	   	       storage = new int[array.length];
	   	       sort(array, 0, array.length-1);
	   }
	
	   public static void sort(int[] array, int lo, int hi) {
	   	       if(lo + 1 == hi) { //sub array with 2 items, might need to swap them!
	   	       	     if(array[lo] > array[hi])
	   	       	     	     swap(array, lo, hi);
	     	     }
	   	       else if (hi > lo) {//sub array with 3 or more items - divide and conquer!
	   	       	     int firstLo = lo;
	   	       	     int firstHi = (lo + hi)/2;
	   	       	     int secondLo = firstHi + 1;
	   	       	     int secondHi = hi;
	     	     	
	   	       	     //split into two sub arrays, sort recursively, then merge together
	     	     	     sort(array, firstLo, firstHi);
	   	       	     sort(array, secondLo, secondHi);
	   	       	     merge(array, firstLo, firstHi, secondLo, secondHi);
	     	     }
	     }

	     //basic swap code
	   public static void swap(int[] array, int i, int j) {
	   	       int temp = array[i];
	   	       array[i] = array[j];
	   	       array[j] = temp;
	   }
	   //Merge Sort class continued!

	   public static void merge(int[] array, int lo1, int hi1, int lo2, int hi2) {
	   	       int currentIndex = lo1;
	   	       int i = lo1;
	   	       int j = lo2;
	     	     	
	   	       while(currentIndex <= hi2) {
	   	       	     if(i > hi1) {//we only have a value from the 2nd array to consider
	     	     	     	     storage[currentIndex] = array[j];
	     	     	     	     j++;
	     	     	     }
	   	       	     else if(j > hi2) {//we only have a value from the 1st array to consider
	     	     	     	     storage[currentIndex] = array[i];
	     	     	     	     i++;
	     	     	     }
	   	       	     else {//we have to consider a value from both of the arrays
	   	       	     	     if(array[i] < array[j]) {
	   	       	     	     	     storage[currentIndex] = array[i];
	   	       	     	     	     i++;
	   	       	     	     }
	   	       	     	     else {
	   	       	     	     	     storage[currentIndex] = array[j];
	   	       	     	     	     j++;
	   	       	     	     }
	   	       	     }
	   	       	     currentIndex++;
	   	       }
	   	
	   	       //copy all the values from the storage array back
	   	       for(int k = lo1; k<= hi1; k++)
	   	       	     array[k] = storage[k];
	     }
}

								
To top