SlideShare a Scribd company logo
Survey of Sorting
Ananda Gunawardena
Naïve sorting algorithms
• Bubble sort: scan for flips, until all are fixed
3 2 1 6 5 4
2 3 1 6 5 4
2 1 3 6 5 4
2 1 3 5 6 4
2 1 3 5 4 6 Etc...
Naïve Sorting
for i=1 to n-1
{ for j=0 to n-i-1
if (A[j].compareTo(A[j+1])>0)
swap(A[j], A[j+1]);
if (no swaps) break;
}
• What happens if
– All keys are equal?
– Keys are sorted in reverse order?
– Keys are sorted?
– keys are randomly distributed?
• Exercise: Count the number of operations in bubble sort
and find a Big O analysis for bubble sort
Insertion sort
105 47 13 99 30 222
47 105 13 99 30 222
13 47 105 99 30 222
13 47 99 105 30 222
13 30 47 99 105 222
105 47 13 99 30 222
Sorted subarray
Insertion sort
• Algorithm
for i = 1 to n-1 do
insert a[i] in the proper place
in a[0:i-1]
• Correctness
•Note: after i steps, the sub-array A[0:i] is
sorted
How fast is insertion sort?
# of slides = O(#inversions)
very fast if array is nearly sorted to begin with
tmp = a[i];
for (j = i; j>0 && a[j-1]>tmp; j--)
a[j] = a[j-1];
a[j] = tmp;
To insert a[i] into a[0:i-1], slide all elements larger
than a[i] to the right.
Selection sort
• Algorithm
for i = n-1 to 1 do
Find the largest entry in the
in the subarray A[0:i]
Swap with A[i]
What is the runtime complexity of
selection sort?
Sorting Comparison
• Discuss the pros and cons of each of the
naïve sorting algorithms
Advanced Sorting
Quick Sort
• Fastest algorithm in practice
• Algorithm
– Find a pivot
– Move all elements smaller than pivot to left
– Move all elements bigger than pivot to right
– Recursively sort each half
– O(n log n) algorithm
Merge Sort
• Divide the array into two equal halves
• Divide each half recursively until each
array is of size 1
• Merge two (sorted) arrays of size 1
• Complete the process recursively
Heap Sort
• Build a max heap
• Delete Max (attach to end of array) until
heap is empty
• Resulting array is sorted
• Complexity
Bucket Sort
Bucket sort
• In addition to comparing pairs of elements,
we require these additional restrictions:
– all elements are non-negative integers
– all elements are less than a predetermined
maximum value
• Elements are usually keys paired with other
data
Bucket sort
1 3 3 1 2
1 2 3
Bucket sort characteristics
• Runs in O(N) time.
• Easy to implement each bucket as a linked
list.
• Is stable:
– If two elements (A,B) are equal with respect to
sorting, and they appear in the input in order
(A,B), then they remain in the same order in the
output.
Radix Sort
Radix sort
• If your integers are in a larger range then
do bucket sort on each digit
• Start by sorting with the low-order digit
using a STABLE bucket sort.
• Then, do the next-lowest,and so on
Radix sort
• Example:
0 1 0
0 0 0
1 0 1
0 0 1
1 1 1
0 1 1
1 0 0
1 1 0
2
0
5
1
7
3
4
6
0
1
2
3
4
5
6
7
0 1 0
0 0 0
1 0 0
1 1 0
1 0 1
0 0 1
1 1 1
0 1 1
0 0 0
1 0 0
1 0 1
0 0 1
0 1 0
1 1 0
1 1 1
0 1 1
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Each sorting step must be stable.
Radix sort characteristics
• Each sorting step can be performed via
bucket sort, and is thus O(N).
• If the numbers are all b bits long, then
there are b sorting steps.
• Hence, radix sort is O(bN).
What about non-binary?
• Radix sort can be used for decimal
numbers and alphanumeric strings.
0 3 2
2 2 4
0 1 6
0 1 5
0 3 1
1 6 9
1 2 3
2 5 2
0 3 1
0 3 2
2 5 2
1 2 3
2 2 4
0 1 5
0 1 6
1 6 9
0 1 5
0 1 6
1 2 3
2 2 4
0 3 1
0 3 2
2 5 2
1 6 9
0 1 5
0 1 6
0 3 1
0 3 2
1 2 3
1 6 9
2 2 4
2 5 2

More Related Content

Similar to Lecture 21 Survey of Sorting.ppt ggggggggggg (20)

PDF
Analysis and Comparative of Sorting Algorithms
ijtsrd
 
PPTX
SORTING techniques.pptx
Dr.Shweta
 
PPT
Counting Sort Lowerbound
despicable me
 
PPTX
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
PPTX
Sorting2
Saurabh Mishra
 
PPT
lecture 9
sajinsc
 
PPTX
Searching and sorting Techniques in Data structures
PRIANKA R
 
PPT
Sorting Algorithms
multimedia9
 
PPT
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
PPT
Quicksort
Gayathri Gaayu
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPTX
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
PPTX
Analysis of algorithms
MUSAIDRIS15
 
PPTX
Sorting ppt
Hassan Mustafa
 
PPTX
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
PPT
Cs105 l15-bucket radix
Gopi Saiteja
 
DOCX
Sorting
BHARATH KUMAR
 
ODP
Sorting Algorithm
Abu Shaik saurab
 
PDF
Linear sort
Amit Kumar Rathi
 
Analysis and Comparative of Sorting Algorithms
ijtsrd
 
SORTING techniques.pptx
Dr.Shweta
 
Counting Sort Lowerbound
despicable me
 
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
Sorting2
Saurabh Mishra
 
lecture 9
sajinsc
 
Searching and sorting Techniques in Data structures
PRIANKA R
 
