Lecture 03 - Simple Sorting Algorithms
Lecture 03 - Simple Sorting Algorithms
International University
School of Computer Science and Engineering
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 2
Objectives
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 3
Major Topics
• Introductory Remarks
• Bubble Sort
• Selection Sort
• Insertion Sort
• Sorting Objects
• Comparing Sorts
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 4
Introduction
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 5
Introduction
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 6
How would you do it?
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 7
Basic idea of these simple sorts
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 8
Bubble sort
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 9
Description
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 10
Observations
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 11
Observations
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 12
Obama on
bubble sort
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 13
Implementation
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 14
Implementation
+After one pass, the largest item must be the last in the list
+Start at the front again:
+the second pass will bring the second largest element into the second last
position
+Repeat n – 1 times, after which, all entries will be in place
+That why it is named Bubble sort
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 15
Simulation
https://www.hackerearth.com/practice/algorithms/sorting/bubble-sort/visualize/
https://visualgo.net/en/sorting
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 16
Bubble Sort process
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 17
The Basic Algorithm
+Here we have two nested loops, and therefore calculating the run
time is straight-forward:
n −1 n ( n − 1) n ( n − 1)
( n − k ) = n ( n − 1) −
k =1 2
=
2
= (n 2 )
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S ( I T5 1 1) 18
Bubble Sort process
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 19
Hand-on
89 58 29 40 12 42 10 1
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 20
Efficiency of the Bubble Sort - Comparisons
• Can readily see that there are fewer comparisons each ‘pass.’
• Thus, number of comparisons is computed as:
(n-1)+(n-2)+… + 1 = n(n-1)/2;
• For 10 elements, the number is 10*9/2 = 45.
• So, the algorithm makes about n2/2 comparisons
• (ignoring the -1 which is negligible especially if N is large)
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 25
Efficiency of the Bubble Sort - Swaps
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 26
Overall – Bubble Sort
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 27
Question
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 28
Selection sort
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 29
Selection Sort
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 30
How does the Selection Sort work?
https://www.hackerearth.com/practice/algorithms/sorting/selection-sort/visualize/
https://visualgo.net/en/sorting
Selection Sort – in more detail
• So, in one pass, you have made n comparisons but possibly ONLY
ONE Swap!
• With each succeeding pass,
• one more item is sorted and in place;
• one fewer item needs to be considered.
• Java code for the Selection Sort (p93-94).
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 33
Selection Sort
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 34
Hand-on
89 58 29 40 12 42 10 1
1 32 12 53 11 76 23 89
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 35
Selection Sort itself - more
https://www.hackerearth.com/practice/algorithms/sorting/insertion-sort/visualize/
https://visualgo.net/en/sorting
Insertion Sort
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 44
Discussion– How it really implemented!!
• Start with out = 1, which means there is only a single element to its
‘left.’
• We infer that this item to its left is sorted unto itself.
• Hard to argue this is not true. (This is out = 0)
• a[out] is the marked item, and it is moved into temp.
• a[out] is the leftmost unsorted item.
Hand-on
89 58 29 40 12 42 10 1
1 32 12 53 11 76 23 89
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 46
Efficiency of the Insertion Sort
• Comparisons:
• On pass one, max of one;
pass two, max of two, etc.
Up to a max of n-1 comparisons.
→ 1+2+3+…+n-1 = n*(n-1)/2 comparisons.
• But, on average
n*(n-1)/4.
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 47
Efficiency of the Insertion Sort (2 of 3)
• Copy:
• Have lots of ‘copies’
• (same as number of comparisons – about)
• ➔ But a copy is not nearly as time-consuming as a swap. Think about this!!
• For random data,
• twice as fast as the bubble sort
• faster than the selection sort.
• Still runs on O(n2) time for random data.
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 48
Efficiency of the Insertion Sort (3 of 3)
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 49
Sorting Objects
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 50
Sorting Objects
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 51
Sorting Objects
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 52
Secondary Sort Fields?
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 53
Comparing the Simple Sorts
• Bubble Sort, Selection Sort, and Insertion Sort all have a worst-case
time complexity of O(n^2).
• Bubble Sort, Selection Sort, and Insertion Sort all have a space
complexity of O(1) as they are in-place sorting algorithms.
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 54
Comparing the Simple Sorts
T U E SD A Y , 2 4 S EP T EM BE R 2 0 24 56
Vietnam National University of HCMC
International University
School of Computer Science and Engineering
THANK YOU