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
5
12
35
42
77 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
5
12
35
42
77 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
5
12
35
42
77 101
Swap
42 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
5
12
35
77
42 101
Swap
35 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
5
12
77
35
42 101
Swap
12 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
5
77
12
35
42 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
5
77
12
35
42 101 Swap
5 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
77
12
35
42 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
77
12
35
42 5 101
5
42
12
35 77 101
42
5
35
12 77 101
42
35
5
12 77 101
42
35
12
5 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.]
4. 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
1
3
2
9
6
4
8
8
3
2
9
6
4
1
8
3
4
9
6
2
1
8
6
4
9
3
2
1
8
9
6
4
3
2
1
8
6
9
4
3
2
1
9
8
6
4
3
2
1
9
8
6
4
3
2
1
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

PPT
Data Structure and Algorithms Sorting
PPTX
Unit 7 sorting
PPTX
Unit vii sorting
PDF
L 14-ct1120
PPTX
Lecture 13 data structures and algorithms
PDF
Sorting
PDF
Sorting
PPTX
Data structure.pptx
Data Structure and Algorithms Sorting
Unit 7 sorting
Unit vii sorting
L 14-ct1120
Lecture 13 data structures and algorithms
Sorting
Sorting
Data structure.pptx

Similar to ds 3Sorting.ppt (20)

PPTX
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
PPTX
BUBBLESORT
PPTX
Different Searching and Sorting Methods.pptx
PPT
Sorting algorithums > Data Structures & Algorithums
PPT
Data Structure (MC501)
PPT
Sorting algorithm
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PPTX
Searching and sorting Techniques in Data structures
PPTX
Data Structure and algorithms for software
PPTX
Sorting Data structure And Algorithm.pptx
PPTX
Sorting method data structure
PPT
Sorting algorithms
PPTX
Sorting pnk
PPTX
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
PPTX
Chapter-2.pptx
PPT
sorting_part1.ppt
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPTX
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
PPT
Sorting algorithms
PPT
simple-sorting algorithms
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
BUBBLESORT
Different Searching and Sorting Methods.pptx
Sorting algorithums > Data Structures & Algorithums
Data Structure (MC501)
Sorting algorithm
358 33 powerpoint-slides_14-sorting_chapter-14
Searching and sorting Techniques in Data structures
Data Structure and algorithms for software
Sorting Data structure And Algorithm.pptx
Sorting method data structure
Sorting algorithms
Sorting pnk
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
Chapter-2.pptx
sorting_part1.ppt
Data Structures - Lecture 8 [Sorting Algorithms]
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
Sorting algorithms
simple-sorting algorithms
Ad

Recently uploaded (20)

DOCX
573137875-Attendance-Management-System-original
PPT
Project quality management in manufacturing
PPT
Drone Technology Electronics components_1
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Internship_Presentation_Final engineering.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
web development for engineering and engineering
PPTX
AgentX UiPath Community Webinar series - Delhi
573137875-Attendance-Management-System-original
Project quality management in manufacturing
Drone Technology Electronics components_1
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
ETO & MEO Certificate of Competency Questions and Answers
bas. eng. economics group 4 presentation 1.pptx
anatomy of limbus and anterior chamber .pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Simulation of electric circuit laws using tinkercad.pptx
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Internship_Presentation_Final engineering.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Chapter 6 Design in software Engineeing.ppt
web development for engineering and engineering
AgentX UiPath Community Webinar series - Delhi
Ad

ds 3Sorting.ppt

  • 2. Sorting Algorithm  Sorting takes an unordered collection and makes it an ordered one. 2 5 12 35 42 77 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 5 12 35 42 77 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 5 12 35 42 77 101 Swap 42 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 5 12 35 77 42 101 Swap 35 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 5 12 77 35 42 101 Swap 12 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 5 77 12 35 42 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 5 77 12 35 42 101 Swap 5 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 77 12 35 42 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 77 12 35 42 5 101 5 42 12 35 77 101 42 5 35 12 77 101 42 35 5 12 77 101 42 35 12 5 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.] 4. 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)