SlideShare a Scribd company logo
Data Structures and Algorithms Sorting Bin Sorts PLSD210
Lecture 9 -  Key Points Quicksort Use for good overall performance where time is not a constraint Heap Sort Slower than quick sort, but guaranteed  O(n  log  n) Use for real-time systems where time is critical Functions as data types Argument of a function can be a function Enables flexible general purpose classes Enables table driven code
Sorting We now know several sorting algorithms Insertion  O(n 2 ) Bubble  O(n 2 ) Heap  O(n  log  n)  Guaranteed Quick  O(n  log  n)  Most of the time! Can we do any better?
Sorting -  Better than  O(n  log  n)  ? If all we know about the keys is an ordering rule No! However, If we can compute an address from the key (in constant time)  then  bin sort  algorithms can provide better performance
Sorting -  Bin Sort Assume All the keys lie in a small, fixed range eg  integers 0-99 characters ‘A’-’z’, ‘0’-’9’ There is at most one item with each value of the key Bin sort Allocate a bin for each value of the key Usually an entry in an array For each item,  Extract the key Compute it’s bin number Place it in the bin Finished!
Sorting -  Bin Sort: Analysis All the keys lie in a small, fixed range There are  m  possible key values There is at most one item with each value of the key Bin sort Allocate a bin for each value of the key  O(m) Usually an entry in an array For each item,  n times Extract the key  O( 1 ) Compute it’s bin number  O( 1 ) Place it in the bin  O( 1 )  x  n    O(n)  Finished!  O(n) + O(m) =  O(n+m)  =  O(n)  if  n >> m Key  condition
Sorting -  Bin Sort: Caveat Key Range All the keys lie in a small, fixed range There are  m  possible key values If this condition is not met,  eg  m >> n, then bin sort is  O(m)  Example Key is a 32-bit integer,   m  = 2 32 Clearly, this isn’t a good way to sort a few thousand integers Also,  we may not have enough space for bins! Bin sort trades  space  for  speed ! There’s no free lunch!
Sorting -  Bin Sort with duplicates There is at most one item with each value of the key Bin sort Allocate a bin for each value of the key  O(m) Usually an entry in an array Array of list heads For each item,  n times Extract the key  O( 1 ) Compute it’s bin number  O( 1 ) Add it to a list  O( 1 )  x  n    O(n) Join the lists   O(m) Finished!  O(n) + O(m) =  O(n+m)  =  O(n)  if  n >> m Relax?
Sorting -  Generalised Bin Sort  Radix sort Bin sort in phases Example Phase 1 - Sort by least significant digit 36 9 0 25 1 49 64 16 81 4 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49
Sorting -  Generalised Bin Sort  Radix sort  - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0
Sorting -  Generalised Bin Sort  Radix sort  - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0 1 Be careful to add  after  anything in the bin already!
Sorting -  Generalised Bin Sort  Radix sort  - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0 1 81
Sorting -  Generalised Bin Sort  Radix sort  - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0 1 81 64
Sorting -  Generalised Bin Sort  Radix sort  - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0 1 4 81 64
Sorting -  Generalised Bin Sort  Radix sort  - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 1 2 3 4 5 6 7 8 9 81 64 25 36 16 49 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 0 1 4 9 Note that the 0 bin had to be quite large!
Sorting -  Generalised Bin Sort  Radix sort  - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 1 2 3 4 5 6 7 8 9 81 64 25 36 16 49 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 0 1 4 9 How much  space is needed in each phase? n  items m  bins
Sorting -  Generalised Bin Sort  Radix sort  - Analysis Phase 1 - Sort by least significant digit Create  m  bins O(m) Allocate  n  items O(n) Phase 2  Create  m  bins  O(m) Allocate  n  items O(n) Final Link  m  bins  O(m) All steps in sequence, so add Total  O(3m+2n)   O(m+n)     O(n)  for   m<<n
Sorting -  Radix Sort - Analysis Radix sort  - General Base (or  radix ) in each phase can be anything suitable Integers Base 10, 16, 100, … Bases don’t have to be the same Still   O(n)   if   n >> s i   for  all  i struct date {   int day;  /* 1 .. 31 */   int month; /* 1 .. 12 */   int year;  /* 0 .. 99 */ } Phase 1 -   s 1 = 31 bins Phase 2 -   s 2 = 12 bins Phase 3 -  s 3 = 100 bins
Radix Sort - Analysis Generalised Radix Sort Algorithm radixsort( A, n ) { for(i=0;i<k;i++) { for(j=0;j<s[i];j++) bin[j] = EMPTY;  for(j=0;j<n;j++) { move A[i]  to the end of bin[A[i]->fi]  } for(j=0;j<s[i];j++)  concat bin[j] onto the end of A;  } } O( s i  ) O( n   ) O( s i  ) For each of  k  radices
Radix Sort - Analysis Generalised Radix Sort Algorithm radixsort( A, n ) { for(i=0;i<k;i++) { for(j=0;j<s[i];j++) bin[j] = EMPTY;  for(j=0;j<n;j++) { move A[i]  to the end of bin[A[i]->fi]  } for(j=0;j<s[i];j++)  concat bin[j] onto the end of A;  } } O( s i  ) O( n   ) O( s i  ) Clear the  s i  bins for the  i th  radix
Radix Sort - Analysis Generalised Radix Sort Algorithm radixsort( A, n ) { for(i=0;i<k;i++) { for(j=0;j<s[i];j++) bin[j] = EMPTY;  for(j=0;j<n;j++) { move A[i]  to the end of bin[A[i]->fi]  } for(j=0;j<s[i];j++)  concat bin[j] onto the end of A;  } } O( s i  ) O( n   ) O( s i  ) Move element  A[i] to the end of the bin addressed by the  i th  field of  A[i]
Radix Sort - Analysis Generalised Radix Sort Algorithm radixsort( A, n ) { for(i=0;i<k;i++) { for(j=0;j<s[i];j++) bin[j] = EMPTY;  for(j=0;j<n;j++) { move A[i]  to the end of bin[A[i]->fi]  } for(j=0;j<s[i];j++)  concat bin[j] onto the end of A;  } } O( s i  ) O( n   ) O( s i  ) Concatenate  s i   bins into one list again
Radix Sort -  Analysis Total k   iterations,  2s i  + n   for each one As long as  k  is constant In general,  if the keys are in   (0, b k - 1 ) Keys are  k -digit base- b  numbers s i  = b  for all  k Complexity  O(n+kb)  =  O(n)  O( s i  + n )  =  O(kn +    s i  ) =  O(n +      s i   ) i= 1 i= 1 i= 1 k k k
Radix Sort -  Analysis Any  set of keys can be mapped to  (0, b k - 1 ) So we can always obtain  O(n)  sorting? If  k  is constant,  yes
Radix Sort -  Analysis But,  if   k   is allowed to increase with   n eg  it takes  log b n  base- b  digits to represent  n so  we have  k =  log  n,  s i  =  2  (say) Radix sort is no better than quicksort  O(  2  + n )  =  O(n  log  n +     2  ) =  O(n  log  n +  2 log  n   ) =  O(n  log  n   ) i= 1 i= 1 log  n log  n
Radix Sort -  Analysis Radix sort is no better than quicksort Another way of looking at this: We can keep  k  constant as  n  increases if we allow duplicate keys keys are in   (0, b k  ),  b k  < n but  if the keys must be unique, then   k   must increase with   n For  O(n)  performance,  the keys must lie in a restricted range
Radix Sort - Realities Radix sort uses a lot of memory n s i   locations for each phase In practice, this makes it difficult to achieve  O(n) performance Cost of memory management outweighs benefits Challenge: Design a general radix sort which runs faster than the library  qsort  on the SGIs! Hint: You will need to be very efficient about memory allocation and de-allocation!
Lecture 9 -  Key Points Bin Sorts If  a function exists which can convert the key to an address ( ie  a small integer) and  the number of addresses (= number of bins) is not too large then  we can obtain  O(n)  sorting …  but remember it’s actually   O(n + m) Number of bins,  m , must be  constant and small

