10 merge sort

Click here to load reader

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of 10 merge sort

Elementary Sorts INFO I308

Merge Sort

1Merge SortThe Merge Sort Algorithm is based on a simple operation known asmerging:combining two ordered arrays to make one larger ordered array.To sort an array: divide it into two halvessort the two halves (recursively)and then merge the results.Merge sort guarantees to sort an array of N items in time proportional to N log N, no matter what the input.

2Merge Sort (cont.)Recursion:Recursionincomputer scienceis a method where the solution to a problem depends on solutions to smaller instances of the same problem.Most computer programming languages support recursion by allowing afunctionto call itself within the program text.In mathematics, recursion is frequently used. The most common example is the factorial:For example, 5! = 5(4)(3)(2)(1), or5! = 5(4!)Merge Sort (cont.)Recursion (cont.):In other words,


This definition says that 0! is 1, while the factorial of any other number is that number times the factorial of one less than that number.

Merge Sort (cont.)Recursion (cont.):Our definition is recursive. Consider 4!4! = 4(4-1)! = 4(3!)What is 3!? We apply the definition again4! = 4(3!) = 4[3(3-1)!] = 4(3)(2!)And so on4! = 4(3!) = 4(3)(2!) = 4(3)(2)(1!) = 4(3)(2)(1)(0!) = 4(3)(2)(1)(1) = 24

Merge Sort (cont.)Recursion (cont.):All good recursive definitions have these two key characteristics:There are one or more base cases for which no recursion is applied.All chains of recursion eventually end up at one of the base cases.The simplest way for these two conditions to occur is for each recursion to act on a smaller version of the original problem. A very small version of the original problem that can be solved without recursion becomes the base case.

Merge Sort (cont.)Merge Sort Algorithm:1. Merge Sort (Overview):Split array into two halvesSort the left half (recursively)Sort the right half (recursively)Merge the two sorted halves

Merge Sort (cont.)Merge Sort Algorithm (cont.):2. Merging two halves:Access the first item from both halvesWhile neither half is finishedCompare the current items of bothCopy smaller current item to the outputAccess next item from that input halfCopy any remaining from first half to outputCopy any remaining from second half to output

Merge Sort (cont.)Merging:The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1x2xm) and Y(y1y2yn) the resulting list is Z(z1z2zm+n)Example: L1 = { 3 8 9 } L2 = { 1 5 7 }merge(L1, L2) = { 1 3 5 7 8 9 }

Merge Sort (cont.)Merging (cont.):3102354152575X:Y:Result:Merge Sort (cont.)Merging (cont.):3102354525751X:Y:Result:Merge Sort (cont.)Merging (cont.):1023545257513X:Y:Result:Merge Sort (cont.)Merging (cont.):1023542575135X:Y:Result:Merge Sort (cont.)Merging (cont.):2354257513510X:Y:Result:Merge Sort (cont.)Merging (cont.):5425751351023X:Y:Result:Merge Sort (cont.)Merging (cont.):5475135102325X:Y:Result:Merge Sort (cont.)Merging (cont.):7513510232554X:Y:Result:Merge Sort (cont.)Merging (cont.):1351023255475X:Y:Result:Merge Sort (cont.)Merging a two lists of one element each is the same as sorting them.Merge sort divides up an unsorted list until the above condition is met and then sorts the divided parts back together in pairs.Specifically this can be done by recursively dividing the unsorted list in half, merge sorting the left side then the right side and then merging the right and left back together.

Merge Sort (cont.)Merge Sort Example:996861558358640Merge Sort (cont.)Merge Sort Example:996861558358640996861558358640Merge Sort (cont.)Merge Sort Example:996861558358640996861558358640861599658358640Merge Sort (cont.)Merge Sort Example:996861558358640996861558358640861599658358640996861558358640Merge Sort (cont.)Merge Sort Example:99686155835864099686155835864086159965835864099686155835864040Merge Sort (cont.)Merge Sort Example:99686155835860440MergeMerge Sort (cont.)Merge Sort Example:996861558358604Merge158669935580486Merge Sort (cont.)Merge Sort Example:Merge158669958350486615869904355886Merge Sort (cont.)Merge Sort Example:Merge615869904355886046153558868699Merge Sort (cont.)Merge Sort Example:046153558868699Merge Sort (cont.)