Why Sorting?
 “When  in doubt, sort” – one of the
 principles of algorithm design. Sorting
 used as a subroutine in many of the
 algorithms:
   Searching in databases: we can do binary
    search on sorted data
   A large number of computer graphics and
    computational geometry problems
   Closest pair, element uniqueness

                                               1
Why Sorting? (2)

 A large number of sorting algorithms are
  developed representing different algorithm
  design techniques.
 A lower bound for sorting W(n log n) is
  used to prove lower bounds of other
  problems



                                               2
Sorting Algorithms so far

 Insertion   sort, selection sort
   Worst-case   running time Q(n2); in-place
 Heap   sort
   Worst-case   running time Q(n log n).




                                                3
Divide and Conquer

   Divide-and-conquer method for algorithm
    design:
     Divide: if the input size is too large to deal with in a
      straightforward manner, divide the problem into two or
      more disjoint subproblems
     Conquer: use divide and conquer recursively to solve
      the subproblems
     Combine: take the solutions to the subproblems and
      “merge” these solutions into a solution for the original
      problem

                                                             4
Merge Sort Algorithm
 Divide: If S has at least two elements (nothing
  needs to be done if S has zero or one elements),
  remove all the elements from S and put them
  into two sequences, S1 and S2 , each containing
  about half of the elements of S. (i.e. S1 contains
  the first n/2elements and S2 contains the
  remaining n/2elements).
 Conquer: Sort sequences S1 and S2 using
  Merge Sort.
 Combine: Put back the elements into S by
  merging the sorted sequences S1 and S2 into
  one sorted sequence
                                                   5
Merge Sort: Algorithm
Merge-Sort(A, p, r)
   if p < r then
      q(p+r)/2
      Merge-Sort(A, p, q)
      Merge-Sort(A, q+1, r)
      Merge(A, p, q, r)


 Merge(A, p, q, r)
    Take the smallest of the two topmost elements of
 sequences A[p..q] and A[q+1..r] and put into the
 resulting sequence. Repeat this, until both sequences
 are empty. Copy the resulting sequence into A[p..r].




                                                         6
MergeSort (Example)




                      7
MergeSort (Example)




                      8
MergeSort (Example)




                      9
MergeSort (Example)




                      10
MergeSort (Example)




                      11
MergeSort (Example)




                      12
MergeSort (Example)




                      13
MergeSort (Example)




                      14
MergeSort (Example)




                      15
MergeSort (Example)




                      16
MergeSort (Example)




                      17
MergeSort (Example)




                      18
MergeSort (Example)




                      19
MergeSort (Example)




                      20
MergeSort (Example)




                      21
MergeSort (Example)




                      22
MergeSort (Example)




                      23
MergeSort (Example)




                      24
MergeSort (Example)




                      25
MergeSort (Example)




                      26
MergeSort (Example)




                      27
MergeSort (Example)




                      28
Merging Two Sequences (cont.)




                                29
Merging Two Sequences (cont.)




                                30
Merging Two Sequences (cont.)




                                31
Merging Two Sequences (cont.)




                                32
Merging Two Sequences (cont.)




                                33
Merge Sort Revisited
   To sort n numbers
     if n=1 done!
     recursively sort 2 lists of
      numbers n/2 and n/2
      elements
     merge 2 sorted lists in Q(n)
      time
   Strategy
     break problem into similar
      (smaller) subproblems
     recursively solve
      subproblems
     combine solutions to answer
                                     34
Recurrences
 Running times of algorithms with Recursive
  calls can be described using recurrences
 A recurrence is an equation or inequality that
  describes a function in terms of its value on
  smaller inputs
                                 solving_trivial_problem                     if n  1
    T (n)  
            num_pieces T (n / subproblem_size_factor)  dividing  combining if n  1

   Example: Merge Sort

                               Q(1)        if n  1
                 T (n)  
                         2T (n / 2)  Q(n) if n  1                             35
