SlideShare a Scribd company logo
More Sorting

 radix sort
 bucket sort
 in-place sorting
 how fast can we sort?




                          1
Radix Sort
    Unlike other sorting methods, radix sort considers the
     structure of the keys
    Assume keys are represented in a base M number
     system (M = radix), i.e., if M = 2, the keys are
     represented in binary




Sorting is done by comparing bits in the same position

Extension to keys that are alphanumeric strings
                                                              2
Radix Exchange Sort
Examine bits from left to right:
1. Sort array with respect to leftmost bit


2. Partition array




3.Recursion
recursively sort top subarray, ignoring leftmost bit
recursively sort bottom subarray, ignoring leftmost bit
Time to sort n b-bit numbers:   O(b n)
                                                          3
Radix Exchange Sort
How do we do the sort from the previous page? Same idea as
partition in Quicksort.
                 repeat
                        scan top-down to find key starting with 1;
                        scan bottom-up to find key starting with 0
                        exchange keys;
                 until scan indices cross;




                                                            4
Radix Exchange Sort




                      5
Radix Exchange Sort vs. Quicksort
Similarities
       both partition array
       both recursively sort sub-arrays
Differences
       Method of partitioning
              radix exchange divides array based on greater
than or                less than 2b-1
              quicksort partitions based on greater  than or
less than              some element of the array
       Time complexity
              Radix exchange            O (bn)
              Quicksort average case O (n log n)


                                                           6
Straight Radix Sort
Examines bits from right to left
for k := 0 to b-1
       sort the array in a stable way,
       looking only at bit k




Note order of these bits after sort.
                                         7
What is “sort in a stable way”!!!
In a stable sort, the initial relative order of equal keys is
unchanged. For example, observe the first step of the sort
from the previous page:




Note that the relative order of those keys ending with 0
is unchanged, and the same is true for elements
ending in 1
                                                                8
The Algorithm is Correct (right?)
We show that any two keys are in the correct relative order
at the end of the algorithm

Given two keys, let k be the leftmost bit-position where
they differ




 At step k the two keys are put in the correct relative
 order
 Because of stability, the successive steps do not
 change the relative order of the two keys
                                                           9
For Instance,
Consider a sort on an array with these two keys:




                                                   10
Radix sorting applied to decimal numbers




                                     11
Straight Radix Sort Time Complexity
for k = 0 to b - 1
       sort the array in a stable way, looking only at bit k


Suppose we can perform the stable sort above in O(n)
time. The total time complexity would be
      O(bn)
As you might have guessed, we can perform a stable
sort based on the keys’ kth digit in O(n) time.

The method, you ask? Why it’s Bucket Sort, of course.


                                                               12
Bucket Sort

 BASICS:
     n numbers
     Each number  {1, 2, 3, ...
     m}
     Stable
     Time: O (n + m)
 For example, m = 3 and our array
 is:
(note that there are two “2”s and two “1”s)

                  First, we create M “buckets”

                                                 13
Bucket Sort
Each element of the array is put in one of the m “buckets”




                                                             14
Bucket Sort


Now, pull the elements
from the buckets into the
array




At last, the sorted array
(sorted in a stable way):
                            15
In-Place Sorting
   A sorting algorithm is said to be in-place if
      it uses no auxiliary data structures (however, O(1)
      auxiliary variables are allowed)
     it updates the input sequence only by means of
      operations replaceElement and swapElements
   Which sorting algorithms seen can be made to
    work in place?




                                                             16
Lower Bd. for Comparison Based Sorting
   internal node: comparison
   external node: permutation
   algorithm execution: root-to-leaf path




                                             17
How Fast Can We Sort?
 How Fast Can We Sort?
 Proposition: The running time of any
  comparison-based algorithm for sorting an n-
  element sequence S is (n log n).
 Justification: The running time of a
  comparison-based sorting algorithm must be
  equal to or greater than the depth of the
  decision tree T associated with this algorithm.



                                                    18
How Fast Can We Sort? (2)
 Each internal node of T is associated with a
  comparison that establishes the ordering of two
  elements of S.
 Each external node of T represents a distinct
  permutation of the elements of S.
 Hence T must have at least n! external nodes which
  again implies T has a height of at least log(n!)
 Since n! has at least n/2 terms that are greater than
  or equal to n/2, we have: log(n!) ≥ (n/2) log(n/2)
 Total Time Complexity: (n log n).


                                                   19

More Related Content

What's hot

Nikit
NikitNikit
Lec4
Lec4Lec4
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
Merge sort
Merge sortMerge sort
Merge sort
Rojin Khadka
 
Mergesort
MergesortMergesort
Mergesort
luzenith_g
 