More Related Content

PPT
Time complexity
PPTX
Asymptotic notations(Big O, Omega, Theta )
PPTX
Lec 5 asymptotic notations and recurrences
PPT
Asymptotic Notation and Complexity
PPT
Basics & asymptotic notations
PDF
Time complexity (linear search vs binary search)
PDF
Quant2a
Time complexity
Asymptotic notations(Big O, Omega, Theta )
Lec 5 asymptotic notations and recurrences
Asymptotic Notation and Complexity
Basics & asymptotic notations
Time complexity (linear search vs binary search)
Quant2a

What's hot (20)

PPT
Algorithm: Quick-Sort
PDF
Asymptotic notation
PPTX
Predication
PPT
Asymptotic Notation
PPT
Asymptotic analysis
PPTX
Analysis of algorithn class 3
PPTX
SORTTING IN LINEAR TIME - Radix Sort
PDF
Asymptotic Notation
PPT
Asymptotic notations
PPTX
Automata theory - Push Down Automata (PDA)
PDF
Skiena algorithm 2007 lecture07 heapsort priority queues
PPT
Lect11 Sorting
PDF
201801 CSE240 Lecture 18
PPT
Cs1311lecture23wdl
PPT
3.6 radix sort
PPT
Algorithum Analysis
PPTX
Asymptotic Notations
PPT
Asymptotic notation
Algorithm: Quick-Sort
Asymptotic notation
Predication
Asymptotic Notation
Asymptotic analysis
Analysis of algorithn class 3
SORTTING IN LINEAR TIME - Radix Sort
Asymptotic Notation
Asymptotic notations
Automata theory - Push Down Automata (PDA)
Skiena algorithm 2007 lecture07 heapsort priority queues
Lect11 Sorting
201801 CSE240 Lecture 18
Cs1311lecture23wdl
3.6 radix sort
Algorithum Analysis
Asymptotic Notations
Asymptotic notation
Ad

