SlideShare a Scribd company logo
Bubble sort
Insertion sort
Selection sort
Sorting Algorithm
Sorting takes an unordered collection and
makes it an ordered one.
2
512354277 101
5 12 35 42 77 101
1 2 3 4 5 6
1 2 3 4 5 6
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-
wise comparisons and swapping
3
512354277 101
1 2 3 4 5 6
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-
wise comparisons and swapping
4
512354277 101Swap42 77
1 2 3 4 5 6
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-
wise comparisons and swapping
5
512357742 101Swap35 77
1 2 3 4 5 6
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-
wise comparisons and swapping
6
512773542 101Swap12 77
1 2 3 4 5 6
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-
wise comparisons and swapping
7
577123542 101
No need to swap
1 2 3 4 5 6
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-
wise comparisons and swapping
8
577123542 101 Swap5 101
1 2 3 4 5 6
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-
wise comparisons and swapping
9
77123542 5 101
Largest value correctly placed
1 2 3 4 5 6
Repeat “Bubble Up” How Many
Times?
If we have N elements…
And if each time we bubble an element, we
place it in its correct location…
Then we repeat the “bubble up” process N
– 1 times.
This guarantees we’ll correctly
place all N elements.
10
“Bubbling” All the Elements
11
77123542 5 101
5421235 77 101
4253512 77 101
4235512 77 101
4235125 77 101
N-1
1 2 3 4 5 6
Bubble Sort
Algorithm
for i  1 to n-1 do
for j  1 to n-i do
if (A[j+1] < A[j]) swap A[j] and A[j+1] ;
}
}
Analysis:
In general, if the list has n elements, we will have to do
(n-1) + (n-2) …. + 2 +1 = (n-1) n / 2 comparisons.
=O(n^2)
12
Insertion Sort
INSERTION_SORT (A, N)
1.Set A[0] = -∞.
2.Repeat Step 3 to 5 for K = 2, 3, …, N:
3. Set TEMP = A[K] and PTR = K – 1.
4. Repeat while TEMP < A[PTR]:
(a) Set A[PTR+1] = A[PTR]
(b) Set PTR = PTR – 1.
[End of Loop.]
5. Set A[PTR+1] = TEMP.
[End of Loop 2.]
6.Return.
Insertion Sort Example
Sort: 34 8 64 51 32 21
34 8 64 51 32 21
The algorithm sees that 8 is smaller than 34 so it swaps.
8 34 64 51 32 21
51 is smaller than 64, so they swap.
8 34 51 64 32 21
8 34 51 64 32 21 (from previous slide)
The algorithm sees 32 as another smaller number and moves
it to its appropriate location between 8 and 34.
8 32 34 51 64 21
The algorithm sees 21 as another smaller number and moves
into between 8 and 32.
Final sorted numbers:
8 21 32 34 51 64
Insertion Sort Complexity
This Sorting algorithm is frequently used when n is very small.
Worst case occurs when array is in reverse order. The inner loop
must use K – 1 comparisons.
f(n) = 1 + 2 + 3 + ….+ (n – 1)
= n(n – 1)/2
= O(n2
)
In average case, there will be approximately (K – 1)/2 comparisons
in the inner loop.
f(n) = (1 + 2 + 3 + ….+ (n – 1))/2
= n(n – 1)/4
= O(n2
)
Selection Sort
This algorithm sorts an array A with N elements.
SELECTION(A, N)
1.Repeat steps 2 and 3 for k=1 to N-1:
2. Call MIN(A, K, N, LOC).
3. [Interchange A[k] and A[LOC]]
Set Temp:= A[k], A[k]:= A[LOC] and A[LOC]:=Temp.
[End of step 1 Loop.]
1.Exit.
MIN(A, K, N, LOC).
1.Set MIN := A[K] and LOC:= K.
2.Repeat for j=k+1 to N:
If Min>A[j], then: Set Min:= A[j] and LOC:=J.
[End of if structure]
3.Return.
Selection Sort Example
1329648
8329641
8349621
8649321
8964321
8694321
9864321
9864321
Selection Sort Complexity
The number f(n) of comparisons in selection sort
algorithm is independent of original order of elements.
There are n-1 comparisons during pass 1 to find the
smallest element, n-2 comparisons during pass 2 to find
the second smallest element, and so on.
Accordingly,
f (n) = (n-1)+(n-2)+-----+2+1
= n(n-1)/2
= O(n2
)
The f (n) holds the same value O(n2
) both for worst case
and average case.
Comparing the Algorithms
Best Average Worst
Case Case Case
Bubble Sort O(n) O(n2
) O(n2
)
Insertion Sort O(n) O(n2
) O(n2
)
Selection Sort O(n2
) O(n2
) O(n2
)
Thank You

More Related Content

What's hot (20)

PDF
8th pre alg -l41
jdurst65
 
PPTX
Merge sort-algorithm for computer science engineering students
University of Science and Technology Chitttagong
 
PPT
Algorithm: Quick-Sort
Tareq Hasan
 