Lec3
Lec3Lec3
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
Sorting
SortingSorting
Sorting
Gopi Saiteja
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Kumar
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
Ashim Lamichhane
 

What's hot (20)

Lec10
Lec10Lec10
Lec10
 
Nikit
NikitNikit
Nikit
 
Lec4
Lec4Lec4
Lec4
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Merge sort
Merge sortMerge sort
Merge sort
 
Big o
Big oBig o
Big o
 
Mergesort
MergesortMergesort
Mergesort
 
Sorting
SortingSorting
Sorting
 
Lec3
Lec3Lec3
Lec3
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Sorting
SortingSorting
Sorting
 
Quicksort
QuicksortQuicksort
Quicksort
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
Lec2
Lec2Lec2
Lec2
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Lec16
Lec16Lec16
Lec16
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 

Viewers also liked

Radix Sort
Radix SortRadix Sort
Shell sort slide
Shell sort slideShell sort slide
Shell sort slide
Bella Angriani
 
Shell sort
Shell sortShell sort
Shell sort
Rajendran
 
I 7
I 7I 7
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sort
Krish_ver2
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentation
Ratul Hasan
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
Sarvesh Rawat
 
Metodos de ordenacion radix sort
Metodos de ordenacion radix sortMetodos de ordenacion radix sort
Metodos de ordenacion radix sorttephyfree
 
Visualizing geolocated Internet measurements
Visualizing geolocated Internet measurementsVisualizing geolocated Internet measurements
Visualizing geolocated Internet measurements
Claudio Squarcella
 
History and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
History and Overview of Tobacco Policy Initiatives in Maine, 1897-PresentHistory and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
History and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
Maine Public Health Association Tobacco Policy Subcommittee and Friends of the FHM
 
1172. su.-.pps_angelines
1172.  su.-.pps_angelines1172.  su.-.pps_angelines
1172. su.-.pps_angelinesfilipj2000
 
Marikas blogi
Marikas blogiMarikas blogi
Marikas blogi
mariammacharashvili
 
China's amazing bridges_-_2003v[1]
China's amazing bridges_-_2003v[1]China's amazing bridges_-_2003v[1]
China's amazing bridges_-_2003v[1]filipj2000
 

Viewers also liked (20)

Radix Sort
Radix SortRadix Sort
Radix Sort
 
Lec20
Lec20Lec20
Lec20
 
Lec19
Lec19Lec19
Lec19
 
Lec21
Lec21Lec21
Lec21
 
Shell sort slide
Shell sort slideShell sort slide
Shell sort slide
 
Shell sort
Shell sortShell sort
Shell sort
 
I 7
I 7I 7
I 7
 
Shell sort[1]
Shell sort[1]Shell sort[1]
Shell sort[1]
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sort
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentation
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
 
Metodos de ordenacion radix sort
Metodos de ordenacion radix sortMetodos de ordenacion radix sort
Metodos de ordenacion radix sort
 
Radix sorting
Radix sortingRadix sorting
Radix sorting
 
Visualizing geolocated Internet measurements
Visualizing geolocated Internet measurementsVisualizing geolocated Internet measurements
Visualizing geolocated Internet measurements
 
History and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
History and Overview of Tobacco Policy Initiatives in Maine, 1897-PresentHistory and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
History and Overview of Tobacco Policy Initiatives in Maine, 1897-Present
 
Tearsofawoman
TearsofawomanTearsofawoman
Tearsofawoman
 
1172. su.-.pps_angelines
1172.  su.-.pps_angelines1172.  su.-.pps_angelines
1172. su.-.pps_angelines
 
The Voice 2000 edition
The Voice 2000 editionThe Voice 2000 edition
The Voice 2000 edition
 
Marikas blogi
Marikas blogiMarikas blogi
Marikas blogi
 
China's amazing bridges_-_2003v[1]
China's amazing bridges_-_2003v[1]China's amazing bridges_-_2003v[1]
China's amazing bridges_-_2003v[1]
 

Similar to Lec23

lecture 9
lecture 9lecture 9
lecture 9sajinsc
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbounddespicable me
 
Sorting2
Sorting2Sorting2
Sorting2
Saurabh Mishra
 
lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
Sorting
SortingSorting
Sorting
BHARATH KUMAR
 
220exercises2
220exercises2220exercises2
220exercises2
sadhanakumble
 
sorting
sortingsorting
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.ppt
SushantRaj25
 
3.ppt
3.ppt3.ppt
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
Vijayaraj Raj
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
Tribhuvan University
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
Aravind NC
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
B.Kirron Reddi
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
Vasim Pathan
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannual
maamir farooq
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 

