SlideShare a Scribd company logo
Merge Sort



Presentation By: Justin Corpron
In the Beginning…
         John von Neumann
           (1903-1957)
          Stored program
          Developed merge sort
           for EDVAC in 1945
Merging
   The key to Merge Sort is merging two sorted
    lists into one, such that if you have two lists
    X (x1≤x2≤…≤xm) and Y(y1≤y2≤…≤yn) the
    resulting list is Z(z1≤z2≤…≤zm+n)
 Example:
L1 = { 3 8 9 } L2 = { 1 5 7 }
merge(L1, L2) = { 1 3 5 7 8 9 }
Merging (cont.)
                     The merge
                      Algorithm:
                      From The Art of
                      Computer
                      Programming vol2:
                      Sorting and Searching
                      by Donald E. Knuth
                      (page 160).
Merging (cont.)
  X:      3   10   23   54 Y:   1   5   25   75


Result:
Merging (cont.)
  X:      3       10   23   54 Y:    5   25   75


Result:       1
Merging (cont.)
  X:          10       23   54 Y:    5   25   75


Result:   1        3
Merging (cont.)
  X:          10       23       54 Y:    25   75


Result:   1        3        5
Merging (cont.)
  X:              23       54 Y:    25   75


Result:   1   3        5    10
Merging (cont.)
  X:                    54 Y:        25   75


Result:   1   3     5    10     23
Merging (cont.)
  X:                    54 Y:             75


Result:   1   3     5    10     23   25
Merging (cont.)
  X:                     Y:                  75


Result:   1   3     5   10    23   25   54
Merging (cont.)
  X:                     Y:



Result:   1   3     5   10    23   25   54   75
Divide And Conquer
   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
    right side then the left side and then merging the
    right and left back together.
Merge Sort Algorithm
Given a list L with a length k:
 If k == 1  the list is sorted
 Else:
  – Merge Sort the left side (0 thru k/2)
  – Merge Sort the right side (k/2+1 thru k)
  – Merge the right side with the left side
Merge Sort Example
99   6   86 15 58 35 86   4   0
Merge Sort Example
 99       6    86 15 58 35 86    4       0


99    6       86 15   58 35 86       4       0
Merge Sort Example
      99       6    86 15 58 35 86    4       0


     99    6       86 15   58 35 86       4       0


99   6             86 15   58 35      86      4       0
Merge Sort Example
       99       6    86 15 58 35 86    4       0


      99    6       86 15   58 35 86       4       0


 99   6             86 15   58 35      86      4           0


99    6         86     15   58   35    86              4       0
Merge Sort Example
       99       6    86 15 58 35 86    4       0


      99    6       86 15   58 35 86       4       0


 99   6             86 15   58 35      86      4           0


99    6         86     15   58   35    86              4       0


                                                       4       0
Merge Sort Example




99      6     86   15   58   35   86   0   4


Merge                                  4   0
Merge Sort Example




 6      99      15 86    58 35     0    4       86


99      6      86   15   58   35   86       0        4


Merge
Merge Sort Example


        6    15 86 99   0   4   35 58 86


 6      99      15 86   58 35      0   4   86




Merge
Merge Sort Example
            0    4   6   15 35 58 86 86 99


        6       15 86 99     0   4   35 58 86




Merge
Merge Sort Example
0    4   6   15 35 58 86 86 99
Implementing Merge Sort
   There are two basic ways to implement merge sort:
    – In Place: Merging is done with only the input array
          Pro: Requires only the space needed to hold the array
          Con: Takes longer to merge because if the next element is in the
           right side then all of the elements must be moved down.
    – Double Storage: Merging is done with a temporary array
      of the same size as the input array.
          Pro: Faster than In Place since the temp array holds the resulting
           array until both left and right sides are merged into the temp
           array, then the temp array is appended over the input array.
          Con: The memory requirement is doubled.
          The merge sort in the text (ch7.6:p238-239) is an example of a
           double array implementation with Comparable objects.
Merge Sort Analysis
The Double Memory Merge Sort runs O (N log N) for
all cases, because of its Divide and Conquer approach.
          T(N) = 2T(N/2) + N = O(N logN)
Finally…
        There are other variants of Merge Sorts including k-
way merge sorting, but the common variant is the Double
Memory Merge Sort. Though the running time is O(N logN)
and runs much faster than insertion sort and bubble sort,
merge sort’s large memory demands makes it not very
practical for main memory sorting.
Important things to remember for the Midterm:
• Best Case, Average Case, and Worst Case = O(N logN)
• Storage Requirement: Double that needed to hold the array
to be sorted.