PPT
Algorithm designing using divide and conquer algorithms
SiddhantShelake
 
PPTX
10 merge sort
irdginfo
 
PPTX
Daa final
Gagan019
 
PPTX
Merge sort algorithm
Shubham Dwivedi
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PPT
03 dc
Hira Gul
 
DOCX
Advance heat transfer 2
JudeOliverMaquiran1
 
PPSX
Sorting and searching
kalyanineve
 
PDF
4 ESO Academics - UNIT 02 - POWERS, ROOTS AND LOGARITHMS
Gogely The Great
 
PPT
Linear Systems Gauss Seidel
Eric Davishahl
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPTX
Functions
Maurice Verreck
 
PPT
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
PPTX
Divide and conquer - Quick sort
Madhu Bala
 
PDF
Divide and conquer
Emmanuel college
 
PPTX
Split and list technique for solving hard problems
Rohit Kumar Singh
 
DOCX
State feedback example
cairo university
 
8th pre alg -l41
jdurst65
 
Merge sort-algorithm for computer science engineering students
University of Science and Technology Chitttagong
 
Algorithm: Quick-Sort
Tareq Hasan
 
Algorithm designing using divide and conquer algorithms
SiddhantShelake
 
10 merge sort
irdginfo
 
Daa final
Gagan019
 
Merge sort algorithm
Shubham Dwivedi
 
Divide and conquer
Dr Shashikant Athawale
 
03 dc
Hira Gul
 
Advance heat transfer 2
JudeOliverMaquiran1
 
Sorting and searching
kalyanineve
 
4 ESO Academics - UNIT 02 - POWERS, ROOTS AND LOGARITHMS
Gogely The Great
 
Linear Systems Gauss Seidel
Eric Davishahl
 
Dinive conquer algorithm
Mohd Arif
 
Functions
Maurice Verreck
 
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
 
Divide and conquer - Quick sort
Madhu Bala
 
Divide and conquer
Emmanuel college
 
Split and list technique for solving hard problems
Rohit Kumar Singh
 
State feedback example
cairo university
 

Similar to Data Structure and Algorithms Sorting (20)

PPT
ds 3Sorting.ppt
AlliVinay1
 
PDF
Sorting and Hashing Algorithm full pdfs.
NikhilSoni177492
 
PPTX
BUBBLESORT
Ashish Sadavarti
 
PPT
Bubble sort
Manek Ar
 
PPTX
bubblesort-130417100323-phpapp02[1].pptx
TusharTikia
 
PPTX
sorting.pptx
DrRanjeetKumar51721
 
PPTX
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
PPT
Bubble Sort Python
ValneyFilho1
 
PPT
cs1311lecture16wdl.ppt
RameshKumarYadav29
 
PPT
Bubble Sort.ppt
MuhammadSheraz836877
 
PPTX
Bubble Sort presentation.pptx
TusharTikia
 
PPT
1624 sequence
Dr Fereidoun Dejahang
 
PDF
Jurnal informatika
MamaMa28
 
PDF
Sorting
Zaid Shabbir
 
PDF
Sorting
Zaid Shabbir
 
PPTX
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
ds 3Sorting.ppt
AlliVinay1
 
Sorting and Hashing Algorithm full pdfs.
NikhilSoni177492
 
BUBBLESORT
Ashish Sadavarti
 
Bubble sort
Manek Ar
 
bubblesort-130417100323-phpapp02[1].pptx
TusharTikia
 
sorting.pptx
DrRanjeetKumar51721
 
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
Unit vii sorting
Tribhuvan University
 
Unit 7 sorting
Dabbal Singh Mahara
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Bubble Sort Python
ValneyFilho1
 
cs1311lecture16wdl.ppt
RameshKumarYadav29
 
Bubble Sort.ppt
MuhammadSheraz836877
 
Bubble Sort presentation.pptx
TusharTikia
 
1624 sequence
Dr Fereidoun Dejahang
 
Jurnal informatika
MamaMa28
 
Sorting
Zaid Shabbir
 
Sorting
Zaid Shabbir
 
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Ad

More from ManishPrajapati78 (14)

PPT
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
PPT
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
PPT
Data Structure and Algorithms Queues
ManishPrajapati78
 
PPTX
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
PPT
Data Structure and Algorithms Stacks
ManishPrajapati78
 
PPT
Data Structure and Algorithms Linked List
ManishPrajapati78
 
PPT
Data Structure and Algorithms Arrays
ManishPrajapati78
 
PPT
Data Structure and Algorithms
ManishPrajapati78
 
PPT
Data Structure and Algorithms Hashing
ManishPrajapati78
 
PPTX
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
PPT
Data Structure and Algorithms Graphs
ManishPrajapati78
 
PPT
Data Structure and Algorithms Huffman Coding Algorithm
ManishPrajapati78
 
PPT
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
PPT
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
Data Structure and Algorithms Queues
ManishPrajapati78
 
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
Data Structure and Algorithms Stacks
ManishPrajapati78
 
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Data Structure and Algorithms
ManishPrajapati78
 
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
Data Structure and Algorithms Graphs
ManishPrajapati78
 