Sorting Algorithms
multimedia9
 
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
Quicksort
Gayathri Gaayu
 
Unit vii sorting
Tribhuvan University
 
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Analysis of algorithms
MUSAIDRIS15
 
Sorting ppt
Hassan Mustafa
 
Algorithms and Data Structures - Parahyangan Catholic University Credit Lionov
Pratik Parmar
 
Cs105 l15-bucket radix
Gopi Saiteja
 
Sorting
BHARATH KUMAR
 
Sorting Algorithm
Abu Shaik saurab
 
Linear sort
Amit Kumar Rathi
 

Recently uploaded (20)

PDF
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PDF
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PDF
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PPTX
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
PDF
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
PPTX
The _Operations_on_Functions_Addition subtruction Multiplication and Division...
mdregaspi24
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PPTX
Dr djdjjdsjsjsjsjsjsjjsjdjdjdjdjjd1.pptx
Nandy31
 
PDF
AUDITABILITY & COMPLIANCE OF AI SYSTEMS IN HEALTHCARE
GAHI Youssef
 
PPTX
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PDF
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PDF
Copia de Strategic Roadmap Infographics by Slidesgo.pptx (1).pdf
ssuserd4c6911
 
PPTX
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
The _Operations_on_Functions_Addition subtruction Multiplication and Division...
mdregaspi24
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
Dr djdjjdsjsjsjsjsjsjjsjdjdjdjdjjd1.pptx
Nandy31
 
AUDITABILITY & COMPLIANCE OF AI SYSTEMS IN HEALTHCARE
GAHI Youssef
 
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
Copia de Strategic Roadmap Infographics by Slidesgo.pptx (1).pdf
ssuserd4c6911
 
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
Ad

Lecture 21 Survey of Sorting.ppt ggggggggggg

  • 2. Naïve sorting algorithms • Bubble sort: scan for flips, until all are fixed 3 2 1 6 5 4 2 3 1 6 5 4 2 1 3 6 5 4 2 1 3 5 6 4 2 1 3 5 4 6 Etc...
  • 3. Naïve Sorting for i=1 to n-1 { for j=0 to n-i-1 if (A[j].compareTo(A[j+1])>0) swap(A[j], A[j+1]); if (no swaps) break; } • What happens if – All keys are equal? – Keys are sorted in reverse order? – Keys are sorted? – keys are randomly distributed? • Exercise: Count the number of operations in bubble sort and find a Big O analysis for bubble sort
  • 4. Insertion sort 105 47 13 99 30 222 47 105 13 99 30 222 13 47 105 99 30 222 13 47 99 105 30 222 13 30 47 99 105 222 105 47 13 99 30 222 Sorted subarray
  • 5. Insertion sort • Algorithm for i = 1 to n-1 do insert a[i] in the proper place in a[0:i-1] • Correctness •Note: after i steps, the sub-array A[0:i] is sorted
  • 6. How fast is insertion sort? # of slides = O(#inversions) very fast if array is nearly sorted to begin with tmp = a[i]; for (j = i; j>0 && a[j-1]>tmp; j--) a[j] = a[j-1]; a[j] = tmp; To insert a[i] into a[0:i-1], slide all elements larger than a[i] to the right.
  • 7. Selection sort • Algorithm for i = n-1 to 1 do Find the largest entry in the in the subarray A[0:i] Swap with A[i] What is the runtime complexity of selection sort?
  • 8. Sorting Comparison • Discuss the pros and cons of each of the naïve sorting algorithms
  • 10. Quick Sort • Fastest algorithm in practice • Algorithm – Find a pivot – Move all elements smaller than pivot to left – Move all elements bigger than pivot to right – Recursively sort each half – O(n log n) algorithm
  • 11. Merge Sort • Divide the array into two equal halves • Divide each half recursively until each array is of size 1 • Merge two (sorted) arrays of size 1 • Complete the process recursively
  • 12. Heap Sort • Build a max heap • Delete Max (attach to end of array) until heap is empty • Resulting array is sorted • Complexity
  • 14. Bucket sort • In addition to comparing pairs of elements, we require these additional restrictions: – all elements are non-negative integers – all elements are less than a predetermined maximum value • Elements are usually keys paired with other data
  • 15. Bucket sort 1 3 3 1 2 1 2 3
  • 16. Bucket sort characteristics • Runs in O(N) time. • Easy to implement each bucket as a linked list. • Is stable: – If two elements (A,B) are equal with respect to sorting, and they appear in the input in order (A,B), then they remain in the same order in the output.
  • 18. Radix sort • If your integers are in a larger range then do bucket sort on each digit • Start by sorting with the low-order digit using a STABLE bucket sort. • Then, do the next-lowest,and so on
  • 19. Radix sort • Example: 0 1 0 0 0 0 1 0 1 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 2 0 5 1 7 3 4 6 0 1 2 3 4 5 6 7 0 1 0 0 0 0 1 0 0 1 1 0 1 0 1 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 1 0 1 1 1 0 1 1 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 Each sorting step must be stable.
  • 20. Radix sort characteristics • Each sorting step can be performed via bucket sort, and is thus O(N). • If the numbers are all b bits long, then there are b sorting steps. • Hence, radix sort is O(bN).
  • 21. What about non-binary? • Radix sort can be used for decimal numbers and alphanumeric strings. 0 3 2 2 2 4 0 1 6 0 1 5 0 3 1 1 6 9 1 2 3 2 5 2 0 3 1 0 3 2 2 5 2 1 2 3 2 2 4 0 1 5 0 1 6 1 6 9 0 1 5 0 1 6 1 2 3 2 2 4 0 3 1 0 3 2 2 5 2 1 6 9 0 1 5 0 1 6 0 3 1 0 3 2 1 2 3 1 6 9 2 2 4 2 5 2