More Related Content

What's hot

Composition Of Functions & Difference Quotient
Composition Of Functions & Difference QuotientComposition Of Functions & Difference Quotient
Composition Of Functions & Difference Quotient
cpirie0607
 
7 5 the other trig functions
7 5 the other trig functions7 5 the other trig functions
7 5 the other trig functions
hisema01
 
Merge sort
Merge sortMerge sort
Merge sort
Kumar
 

What's hot (20)

5 i y 5h
5 i y 5h5 i y 5h
5 i y 5h
 
5 d
5 d5 d
5 d
 
5 f y 5 g
5 f y 5 g5 f y 5 g
5 f y 5 g
 
5 a
5 a5 a
5 a
 
5 e
5 e5 e
5 e
 
Composition Of Functions & Difference Quotient
Composition Of Functions & Difference QuotientComposition Of Functions & Difference Quotient
Composition Of Functions & Difference Quotient
 
ON SQUARING A NUMBER AND T-SEMI PRIME NUMBER
ON SQUARING A NUMBER AND T-SEMI PRIME NUMBER ON SQUARING A NUMBER AND T-SEMI PRIME NUMBER
ON SQUARING A NUMBER AND T-SEMI PRIME NUMBER
 
Radix sort concept
Radix sort conceptRadix sort concept
Radix sort concept
 
Sketch root locus
Sketch root locusSketch root locus
Sketch root locus
 
25 String Matching
25 String Matching25 String Matching
25 String Matching
 
Quick sort algorithm using slide presentation , Learn selection sort example ...
Quick sort algorithm using slide presentation , Learn selection sort example ...Quick sort algorithm using slide presentation , Learn selection sort example ...
Quick sort algorithm using slide presentation , Learn selection sort example ...
 
Question 4 Math 1
Question 4 Math 1Question 4 Math 1
Question 4 Math 1
 
3.5 notes
3.5 notes3.5 notes
3.5 notes
 
4 ESO Academics - Unit 03 - Exercises 4.3.3 - Division of Polynomials. Ruffin...
4 ESO Academics - Unit 03 - Exercises 4.3.3 - Division of Polynomials. Ruffin...4 ESO Academics - Unit 03 - Exercises 4.3.3 - Division of Polynomials. Ruffin...
4 ESO Academics - Unit 03 - Exercises 4.3.3 - Division of Polynomials. Ruffin...
 
Rabin karp string matching algorithm
Rabin karp string matching algorithmRabin karp string matching algorithm
Rabin karp string matching algorithm
 
Flyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest pathFlyod's algorithm for finding shortest path
Flyod's algorithm for finding shortest path
 
Rabin Carp String Matching algorithm
Rabin Carp String Matching  algorithmRabin Carp String Matching  algorithm
Rabin Carp String Matching algorithm
 
Sequence Alignment
Sequence AlignmentSequence Alignment
Sequence Alignment
 
7 5 the other trig functions
7 5 the other trig functions7 5 the other trig functions
7 5 the other trig functions
 
Merge sort
Merge sortMerge sort
Merge sort
 

Similar to 24 Cs146 Jc Merge

Lecture 05 b radicals multiplication and division
Lecture 05 b radicals multiplication and divisionLecture 05 b radicals multiplication and division
Lecture 05 b radicals multiplication and division
Hazel Joy Chong
 

Similar to 24 Cs146 Jc Merge (20)

Mergesort
MergesortMergesort
Mergesort
 
merge sort help in language C with algorithms
merge sort help in language C with algorithmsmerge sort help in language C with algorithms
merge sort help in language C with algorithms
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Chapter 3: Linear Systems and Matrices - Part 2/Slides
Chapter 3: Linear Systems and Matrices - Part 2/SlidesChapter 3: Linear Systems and Matrices - Part 2/Slides
Chapter 3: Linear Systems and Matrices - Part 2/Slides
 
Python week4-lecture1-handout
Python week4-lecture1-handoutPython week4-lecture1-handout
Python week4-lecture1-handout
 
0.3 Factoring Polynomials
0.3 Factoring Polynomials0.3 Factoring Polynomials
0.3 Factoring Polynomials
 
Merge sort
Merge sortMerge sort
Merge sort
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Lecture 05 b radicals multiplication and division
Lecture 05 b radicals multiplication and divisionLecture 05 b radicals multiplication and division
Lecture 05 b radicals multiplication and division
 
