Presentation is loading. Please wait.

Presentation is loading. Please wait.

 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.

Similar presentations


Presentation on theme: " MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions."— Presentation transcript:

1

2  MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.  It works by recursively splitting the array into two parts ( ◦ at the midpoint if the number of elements is even, otherwise the right half has one extra ◦ recursing onto each of the two halves. Afterwards, the two halves are merged together

3  Here is a visual example of how MergeSort recursively splits the array in half for sorting. I'm using a diagram of a binary tree to represent the steps in our recursion process. Once the array is recursively split into single elements (3, 5, 4, 9, and 2 above), according to the recursive definition we know that those elements are in a sorted order, right?

4 An array {3} is sorted, because it has only one element. When the recursion is rolling backwards (after expanding), it merges the two branched columns together. Here's a graphical example of how the last merge works (merging [3][5] with [2][4][9]): The first comparison is made:

5 You may be wondering why it's [2][4][9] and not [4][9][2]. It's [2][4][9], because the array we're merging is already sorted as it was merged the same way in all of the recursions below the current one (below, like in the tree). It appears that 2 is less than 3, so 2 becomes the first element in our array and is removed from the sub-array. The second comparison is made:

6  The merging continues as explained while both arrays have elements.  As soon as one of the arrays becomes empty, all the elements of the other array are blindly (without any comparisons) appended onto our final array.  Those elements are already in ascending (sorted) order and are higher than all of the elements presently in the final array.  By 'final array', I'm referring to the array we're building while merging the two sub-arrays together... it is the array that our MergeSort method returns.

7  merge sort works as follows: ◦ Divide the unsorted list into two sublists of about half the size ◦ Sort each of the two sublists ◦ Merge the two sorted sublists back into one sorted list.  The algorithm was invented by John von Neumann in 1945.

8  public int[] mergeSort(int array[])  // pre: array is full, all elements are valid integers (not null)  // post: array is sorted in ascending order (lowest to highest)  {  // if the array has more than 1 element, we need to split it and merge the sorted halves  if(array.length > 1)  {  // number of elements in sub-array 1  // if odd, sub-array 1 has the smaller half of the elements e.g. if 7 elements total, sub-array 1 will have 3, and sub-array 2 will have 4  int elementsInA1 = array.length/2;  // since we want an even split, we initialize the length of sub-array 2 to equal the length of sub-array 1  int elementsInA2 = elementsInA1;  // if the array has an odd number of elements, let the second half take the extra one  if((array.length % 2) == 1)  elementsInA2 += 1;  // declare and initialize the two arrays once we've determined their sizes  int arr1[] = new int[elementsInA1];  int arr2[] = new int[elementsInA2];  // copy the first part of 'array' into 'arr1', causing arr1 to become full  for(int i = 0; i < elementsInA1; i++)  arr1[i] = array[i];  // copy the remaining elements of 'array' into 'arr2', causing arr2 to become full  for(int i = elementsInA1; i < elementsInA1 + elementsInA2; i++)  arr2[i - elementsInA1] = array[i];  // recursively call mergeSort on each of the two sub-arrays that we've just created  // note: when mergeSort returns, arr1 and arr2 will both be sorted!  // it's not magic, the merging is done below, that's how mergesort works :)  arr1 = mergeSort(arr1);  arr2 = mergeSort(arr2);   //the three variables below are indexes that we'll need for merging [i] stores the index of the main array.  // know where to place the smallest element from the two sub-arrays. [j] stores the index of which element from arr1 currently being  //compared [k] stores the index of which element from arr2 is currently being compared  int i = 0, j = 0, k = 0; 

9  // the below loop will run until one of the sub-arrays becomes empty  // it means until the index equals the length of the sub-array  while(arr1.length != j && arr2.length != k)  {  // if the current element of arr1 is less than current element of arr2  if(arr1[j] < arr2[k])  {  // copy the current element of arr1 into the final array  array[i] = arr1[j];  // increase the index of the final array to avoid replacing the element which we've just added  i++;  // increase the index of arr1 to avoid comparing the element which we've just added  j++;  }  // if the current element of arr2 is less than current element of arr1  else  {  // copy the current element of arr1 into the final array  array[i] = arr2[k];  // increase the index of the final array to avoid replacing the element which we've just added  i++;  // increase the index of arr2 to avoid comparing the element which we've just added  k++;  }  // at this point, one of the sub-arrays has been exhausted and there are no more elements in it to compare. this means that all the elements in the remaining array are the highest (and sorted), so it's safe to copy them all into thefinal array.  while(arr1.length != j)  {  array[i] = arr1[j];  i++;  j++;  }  while(arr2.length != k)  {  array[i] = arr2[k];  i++;  k++;  }  // return the sorted array to the caller of the function  return array;

10  public int[] mergeSort(int array[])  {  if(array.length > 1) {  int elementsInA1 = array.length/2;  int elementsInA2 = elementsInA1;  if((array.length % 2) == 1)  elementsInA2 += 1;  int arr1[] = new int[elementsInA1];  int arr2[] = new int[elementsInA2];  for(int i = 0; i < elementsInA1; i++)  arr1[i] = array[i];  for(int i = elementsInA1; i < elementsInA1 + elementsInA2; i++)  arr2[i - elementsInA1] = array[i];  arr1 = mergeSort(arr1);  arr2 = mergeSort(arr2);  int i = 0, j = 0, k = 0; while(arr1.length != j && arr2.length != k) {  if(arr1[j] < arr2[k]) {  array[i] = arr1[j];  i++;  j++; }  else {  array[i] = arr2[k];  i++;  k++; } } //end while  while(arr1.length != j) {  array[i] = arr1[j];  i++;  j++; }  while(arr2.length != k){  array[i] = arr2[k];  i++;  k++; } }  return array;

11  1) Use Mergesort to sort numbers.txt and names.txt  2) Modify the code to add a name or number, and sort.


Download ppt " MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions."

Similar presentations


Ads by Google