Similar to Lec23 (20)

lecture 9
lecture 9lecture 9
lecture 9
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
Linear sorting
Linear sortingLinear sorting
Linear sorting
 
Sorting2
Sorting2Sorting2
Sorting2
 
lecture 10
lecture 10lecture 10
lecture 10
 
Sorting
SortingSorting
Sorting
 
220exercises2
220exercises2220exercises2
220exercises2
 
sorting
sortingsorting
sorting
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Binsort
BinsortBinsort
Binsort
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannual
 
Selection sort
Selection sortSelection sort
Selection sort
 

More from Anjneya Varshney (7)

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

Recently uploaded

Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 

Lec23

  • 1. More Sorting  radix sort  bucket sort  in-place sorting  how fast can we sort? 1
  • 2. Radix Sort  Unlike other sorting methods, radix sort considers the structure of the keys  Assume keys are represented in a base M number system (M = radix), i.e., if M = 2, the keys are represented in binary Sorting is done by comparing bits in the same position Extension to keys that are alphanumeric strings 2
  • 3. Radix Exchange Sort Examine bits from left to right: 1. Sort array with respect to leftmost bit 2. Partition array 3.Recursion recursively sort top subarray, ignoring leftmost bit recursively sort bottom subarray, ignoring leftmost bit Time to sort n b-bit numbers: O(b n) 3
  • 4. Radix Exchange Sort How do we do the sort from the previous page? Same idea as partition in Quicksort. repeat scan top-down to find key starting with 1; scan bottom-up to find key starting with 0 exchange keys; until scan indices cross; 4
  • 6. Radix Exchange Sort vs. Quicksort Similarities both partition array both recursively sort sub-arrays Differences Method of partitioning radix exchange divides array based on greater than or less than 2b-1 quicksort partitions based on greater than or less than some element of the array Time complexity Radix exchange O (bn) Quicksort average case O (n log n) 6
  • 7. Straight Radix Sort Examines bits from right to left for k := 0 to b-1 sort the array in a stable way, looking only at bit k Note order of these bits after sort. 7
  • 8. What is “sort in a stable way”!!! In a stable sort, the initial relative order of equal keys is unchanged. For example, observe the first step of the sort from the previous page: Note that the relative order of those keys ending with 0 is unchanged, and the same is true for elements ending in 1 8
  • 9. The Algorithm is Correct (right?) We show that any two keys are in the correct relative order at the end of the algorithm Given two keys, let k be the leftmost bit-position where they differ At step k the two keys are put in the correct relative order Because of stability, the successive steps do not change the relative order of the two keys 9
  • 10. For Instance, Consider a sort on an array with these two keys: 10
  • 11. Radix sorting applied to decimal numbers 11
  • 12. Straight Radix Sort Time Complexity for k = 0 to b - 1 sort the array in a stable way, looking only at bit k Suppose we can perform the stable sort above in O(n) time. The total time complexity would be O(bn) As you might have guessed, we can perform a stable sort based on the keys’ kth digit in O(n) time. The method, you ask? Why it’s Bucket Sort, of course. 12
  • 13. Bucket Sort BASICS: n numbers Each number  {1, 2, 3, ... m} Stable Time: O (n + m) For example, m = 3 and our array is: (note that there are two “2”s and two “1”s) First, we create M “buckets” 13
  • 14. Bucket Sort Each element of the array is put in one of the m “buckets” 14
  • 15. Bucket Sort Now, pull the elements from the buckets into the array At last, the sorted array (sorted in a stable way): 15
  • 16. In-Place Sorting  A sorting algorithm is said to be in-place if  it uses no auxiliary data structures (however, O(1) auxiliary variables are allowed)  it updates the input sequence only by means of operations replaceElement and swapElements  Which sorting algorithms seen can be made to work in place? 16
  • 17. Lower Bd. for Comparison Based Sorting  internal node: comparison  external node: permutation  algorithm execution: root-to-leaf path 17
  • 18. How Fast Can We Sort?  How Fast Can We Sort?  Proposition: The running time of any comparison-based algorithm for sorting an n- element sequence S is (n log n).  Justification: The running time of a comparison-based sorting algorithm must be equal to or greater than the depth of the decision tree T associated with this algorithm. 18
  • 19. How Fast Can We Sort? (2)  Each internal node of T is associated with a comparison that establishes the ordering of two elements of S.  Each external node of T represents a distinct permutation of the elements of S.  Hence T must have at least n! external nodes which again implies T has a height of at least log(n!)  Since n! has at least n/2 terms that are greater than or equal to n/2, we have: log(n!) ≥ (n/2) log(n/2)  Total Time Complexity: (n log n). 19