SlideShare une entreprise Scribd logo
1  sur  6
Télécharger pour lire hors ligne
CS231 Algorithms                                                                 Handout # 15
Prof. Lyn Turbak                                                               October 13, 2001
Wellesley College

                                      Linear Sorting
Reading: CLRS Ch. 8, CLR Ch. 9

The Best Worst-Case Running Time for Comparison-Based Sorts
What is the minimum height of a binary tree with L leaves?

Given n distinct elements, how many different arrays can we make with these elements (with no
duplicates)? (For example, consider {1, 2, 3}.)




Can view comparison-based sorts as binary decision trees that process sets of arrays over n distinct
elements. For example:




Note that each leaf of the tree must hold a singleton array.

What is the minimum height of a decision tree for arrays of length n?




(Note that lg(n!) = n · lg(n) via Stirling’s equation or calculus (see CLRS Exercise 8.1-2/CLR
Exercise 9.1-2).)




                                                 1
Digression: Finding the Odd Nugget
Suppose you are given n nuggets, all of which have exactly the same weight, except for one “odd”
one, which may be slightly lighter or heavier than the rest. You are allowed to perform experiments
in which you compare the weights of two collections with k nuggets each: one collection may be <,
=, or > the other. How many experiments do you need to find the odd nugget?

   • Show via a counting argument that 3 experiments is not sufficient for finding the odd nugget
     when n = 14.




   • By case analysis on the first experiment, show via a counting argument that 3 experiments
     is not sufficient for finding the odd nugget when n = 13.




   • It is possible to find the odd nugget with 3 experiments when n = 12. This is left as an
     exercise for the reader.




                                                2
Linear Sorting Algorithms
All comparison-based sorting algorithms have worst-case time Ω(n · lg(n)).
Yet there are linear sorting algorithms – algorithms with worst-case time Θ(n). How can this be?
They’re not comparison-based! They take advantage of some feature of the input. E.g.:

   • The elements are integers in a restricted range, or easily convertible to integers in a restricted
     range (e.g., the letters of the alphabet).

   • The elements have a special probability distribution.


Integer Bucket Sort
Problem: sort n integers taken from the range 1..k.

Example: 4 2 6 3 2 4 2 6 1 2 4

Solution:

     Integer-Bucket-Sort(A,B,k)
          A is input array of length n
            containing keys in range [1..k].
          B is output array of length n.
       Count ← new array(k)
       for i ← 1 to k do
         Count[i] ← O
          Find count of each key in A.
       for j ← 1 to length[A] do
         Count[A[j]] <- Count[A[j]] + 1
          Read out result.
       j ← 1
       for p ← 1 to k do
         for q ← 1 to Count[p]
           B[j] ← p
           j ← j + 1


Can A be the same as B in this algorithm?




                                                  3
Counting Sort (a.k.a. Distribution Counting)
Integer bucket sort has a problem when there is satellite data. E.g., suppose (k, n) pairs a key k
with a satellite integer n:

          (4,1) (2,1) (6,1) (3,1) (2,2) (4,2) (2,3) (6,2) (1,1) (2,4) (4,3)

