merge-sort by xiangpeng

VIEWS: 21 PAGES: 14

									                                Merge Sort

Divide & Conquer
  Divide into two halves
    Recursively sort the two
    Merge the two sorted lists




          A    L    G    O      R   I   T    H   M   S




                                                         1
                                Merge Sort

Divide & Conquer
  Divide into two halves
    Recursively sort the two
    Merge the two sorted lists




      A    L    G    O      R       I   T    H   M   S




                                                         2
                                Merge Sort

Divide & Conquer
  Divide into two halves
    Recursively sort the two
    Merge the two sorted lists




      A    G    L    O      R       H   I    M   S   T




                                                         3
                               Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




     smallest                    smallest


       A       G   L   O   R         H      I   M   S    T


           A                                                 auxiliary array




                                                                               4
                                  Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




          smallest                 smallest


      A       G       L   O   R       H       I   M   S   T


          A       G                                           auxiliary array




                                                                                5
                                      Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                  smallest             smallest


      A       G       L       O   R       H       I   M   S   T


          A       G       H                                       auxiliary array




                                                                                    6
                                          Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                  smallest                        smallest


      A       G       L       O       R       H      I       M   S   T


          A       G       H       I                                      auxiliary array




                                                                                           7
                                              Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                  smallest                                smallest


      A       G       L       O       R           H   I      M       S   T


          A       G       H       I       L                                  auxiliary array




                                                                                               8
                                              Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                          smallest                         smallest


      A       G       L       O       R            H   I      M       S   T


          A       G       H       I       L    M                              auxiliary array




                                                                                                9
                                              Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                          smallest                                 smallest


      A       G       L       O       R            H       I   M      S       T


          A       G       H       I       L    M       O                          auxiliary array




                                                                                                    10
                                              Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                                  smallest                             smallest


      A       G       L       O       R            H       I       M      S       T


          A       G       H       I       L    M       O       R                      auxiliary array




                                                                                                        11
                                              Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                                      first half
                                      exhausted                        smallest


      A       G       L       O       R            H       I       M       S      T


          A       G       H       I       L    M       O       R       S              auxiliary array




                                                                                                        12
                                              Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                                      first half
                                      exhausted                                smallest


      A       G       L       O       R            H       I       M       S       T


          A       G       H       I       L    M       O       R       S       T          auxiliary array




                                                                                                            13
                                              Merge Sort

Merge.
 Keep track of smallest element in each sorted half.
    Insert smallest of two elements into auxiliary array.
    Repeat until done.




                                      first half                                   second half
                                      exhausted                                     exhausted



      A       G       L       O       R            H       I       M       S       T


          A       G       H       I       L    M       O       R       S       T          auxiliary array




                                                                                                            14

								
To top