23 Matrix Algorithms
23 Matrix Algorithms23 Matrix Algorithms
23 Matrix Algorithms
 
Lesson 1 matrix
Lesson 1 matrixLesson 1 matrix
Lesson 1 matrix
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
MODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptxMODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptx
 
Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
Firefly exact MCMC for Big Data
Firefly exact MCMC for Big DataFirefly exact MCMC for Big Data
Firefly exact MCMC for Big Data
 
Algorithms Lab PPT
Algorithms Lab PPTAlgorithms Lab PPT
Algorithms Lab PPT
 

24 Cs146 Jc Merge

  • 2. In the Beginning… John von Neumann (1903-1957)  Stored program  Developed merge sort for EDVAC in 1945
  • 3. Merging  The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1≤x2≤…≤xm) and Y(y1≤y2≤…≤yn) the resulting list is Z(z1≤z2≤…≤zm+n)  Example: L1 = { 3 8 9 } L2 = { 1 5 7 } merge(L1, L2) = { 1 3 5 7 8 9 }
  • 4. Merging (cont.)  The merge Algorithm: From The Art of Computer Programming vol2: Sorting and Searching by Donald E. Knuth (page 160).
  • 5. Merging (cont.) X: 3 10 23 54 Y: 1 5 25 75 Result:
  • 6. Merging (cont.) X: 3 10 23 54 Y: 5 25 75 Result: 1
  • 7. Merging (cont.) X: 10 23 54 Y: 5 25 75 Result: 1 3
  • 8. Merging (cont.) X: 10 23 54 Y: 25 75 Result: 1 3 5
  • 9. Merging (cont.) X: 23 54 Y: 25 75 Result: 1 3 5 10
  • 10. Merging (cont.) X: 54 Y: 25 75 Result: 1 3 5 10 23
  • 11. Merging (cont.) X: 54 Y: 75 Result: 1 3 5 10 23 25
  • 12. Merging (cont.) X: Y: 75 Result: 1 3 5 10 23 25 54
  • 13. Merging (cont.) X: Y: Result: 1 3 5 10 23 25 54 75
  • 14. Divide And Conquer  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 right side then the left side and then merging the right and left back together.
  • 15. Merge Sort Algorithm Given a list L with a length k:  If k == 1  the list is sorted  Else: – Merge Sort the left side (0 thru k/2) – Merge Sort the right side (k/2+1 thru k) – Merge the right side with the left side
  • 16. Merge Sort Example 99 6 86 15 58 35 86 4 0
  • 17. Merge Sort Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0
  • 18. Merge Sort Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0
  • 19. Merge Sort Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0
  • 20. Merge Sort Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 4 0
  • 21. Merge Sort Example 99 6 86 15 58 35 86 0 4 Merge 4 0
  • 22. Merge Sort Example 6 99 15 86 58 35 0 4 86 99 6 86 15 58 35 86 0 4 Merge
  • 23. Merge Sort Example 6 15 86 99 0 4 35 58 86 6 99 15 86 58 35 0 4 86 Merge
  • 24. Merge Sort Example 0 4 6 15 35 58 86 86 99 6 15 86 99 0 4 35 58 86 Merge
  • 25. Merge Sort Example 0 4 6 15 35 58 86 86 99
  • 26. Implementing Merge Sort  There are two basic ways to implement merge sort: – In Place: Merging is done with only the input array  Pro: Requires only the space needed to hold the array  Con: Takes longer to merge because if the next element is in the right side then all of the elements must be moved down. – Double Storage: Merging is done with a temporary array of the same size as the input array.  Pro: Faster than In Place since the temp array holds the resulting array until both left and right sides are merged into the temp array, then the temp array is appended over the input array.  Con: The memory requirement is doubled.  The merge sort in the text (ch7.6:p238-239) is an example of a double array implementation with Comparable objects.
  • 27. Merge Sort Analysis The Double Memory Merge Sort runs O (N log N) for all cases, because of its Divide and Conquer approach. T(N) = 2T(N/2) + N = O(N logN)
  • 28. Finally… There are other variants of Merge Sorts including k- way merge sorting, but the common variant is the Double Memory Merge Sort. Though the running time is O(N logN) and runs much faster than insertion sort and bubble sort, merge sort’s large memory demands makes it not very practical for main memory sorting. Important things to remember for the Midterm: • Best Case, Average Case, and Worst Case = O(N logN) • Storage Requirement: Double that needed to hold the array to be sorted.