SlideShare une entreprise Scribd logo
1  sur  41
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

Contenu connexe

Tendances (20)

Lec5
Lec5Lec5
Lec5
 
Lec10
Lec10Lec10
Lec10
 
Lec1
Lec1Lec1
Lec1
 
Digital Signal Processing Assignment Help
Digital Signal Processing Assignment HelpDigital Signal Processing Assignment Help
Digital Signal Processing Assignment Help
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Signal Processing Assignment Help
Signal Processing Assignment HelpSignal Processing Assignment Help
Signal Processing Assignment Help
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Signal Processing Assignment Help
Signal Processing Assignment HelpSignal Processing Assignment Help
Signal Processing Assignment Help
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Big o
Big oBig o
Big o
 
Signals and systems assignment help
Signals and systems assignment helpSignals and systems assignment help
Signals and systems assignment help
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Signals Processing Homework Help
Signals Processing Homework HelpSignals Processing Homework Help
Signals Processing Homework Help
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 

En vedette (20)

Lec19
Lec19Lec19
Lec19
 
Lec20
Lec20Lec20
Lec20
 
Lec21
Lec21Lec21
Lec21
 
I 7
I 7I 7
I 7
 
Lec4
Lec4Lec4
Lec4
 
Lec3
Lec3Lec3
Lec3
 
10강 기업교육론 20110504
10강 기업교육론 2011050410강 기업교육론 20110504
10강 기업교육론 20110504
 
A New Wave of Tobacco Products, April 2011 Update
A New Wave of Tobacco Products, April 2011 UpdateA New Wave of Tobacco Products, April 2011 Update
A New Wave of Tobacco Products, April 2011 Update
 
Observations made by the group (Edited)
Observations made by the group (Edited)Observations made by the group (Edited)
Observations made by the group (Edited)
 
C:\Documents And Settings\Lakshmi Menon\My Documents\Twinkling Stars
C:\Documents And Settings\Lakshmi Menon\My Documents\Twinkling StarsC:\Documents And Settings\Lakshmi Menon\My Documents\Twinkling Stars
C:\Documents And Settings\Lakshmi Menon\My Documents\Twinkling Stars
 
Shanxi china
Shanxi  chinaShanxi  china
Shanxi china
 
Plagesdumonde
PlagesdumondePlagesdumonde
Plagesdumonde
 
ChapmanCreative Porfolio
ChapmanCreative PorfolioChapmanCreative Porfolio
ChapmanCreative Porfolio
 
Kpe profile
Kpe profileKpe profile
Kpe profile
 
Maine: Basic Tobacco Information, July 2010
Maine: Basic Tobacco Information, July 2010Maine: Basic Tobacco Information, July 2010
Maine: Basic Tobacco Information, July 2010
 
Прориси
ПрорисиПрориси
Прориси
 
Printing industry
Printing industryPrinting industry
Printing industry
 
The bestof
The bestofThe bestof
The bestof
 
Sti case study chapter 2 3
Sti case study chapter 2 3Sti case study chapter 2 3
Sti case study chapter 2 3
 
Natural Depths
Natural DepthsNatural Depths
Natural Depths
 

Similaire à Lec22

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptxSajalFayyaz
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
State Space Realizations_new.pptx
State Space Realizations_new.pptxState Space Realizations_new.pptx
State Space Realizations_new.pptxMohdNajibAliMokhtar
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbounddespicable me
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdfSrinivasaReddyPolamR
 
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTSBASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTSjainyshah20
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013Gary Short
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 

Similaire à Lec22 (20)

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Sorting2
Sorting2Sorting2
Sorting2
 
State Space Realizations_new.pptx
State Space Realizations_new.pptxState Space Realizations_new.pptx
State Space Realizations_new.pptx
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
lecture 1
lecture 1lecture 1
lecture 1
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
 
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTSBASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 

Plus de Anjneya Varshney (7)

Lec16
Lec16Lec16
Lec16
 
Lec12
Lec12Lec12
Lec12
 
Lec11
Lec11Lec11
Lec11
 
Lec8
Lec8Lec8
Lec8
 
Lec7
Lec7Lec7
Lec7
 
Lec6
Lec6Lec6
Lec6
 
Lec13
Lec13Lec13
Lec13
 

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