Data Structure and Algorithms Huffman Coding Algorithm
ManishPrajapati78
 
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
Ad

Recently uploaded (20)

PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 

Data Structure and Algorithms Sorting

  • 2. Sorting Algorithm Sorting takes an unordered collection and makes it an ordered one. 2 512354277 101 5 12 35 42 77 101 1 2 3 4 5 6 1 2 3 4 5 6
  • 3. "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair- wise comparisons and swapping 3 512354277 101 1 2 3 4 5 6
  • 4. "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair- wise comparisons and swapping 4 512354277 101Swap42 77 1 2 3 4 5 6
  • 5. "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair- wise comparisons and swapping 5 512357742 101Swap35 77 1 2 3 4 5 6
  • 6. "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair- wise comparisons and swapping 6 512773542 101Swap12 77 1 2 3 4 5 6
  • 7. "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair- wise comparisons and swapping 7 577123542 101 No need to swap 1 2 3 4 5 6
  • 8. "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair- wise comparisons and swapping 8 577123542 101 Swap5 101 1 2 3 4 5 6
  • 9. "Bubbling Up" the Largest Element Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair- wise comparisons and swapping 9 77123542 5 101 Largest value correctly placed 1 2 3 4 5 6
  • 10. Repeat “Bubble Up” How Many Times? If we have N elements… And if each time we bubble an element, we place it in its correct location… Then we repeat the “bubble up” process N – 1 times. This guarantees we’ll correctly place all N elements. 10
  • 11. “Bubbling” All the Elements 11 77123542 5 101 5421235 77 101 4253512 77 101 4235512 77 101 4235125 77 101 N-1 1 2 3 4 5 6
  • 12. Bubble Sort Algorithm for i  1 to n-1 do for j  1 to n-i do if (A[j+1] < A[j]) swap A[j] and A[j+1] ; } } Analysis: In general, if the list has n elements, we will have to do (n-1) + (n-2) …. + 2 +1 = (n-1) n / 2 comparisons. =O(n^2) 12
  • 13. Insertion Sort INSERTION_SORT (A, N) 1.Set A[0] = -∞. 2.Repeat Step 3 to 5 for K = 2, 3, …, N: 3. Set TEMP = A[K] and PTR = K – 1. 4. Repeat while TEMP < A[PTR]: (a) Set A[PTR+1] = A[PTR] (b) Set PTR = PTR – 1. [End of Loop.] 5. Set A[PTR+1] = TEMP. [End of Loop 2.] 6.Return.
  • 14. Insertion Sort Example Sort: 34 8 64 51 32 21 34 8 64 51 32 21 The algorithm sees that 8 is smaller than 34 so it swaps. 8 34 64 51 32 21 51 is smaller than 64, so they swap. 8 34 51 64 32 21 8 34 51 64 32 21 (from previous slide) The algorithm sees 32 as another smaller number and moves it to its appropriate location between 8 and 34. 8 32 34 51 64 21 The algorithm sees 21 as another smaller number and moves into between 8 and 32. Final sorted numbers: 8 21 32 34 51 64
  • 15. Insertion Sort Complexity This Sorting algorithm is frequently used when n is very small. Worst case occurs when array is in reverse order. The inner loop must use K – 1 comparisons. f(n) = 1 + 2 + 3 + ….+ (n – 1) = n(n – 1)/2 = O(n2 ) In average case, there will be approximately (K – 1)/2 comparisons in the inner loop. f(n) = (1 + 2 + 3 + ….+ (n – 1))/2 = n(n – 1)/4 = O(n2 )
  • 16. Selection Sort This algorithm sorts an array A with N elements. SELECTION(A, N) 1.Repeat steps 2 and 3 for k=1 to N-1: 2. Call MIN(A, K, N, LOC). 3. [Interchange A[k] and A[LOC]] Set Temp:= A[k], A[k]:= A[LOC] and A[LOC]:=Temp. [End of step 1 Loop.] 1.Exit. MIN(A, K, N, LOC). 1.Set MIN := A[K] and LOC:= K. 2.Repeat for j=k+1 to N: If Min>A[j], then: Set Min:= A[j] and LOC:=J. [End of if structure] 3.Return.
  • 18. Selection Sort Complexity The number f(n) of comparisons in selection sort algorithm is independent of original order of elements. There are n-1 comparisons during pass 1 to find the smallest element, n-2 comparisons during pass 2 to find the second smallest element, and so on. Accordingly, f (n) = (n-1)+(n-2)+-----+2+1 = n(n-1)/2 = O(n2 ) The f (n) holds the same value O(n2 ) both for worst case and average case.
  • 19. Comparing the Algorithms Best Average Worst Case Case Case Bubble Sort O(n) O(n2 ) O(n2 ) Insertion Sort O(n) O(n2 ) O(n2 ) Selection Sort O(n2 ) O(n2 ) O(n2 )