Lecture 07 - Sorting
Lecture 07 - Sorting
March 5, 2020
+ 2
Objectives
◼ Idea of Sorting
◼ What is Sorting?
◼ Selection Sort
◼ Insertion Sort
◼ Merge Sort
◼ Quick Sort
March 5, 2020
+ 3
Idea of Sorting
March 5, 2020
+ 4
Recommended Readings
March 5, 2020
+ 5
What is Sorting?
◼ Example
◼ To arrange names in alphabetical order
◼ Arrange students by grade, etc.
A={3162134590} (unsorted)
A={0112334569} (sorted)
March 5, 2020
+ 6
◼ Examples:
◼ Sorted in Ascending Order: Phone directory and dictionary
March 5, 2020
+ 7
March 5, 2020
+ 8
Sorting Process
◼ Swap: Change the position of the elements in order to get the right
order
Sorting Algorithms
◼ Selection Sort
◼ Insertion Sort
◼ Quick Sort
March 5, 2020
+ 10
Bubble Sort
March 5, 2020
+ 11
End If
March 5, 2020
+ 12
Bubble Sort
March 5, 2020
+ 13
March 5, 2020
+ 14
Selection Sort
March 5, 2020
+ Selection Sort 15
March 5, 2020
+ 16
March 5, 2020
+ Insertion Sort 17
March 5, 2020
+ 18
Insertion Sort
◼ For each iteration
◼ insertion sort removes one element from the input
data
◼ finds the location it belongs within the sorted list,
and
◼ inserts it there.
March 5, 2020
+ 19
Insertion Sort
March 5, 2020
+ 20
March 5, 2020
+ 21
Comparison
◼ Selection
sort can be used when amount of data is small and
swapping is time-consuming.
◼ Insertion sort is most versatile and the best when the list is almost
sorted.
March 5, 2020
+ 22
Merge Sort
Merge Sort
March 5, 2020
+ 24
Merge Sort
March 5, 2020
+ 25
March 5, 2020
+ Quick Sort 26
March 5, 2020
+ Quick Sort 27
◼E elements equal x x
◼G elements greater than x
L E G
◼ Recursively: sort L and G
March 5, 2020
+ 28
March 5, 2020
+ 29
End of Sorting – 09
March 5, 2020