Solving Recurrences
   Repeated substitution method
       Expanding the recurrence by substitution and noticing
        patterns
   Substitution method
     guessing the solutions
     verifying the solution by the mathematical induction

 Recursion-trees
 Master method
       templates for different classes of recurrences


                                                             36
Repeated Substitution Method
   Let’s find the running time of merge sort (let’s
    assume that n=2b, for some b).
                           1        if n  1
             T (n)  
                     2T (n / 2)  n if n  1

           T (n)  2T  n / 2   n      substitute
                   2  2T  n / 4   n / 2   n expand
                   22 T (n / 4)  2n substitute
                   22 (2T (n / 8)  n / 4)  2n expand
                    23 T (n / 8)  3n    observe the pattern
           T (n)  2i T (n / 2i )  in
                   2lg n T (n / n)  n lg n  n  n lg n
                                                                37
Repeated Substitution Method
   The procedure is straightforward:
     Substitute
     Expand
     Substitute
     Expand
    …
     Observe a pattern and write how your expression
      looks after the i-th substitution
     Find out what the value of i (e.g., lgn) should be to get
      the base case of the recurrence (say T(1))
     Insert the value of T(1) and the expression of i into
      your expression
                                                             38
Java Implementation of Merge-Sort




                                    39
Java Implementation of MergeSort (cont.)
public class ListMergeSort implements SortObject {

public void sort(Sequence S, Comparator c) {
   int n = S.size();
   if (n < 2) return; //sequence with 0/1 element is sorted.
   // divide
   Sequence S1 = (Sequence)S.newContainer();
   // put the first half of S into S1
   for (int i=1; i <= (n+1)/2; i++) {
      S1.insertLast(S.remove(S.first()));
      }
   Sequence S2 = (Sequence)S.newContainer();
   // put the second half of S into S2
   for (int i=1; i <= n/2; i++) {
      S2.insertLast(S.remove(S.first()));
      }
   sort(S1,c); // recur
   sort(S2,c);
   merge(S1,S2,c,S); // conquer
                                                         40
  }
Java Implementation of MergeSort (cont.)

public void merge(Sequence S1, Sequence S2, Comparator c, Sequence S) {
         while(!S1.isEmpty() && !S2.isEmpty()) {
                   if(c.isLessThanOrEqualTo(S1.first().element(),
                              S2.first().element())) {
                              // S1’s 1st elt <= S2’s 1st elt
                              S.insertLast(S1.remove(S1.first()));
                   }else { // S2’s 1st elt is the smaller one
                              S.insertLast(S2.remove(S2.first()));
                   }
         }if(S1.isEmpty()) {
                   while(!S2.isEmpty()) {
                              S.insertLast(S2.remove(S2.first()));
                   }
         }if(S2.isEmpty()) {
                   while(!S1.isEmpty()) {
                              S.insertLast(S1.remove(S1.first()));
                   }}}
                                                                      41

More Related Content

What's hot (20)

PDF
PPTX
Digital Signal Processing Assignment Help
PPT
Randomized algorithms ver 1.0
DOC
Time and space complexity
PPTX
Signal Processing Assignment Help
PPT
Asymptotic Notation and Complexity
PPT
Algorithm.ppt
PPTX
Signal Processing Assignment Help
PPTX
Asymptotic Notation
DOC
algorithm Unit 4
PDF
PPTX
Signals and systems assignment help
PPTX
Algorithm Assignment Help
PPTX
Signals Processing Homework Help
PPTX
Asymptotic notations(Big O, Omega, Theta )
PPTX
Algorithm Homework Help
PPT
Asymptotic analysis
PDF
Anlysis and design of algorithms part 1
Digital Signal Processing Assignment Help
Randomized algorithms ver 1.0
Time and space complexity
Signal Processing Assignment Help
Asymptotic Notation and Complexity
Algorithm.ppt
Signal Processing Assignment Help
Asymptotic Notation
algorithm Unit 4
Signals and systems assignment help
Algorithm Assignment Help
Signals Processing Homework Help
Asymptotic notations(Big O, Omega, Theta )
Algorithm Homework Help
Asymptotic analysis
Anlysis and design of algorithms part 1
Ad

Viewers also liked (20)

PPT
10강 기업교육론 20110504
KEY
Observations made by the group (Edited)
PPT
C:\Documents And Settings\Lakshmi Menon\My Documents\Twinkling Stars
PPS
Shanxi china
PPS
Plagesdumonde
PDF
ChapmanCreative Porfolio
PPTX
Kpe profile
PPTX
Прориси
PPTX
Printing industry
PPS
The bestof
PPTX
Sti case study chapter 2 3
PAGES
Natural Depths
10강 기업교육론 20110504
Observations made by the group (Edited)
C:\Documents And Settings\Lakshmi Menon\My Documents\Twinkling Stars
Shanxi china
Plagesdumonde
ChapmanCreative Porfolio
Kpe profile
Прориси
Printing industry
The bestof
Sti case study chapter 2 3
Natural Depths
Ad

Similar to Lec22 (20)

PPTX
T2311 - Ch 4_Part1.pptx
PPTX
Data structure 8.pptx
PPTX
Divided and conqurddddddddddddddfffffe.pptx
PPT
Unit-2-Sorting (Merge+Quick+Heap+Binary Searach).ppt
PDF
Sienna 4 divideandconquer
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
PPTX
Divide and Conquer in DAA concept. For B Tech CSE
PPT
3-Chapter Three - Divide and Conquer.ppt
PPT
Divide and conquer
PDF
Skiena algorithm 2007 lecture09 linear sorting
PPTX
Divide and Conquer_Binary_Search_Merge_Sort.pptx
PPTX
Divide and Conquer - Part 1
PPTX
Sorting2
PPTX
State Space Realizations_new.pptx
PDF
PPT
5.5 back tracking 02
PPT
lecture 1
PPT
Counting Sort Lowerbound
PPTX
Merge sort and quick sort
PDF
Daa chapter 2
T2311 - Ch 4_Part1.pptx
Data structure 8.pptx
Divided and conqurddddddddddddddfffffe.pptx
Unit-2-Sorting (Merge+Quick+Heap+Binary Searach).ppt
Sienna 4 divideandconquer
Cs6402 design and analysis of algorithms may june 2016 answer key
Divide and Conquer in DAA concept. For B Tech CSE
3-Chapter Three - Divide and Conquer.ppt
Divide and conquer
Skiena algorithm 2007 lecture09 linear sorting
Divide and Conquer_Binary_Search_Merge_Sort.pptx
Divide and Conquer - Part 1
Sorting2
State Space Realizations_new.pptx
5.5 back tracking 02
lecture 1
Counting Sort Lowerbound
Merge sort and quick sort
Daa chapter 2

Lec22