Similar to Binsort (20)

PPT
RadixSort.ppt
PPT
3.5 merge sort
PPTX
Sorting ppt
PPTX
Data structure using c module 3
PPTX
Address calculation-sort
PPTX
Sorting2
PDF
Linear sort
PPTX
Radix Sort
PPTX
Analysis of algorithms
PPTX
Radix sorting
PPT
Counting Sort Lowerbound
PPT
Lecture 21 Survey of Sorting.ppt ggggggggggg
PPT
lecture 10
PPT
Algorithms with-java-advanced-1.0
PPTX
Radix Sort
PPT
lecture 9
PPT
Data Structure (MC501)
PPTX
ppt2- sorting data in design and anlysis.pptx
PDF
Sorting
RadixSort.ppt
3.5 merge sort
Sorting ppt
Data structure using c module 3
Address calculation-sort
Sorting2
Linear sort
Radix Sort
Analysis of algorithms
Radix sorting
Counting Sort Lowerbound
Lecture 21 Survey of Sorting.ppt ggggggggggg
lecture 10
Algorithms with-java-advanced-1.0
Radix Sort
lecture 9
Data Structure (MC501)
ppt2- sorting data in design and anlysis.pptx
Sorting
Ad

Binsort

  • 1. Data Structures and Algorithms Sorting Bin Sorts PLSD210
  • 2. Lecture 9 - Key Points Quicksort Use for good overall performance where time is not a constraint Heap Sort Slower than quick sort, but guaranteed O(n log n) Use for real-time systems where time is critical Functions as data types Argument of a function can be a function Enables flexible general purpose classes Enables table driven code
  • 3. Sorting We now know several sorting algorithms Insertion O(n 2 ) Bubble O(n 2 ) Heap O(n log n) Guaranteed Quick O(n log n) Most of the time! Can we do any better?
  • 4. Sorting - Better than O(n log n) ? If all we know about the keys is an ordering rule No! However, If we can compute an address from the key (in constant time) then bin sort algorithms can provide better performance
  • 5. Sorting - Bin Sort Assume All the keys lie in a small, fixed range eg integers 0-99 characters ‘A’-’z’, ‘0’-’9’ There is at most one item with each value of the key Bin sort Allocate a bin for each value of the key Usually an entry in an array For each item, Extract the key Compute it’s bin number Place it in the bin Finished!
  • 6. Sorting - Bin Sort: Analysis All the keys lie in a small, fixed range There are m possible key values There is at most one item with each value of the key Bin sort Allocate a bin for each value of the key O(m) Usually an entry in an array For each item, n times Extract the key O( 1 ) Compute it’s bin number O( 1 ) Place it in the bin O( 1 ) x n  O(n) Finished! O(n) + O(m) = O(n+m) = O(n) if n >> m Key condition
  • 7. Sorting - Bin Sort: Caveat Key Range All the keys lie in a small, fixed range There are m possible key values If this condition is not met, eg m >> n, then bin sort is O(m) Example Key is a 32-bit integer, m = 2 32 Clearly, this isn’t a good way to sort a few thousand integers Also, we may not have enough space for bins! Bin sort trades space for speed ! There’s no free lunch!
  • 8. Sorting - Bin Sort with duplicates There is at most one item with each value of the key Bin sort Allocate a bin for each value of the key O(m) Usually an entry in an array Array of list heads For each item, n times Extract the key O( 1 ) Compute it’s bin number O( 1 ) Add it to a list O( 1 ) x n  O(n) Join the lists O(m) Finished! O(n) + O(m) = O(n+m) = O(n) if n >> m Relax?
  • 9. Sorting - Generalised Bin Sort Radix sort Bin sort in phases Example Phase 1 - Sort by least significant digit 36 9 0 25 1 49 64 16 81 4 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49
  • 10. Sorting - Generalised Bin Sort Radix sort - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0
  • 11. Sorting - Generalised Bin Sort Radix sort - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0 1 Be careful to add after anything in the bin already!
  • 12. Sorting - Generalised Bin Sort Radix sort - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0 1 81
  • 13. Sorting - Generalised Bin Sort Radix sort - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0 1 81 64
  • 14. Sorting - Generalised Bin Sort Radix sort - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 1 2 3 4 5 6 7 8 9 0 1 4 81 64
  • 15. Sorting - Generalised Bin Sort Radix sort - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 1 2 3 4 5 6 7 8 9 81 64 25 36 16 49 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 0 1 4 9 Note that the 0 bin had to be quite large!
  • 16. Sorting - Generalised Bin Sort Radix sort - Bin sort in phases Phase 1 - Sort by least significant digit Phase 2 - Sort by most significant digit 1 2 3 4 5 6 7 8 9 81 64 25 36 16 49 0 1 2 3 4 5 6 7 8 9 0 1 81 64 4 25 36 16 9 49 0 0 1 4 9 How much space is needed in each phase? n items m bins
  • 17. Sorting - Generalised Bin Sort Radix sort - Analysis Phase 1 - Sort by least significant digit Create m bins O(m) Allocate n items O(n) Phase 2 Create m bins O(m) Allocate n items O(n) Final Link m bins O(m) All steps in sequence, so add Total O(3m+2n)  O(m+n)  O(n) for m<<n
  • 18. Sorting - Radix Sort - Analysis Radix sort - General Base (or radix ) in each phase can be anything suitable Integers Base 10, 16, 100, … Bases don’t have to be the same Still O(n) if n >> s i for all i struct date { int day; /* 1 .. 31 */ int month; /* 1 .. 12 */ int year; /* 0 .. 99 */ } Phase 1 - s 1 = 31 bins Phase 2 - s 2 = 12 bins Phase 3 - s 3 = 100 bins
  • 19. Radix Sort - Analysis Generalised Radix Sort Algorithm radixsort( A, n ) { for(i=0;i<k;i++) { for(j=0;j<s[i];j++) bin[j] = EMPTY; for(j=0;j<n;j++) { move A[i] to the end of bin[A[i]->fi] } for(j=0;j<s[i];j++) concat bin[j] onto the end of A; } } O( s i ) O( n ) O( s i ) For each of k radices
  • 20. Radix Sort - Analysis Generalised Radix Sort Algorithm radixsort( A, n ) { for(i=0;i<k;i++) { for(j=0;j<s[i];j++) bin[j] = EMPTY; for(j=0;j<n;j++) { move A[i] to the end of bin[A[i]->fi] } for(j=0;j<s[i];j++) concat bin[j] onto the end of A; } } O( s i ) O( n ) O( s i ) Clear the s i bins for the i th radix
  • 21. Radix Sort - Analysis Generalised Radix Sort Algorithm radixsort( A, n ) { for(i=0;i<k;i++) { for(j=0;j<s[i];j++) bin[j] = EMPTY; for(j=0;j<n;j++) { move A[i] to the end of bin[A[i]->fi] } for(j=0;j<s[i];j++) concat bin[j] onto the end of A; } } O( s i ) O( n ) O( s i ) Move element A[i] to the end of the bin addressed by the i th field of A[i]
  • 22. Radix Sort - Analysis Generalised Radix Sort Algorithm radixsort( A, n ) { for(i=0;i<k;i++) { for(j=0;j<s[i];j++) bin[j] = EMPTY; for(j=0;j<n;j++) { move A[i] to the end of bin[A[i]->fi] } for(j=0;j<s[i];j++) concat bin[j] onto the end of A; } } O( s i ) O( n ) O( s i ) Concatenate s i bins into one list again
  • 23. Radix Sort - Analysis Total k iterations, 2s i + n for each one As long as k is constant In general, if the keys are in (0, b k - 1 ) Keys are k -digit base- b numbers s i = b for all k Complexity O(n+kb) = O(n)  O( s i + n ) = O(kn +  s i ) = O(n +  s i ) i= 1 i= 1 i= 1 k k k
  • 24. Radix Sort - Analysis Any set of keys can be mapped to (0, b k - 1 ) So we can always obtain O(n) sorting? If k is constant, yes
  • 25. Radix Sort - Analysis But, if k is allowed to increase with n eg it takes log b n base- b digits to represent n so we have k = log n, s i = 2 (say) Radix sort is no better than quicksort  O( 2 + n ) = O(n log n +  2 ) = O(n log n + 2 log n ) = O(n log n ) i= 1 i= 1 log n log n
  • 26. Radix Sort - Analysis Radix sort is no better than quicksort Another way of looking at this: We can keep k constant as n increases if we allow duplicate keys keys are in (0, b k ), b k < n but if the keys must be unique, then k must increase with n For O(n) performance, the keys must lie in a restricted range
  • 27. Radix Sort - Realities Radix sort uses a lot of memory n s i locations for each phase In practice, this makes it difficult to achieve O(n) performance Cost of memory management outweighs benefits Challenge: Design a general radix sort which runs faster than the library qsort on the SGIs! Hint: You will need to be very efficient about memory allocation and de-allocation!
  • 28. Lecture 9 - Key Points Bin Sorts If a function exists which can convert the key to an address ( ie a small integer) and the number of addresses (= number of bins) is not too large then we can obtain O(n) sorting … but remember it’s actually O(n + m) Number of bins, m , must be constant and small