(Here, it happens that the satellite data for a particular key is sorted left to right, but that’s just
“coincidence”.)

     Counting-Sort(A,B,k)
          A is input array of length n
           containing keys in range [1..k].
          B is output array of length n.
       Count ← new array(k)
       for i ← 1 to k do
         Count[i] ← O
          Find count of each key in A.
       for j ← 1 to length[A] do
         Count[A[j]] <- Count[A[j]] + 1
          Find partial sums of key counts in A.
       for i ← 2 to k do
         Count[i] <- Count[i] + Count[i-1]
          Put elements in final position.
       for j ← length[A] downto 1 do
         B[Count[A[j]] <- A[j]
         Count[A[j]] <- Count[A[j]] - 1


Can A be the same as B in this algorithm?



How much time does Counting-Sort need?



How much space does Counting-Sort need?



Is Counting-Sort stable?



Would Counting-Sort be stable if the for j . . . loop counted up rather than down?




                                                  4
General Bucket Sort
Can’t use counting sort if keys aren’t integers in a given range. What if keys are real numbers in
a given range?
If keys are evenly distributed can use a general bucket sort:

     Step 1: Given n elements, make an array of n buckets, each of which contains an empty
         list.
     Step 2: Insert each element into the list of the bucket chosen by matching its key with
         the result of dividing the range into n equal-sized intervals
     Step3: Use any O(n2 ) sorting algorithm to sort the resulting lists in the buckets.
     Step 4: Read the elements from the lists in order back into the input array.

Example: Sorting 10 numbers between 0 and 1:

     .63
     .75
     .16
     .65
     .61
     .17
     .89
     .92
     .28
     .56




   • In the best case, there is one element per bucket, and running time is Θ(n).

   • In the worst case, all elements end up in same bucket and running time is Θ(n2 ).

   • Assuming evenly distributed keys, can use probability analysis to show average case running
     time is Θ(n). (See CLRS/CLR for details.)




                                                 5
Radix Sort
Problem: Suppose you have a machine that can perform a stable sort on the ith digit of a d-digit
number. How can you use the machine to sort a “pile” of n d-digit numbers?
Example:

        443
        124
        232
        431
        132
        123
        321
        211
        121
        441
        122
        442

To avoid lots of intermediate piles, process digits right-to-left, not left-to-right!

        Radix-Sort(A, d)
          for i ← 1 to d do
            Stable-Sort(A, i)        use digit i for comparison

Any stable sort will do. Counting-Sort is a good candidate since it’s Θ(n + k).
What is the running time for sorting n d-digit numbers, where digits are in the range [1..k]?




Note:

   • If d is constant and k = O(n), then running time of Radix-Sort is Θ(n).

   • In many applications, d = logk (n), so the sort ends up being Θ(n · logk (n)) for a given k.




                                                    6

Contenu connexe

Tendances

Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O NotationJawad Khan
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
Big o notation
Big o notationBig o notation
Big o notationkeb97
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. Rajdeep Chatterjee
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
Algorithms 101 for Data Scientists
Algorithms 101 for Data ScientistsAlgorithms 101 for Data Scientists
Algorithms 101 for Data ScientistsChristopher Conlan
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...Dharmendra Prasad
 

Tendances (20)

Data Structures- Hashing
Data Structures- Hashing Data Structures- Hashing
Data Structures- Hashing
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Algorithms Question bank
Algorithms Question bankAlgorithms Question bank
Algorithms Question bank
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Lec5
Lec5Lec5
Lec5
 
Sorting
SortingSorting
Sorting
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
Big o notation
Big o notationBig o notation
Big o notation
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
Algorithms 101 for Data Scientists
Algorithms 101 for Data ScientistsAlgorithms 101 for Data Scientists
Algorithms 101 for Data Scientists
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Unit 3
Unit 3Unit 3
Unit 3
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 

En vedette

Berkeley Data Analytics Stack Genel Bakış
Berkeley Data Analytics Stack Genel Bakış Berkeley Data Analytics Stack Genel Bakış
Berkeley Data Analytics Stack Genel Bakış Veysel Taşcıoğlu
 
Higher Education: Is Web Self-Service Right for You
Higher Education: Is Web Self-Service Right for YouHigher Education: Is Web Self-Service Right for You
Higher Education: Is Web Self-Service Right for YouIntelliResponse Systems Inc.
 
Branding highlights2002b
Branding highlights2002bBranding highlights2002b
Branding highlights2002bgdamon1010
 
Apec women and the economy 2011 mo - australia
Apec women and the economy 2011   mo - australiaApec women and the economy 2011   mo - australia
Apec women and the economy 2011 mo - australiaotoolem
 
Photoshop
PhotoshopPhotoshop
PhotoshoptoSterr
 
An example of cms - wordpress
An example of cms - wordpressAn example of cms - wordpress
An example of cms - wordpressEunus Hosen
 

En vedette (11)

481 bb0091
481 bb0091481 bb0091
481 bb0091
 
Berkeley Data Analytics Stack Genel Bakış
Berkeley Data Analytics Stack Genel Bakış Berkeley Data Analytics Stack Genel Bakış
Berkeley Data Analytics Stack Genel Bakış
 
Big data
Big dataBig data
Big data
 
Higher Education: Is Web Self-Service Right for You
Higher Education: Is Web Self-Service Right for YouHigher Education: Is Web Self-Service Right for You
Higher Education: Is Web Self-Service Right for You
 
Branding highlights2002b
Branding highlights2002bBranding highlights2002b
Branding highlights2002b
 
Apec women and the economy 2011 mo - australia
Apec women and the economy 2011   mo - australiaApec women and the economy 2011   mo - australia
Apec women and the economy 2011 mo - australia
 
Photoshop
PhotoshopPhotoshop
Photoshop
 
Clojure 1a
Clojure 1aClojure 1a
Clojure 1a
 
Exp int toc
Exp int tocExp int toc
Exp int toc
 
An example of cms - wordpress
An example of cms - wordpressAn example of cms - wordpress
An example of cms - wordpress
 
Colab42
Colab42Colab42
Colab42
 

Similaire à CS231 Algorithms Handout #15 Linear Sorting

lecture 9
lecture 9lecture 9
lecture 9sajinsc
 
lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxjainaaru59
 
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
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitationschidabdu
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingSam Light
 

Similaire à CS231 Algorithms Handout #15 Linear Sorting (20)

sorting
sortingsorting
sorting
 
lecture 9
lecture 9lecture 9
lecture 9
 
lecture 10
lecture 10lecture 10
lecture 10
 
Unit 3
Unit 3Unit 3
Unit 3
 
Sorting2
Sorting2Sorting2
Sorting2
 
Algorithm Exam Help
Algorithm Exam HelpAlgorithm Exam Help
Algorithm Exam Help
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Q
QQ
Q
 
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
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Encoding survey
Encoding surveyEncoding survey
Encoding survey
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 

Plus de Krishna Chaytaniah

Plus de Krishna Chaytaniah (8)

Istqb ctfl syll 2011
Istqb ctfl syll 2011Istqb ctfl syll 2011
Istqb ctfl syll 2011
 
Introduction to writing algorithms
Introduction to writing algorithmsIntroduction to writing algorithms
Introduction to writing algorithms
 
Design and analysis of computer algorithms (dave mount, 1999)
Design and analysis of computer algorithms (dave mount, 1999)Design and analysis of computer algorithms (dave mount, 1999)
Design and analysis of computer algorithms (dave mount, 1999)
 
Design and analysis of computer algorithms
Design and analysis of computer algorithmsDesign and analysis of computer algorithms
Design and analysis of computer algorithms
 
Writing algorithms
Writing algorithmsWriting algorithms
Writing algorithms
 
Node js
Node jsNode js
Node js
 
Oracle apps
Oracle appsOracle apps
Oracle apps
 
Salesforce content best_practices_rollout_webinar_deck
Salesforce content best_practices_rollout_webinar_deckSalesforce content best_practices_rollout_webinar_deck
Salesforce content best_practices_rollout_webinar_deck
 

CS231 Algorithms Handout #15 Linear Sorting

  • 1. CS231 Algorithms Handout # 15 Prof. Lyn Turbak October 13, 2001 Wellesley College Linear Sorting Reading: CLRS Ch. 8, CLR Ch. 9 The Best Worst-Case Running Time for Comparison-Based Sorts What is the minimum height of a binary tree with L leaves? Given n distinct elements, how many different arrays can we make with these elements (with no duplicates)? (For example, consider {1, 2, 3}.) Can view comparison-based sorts as binary decision trees that process sets of arrays over n distinct elements. For example: Note that each leaf of the tree must hold a singleton array. What is the minimum height of a decision tree for arrays of length n? (Note that lg(n!) = n · lg(n) via Stirling’s equation or calculus (see CLRS Exercise 8.1-2/CLR Exercise 9.1-2).) 1
  • 2. Digression: Finding the Odd Nugget Suppose you are given n nuggets, all of which have exactly the same weight, except for one “odd” one, which may be slightly lighter or heavier than the rest. You are allowed to perform experiments in which you compare the weights of two collections with k nuggets each: one collection may be <, =, or > the other. How many experiments do you need to find the odd nugget? • Show via a counting argument that 3 experiments is not sufficient for finding the odd nugget when n = 14. • By case analysis on the first experiment, show via a counting argument that 3 experiments is not sufficient for finding the odd nugget when n = 13. • It is possible to find the odd nugget with 3 experiments when n = 12. This is left as an exercise for the reader. 2
  • 3. Linear Sorting Algorithms All comparison-based sorting algorithms have worst-case time Ω(n · lg(n)). Yet there are linear sorting algorithms – algorithms with worst-case time Θ(n). How can this be? They’re not comparison-based! They take advantage of some feature of the input. E.g.: • The elements are integers in a restricted range, or easily convertible to integers in a restricted range (e.g., the letters of the alphabet). • The elements have a special probability distribution. Integer Bucket Sort Problem: sort n integers taken from the range 1..k. Example: 4 2 6 3 2 4 2 6 1 2 4 Solution: Integer-Bucket-Sort(A,B,k) A is input array of length n containing keys in range [1..k]. B is output array of length n. Count ← new array(k) for i ← 1 to k do Count[i] ← O Find count of each key in A. for j ← 1 to length[A] do Count[A[j]] <- Count[A[j]] + 1 Read out result. j ← 1 for p ← 1 to k do for q ← 1 to Count[p] B[j] ← p j ← j + 1 Can A be the same as B in this algorithm? 3
  • 4. Counting Sort (a.k.a. Distribution Counting) Integer bucket sort has a problem when there is satellite data. E.g., suppose (k, n) pairs a key k with a satellite integer n: (4,1) (2,1) (6,1) (3,1) (2,2) (4,2) (2,3) (6,2) (1,1) (2,4) (4,3) (Here, it happens that the satellite data for a particular key is sorted left to right, but that’s just “coincidence”.) Counting-Sort(A,B,k) A is input array of length n containing keys in range [1..k]. B is output array of length n. Count ← new array(k) for i ← 1 to k do Count[i] ← O Find count of each key in A. for j ← 1 to length[A] do Count[A[j]] <- Count[A[j]] + 1 Find partial sums of key counts in A. for i ← 2 to k do Count[i] <- Count[i] + Count[i-1] Put elements in final position. for j ← length[A] downto 1 do B[Count[A[j]] <- A[j] Count[A[j]] <- Count[A[j]] - 1 Can A be the same as B in this algorithm? How much time does Counting-Sort need? How much space does Counting-Sort need? Is Counting-Sort stable? Would Counting-Sort be stable if the for j . . . loop counted up rather than down? 4
  • 5. General Bucket Sort Can’t use counting sort if keys aren’t integers in a given range. What if keys are real numbers in a given range? If keys are evenly distributed can use a general bucket sort: Step 1: Given n elements, make an array of n buckets, each of which contains an empty list. Step 2: Insert each element into the list of the bucket chosen by matching its key with the result of dividing the range into n equal-sized intervals Step3: Use any O(n2 ) sorting algorithm to sort the resulting lists in the buckets. Step 4: Read the elements from the lists in order back into the input array. Example: Sorting 10 numbers between 0 and 1: .63 .75 .16 .65 .61 .17 .89 .92 .28 .56 • In the best case, there is one element per bucket, and running time is Θ(n). • In the worst case, all elements end up in same bucket and running time is Θ(n2 ). • Assuming evenly distributed keys, can use probability analysis to show average case running time is Θ(n). (See CLRS/CLR for details.) 5
  • 6. Radix Sort Problem: Suppose you have a machine that can perform a stable sort on the ith digit of a d-digit number. How can you use the machine to sort a “pile” of n d-digit numbers? Example: 443 124 232 431 132 123 321 211 121 441 122 442 To avoid lots of intermediate piles, process digits right-to-left, not left-to-right! Radix-Sort(A, d) for i ← 1 to d do Stable-Sort(A, i) use digit i for comparison Any stable sort will do. Counting-Sort is a good candidate since it’s Θ(n + k). What is the running time for sorting n d-digit numbers, where digits are in the range [1..k]? Note: • If d is constant and k = O(n), then running time of Radix-Sort is Θ(n). • In many applications, d = logk (n), so the sort ends up being Θ(n · logk (n)) for a given k. 6