  • 1. Why Sorting?  “When in doubt, sort” – one of the principles of algorithm design. Sorting used as a subroutine in many of the algorithms:  Searching in databases: we can do binary search on sorted data  A large number of computer graphics and computational geometry problems  Closest pair, element uniqueness 1
  • 2. Why Sorting? (2)  A large number of sorting algorithms are developed representing different algorithm design techniques.  A lower bound for sorting W(n log n) is used to prove lower bounds of other problems 2
  • 3. Sorting Algorithms so far  Insertion sort, selection sort  Worst-case running time Q(n2); in-place  Heap sort  Worst-case running time Q(n log n). 3
  • 4. Divide and Conquer  Divide-and-conquer method for algorithm design:  Divide: if the input size is too large to deal with in a straightforward manner, divide the problem into two or more disjoint subproblems  Conquer: use divide and conquer recursively to solve the subproblems  Combine: take the solutions to the subproblems and “merge” these solutions into a solution for the original problem 4
  • 5. Merge Sort Algorithm  Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove all the elements from S and put them into two sequences, S1 and S2 , each containing about half of the elements of S. (i.e. S1 contains the first n/2elements and S2 contains the remaining n/2elements).  Conquer: Sort sequences S1 and S2 using Merge Sort.  Combine: Put back the elements into S by merging the sorted sequences S1 and S2 into one sorted sequence 5
  • 6. Merge Sort: Algorithm Merge-Sort(A, p, r) if p < r then q(p+r)/2 Merge-Sort(A, p, q) Merge-Sort(A, q+1, r) Merge(A, p, q, r) Merge(A, p, q, r) Take the smallest of the two topmost elements of sequences A[p..q] and A[q+1..r] and put into the resulting sequence. Repeat this, until both sequences are empty. Copy the resulting sequence into A[p..r]. 6
  • 29. Merging Two Sequences (cont.) 29
  • 30. Merging Two Sequences (cont.) 30
  • 31. Merging Two Sequences (cont.) 31
  • 32. Merging Two Sequences (cont.) 32
  • 33. Merging Two Sequences (cont.) 33
  • 34. Merge Sort Revisited  To sort n numbers  if n=1 done!  recursively sort 2 lists of numbers n/2 and n/2 elements  merge 2 sorted lists in Q(n) time  Strategy  break problem into similar (smaller) subproblems  recursively solve subproblems  combine solutions to answer 34
  • 35. Recurrences  Running times of algorithms with Recursive calls can be described using recurrences  A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs  solving_trivial_problem if n  1 T (n)   num_pieces T (n / subproblem_size_factor)  dividing  combining if n  1  Example: Merge Sort  Q(1) if n  1 T (n)   2T (n / 2)  Q(n) if n  1 35
  • 36. Solving Recurrences  Repeated substitution method  Expanding the recurrence by substitution and noticing patterns  Substitution method  guessing the solutions  verifying the solution by the mathematical induction  Recursion-trees  Master method  templates for different classes of recurrences 36
  • 37. Repeated Substitution Method  Let’s find the running time of merge sort (let’s assume that n=2b, for some b).  1 if n  1 T (n)   2T (n / 2)  n if n  1 T (n)  2T  n / 2   n substitute  2  2T  n / 4   n / 2   n expand  22 T (n / 4)  2n substitute  22 (2T (n / 8)  n / 4)  2n expand  23 T (n / 8)  3n observe the pattern T (n)  2i T (n / 2i )  in  2lg n T (n / n)  n lg n  n  n lg n 37
  • 38. Repeated Substitution Method  The procedure is straightforward:  Substitute  Expand  Substitute  Expand …  Observe a pattern and write how your expression looks after the i-th substitution  Find out what the value of i (e.g., lgn) should be to get the base case of the recurrence (say T(1))  Insert the value of T(1) and the expression of i into your expression 38
  • 39. Java Implementation of Merge-Sort 39
  • 40. Java Implementation of MergeSort (cont.) public class ListMergeSort implements SortObject { public void sort(Sequence S, Comparator c) { int n = S.size(); if (n < 2) return; //sequence with 0/1 element is sorted. // divide Sequence S1 = (Sequence)S.newContainer(); // put the first half of S into S1 for (int i=1; i <= (n+1)/2; i++) { S1.insertLast(S.remove(S.first())); } Sequence S2 = (Sequence)S.newContainer(); // put the second half of S into S2 for (int i=1; i <= n/2; i++) { S2.insertLast(S.remove(S.first())); } sort(S1,c); // recur sort(S2,c); merge(S1,S2,c,S); // conquer 40 }
  • 41. Java Implementation of MergeSort (cont.) public void merge(Sequence S1, Sequence S2, Comparator c, Sequence S) { while(!S1.isEmpty() && !S2.isEmpty()) { if(c.isLessThanOrEqualTo(S1.first().element(), S2.first().element())) { // S1’s 1st elt <= S2’s 1st elt S.insertLast(S1.remove(S1.first())); }else { // S2’s 1st elt is the smaller one S.insertLast(S2.remove(S2.first())); } }if(S1.isEmpty()) { while(!S2.isEmpty()) { S.insertLast(S2.remove(S2.first())); } }if(S2.isEmpty()) { while(!S1.isEmpty()) { S.insertLast(S1.remove(S1.first())); }}} 41