DSA Ch10 11 Sorting
DSA Ch10 11 Sorting
Chapter 10
Sorting Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Data Structures and Algorithms Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Luong The Nhan
Devide-and-
Faculty of Computer Science and Engineering Conquer
Quick Sort
University of Technology, VNU-HCM Merge Sort
10.1
Sorting
Outcomes
Luong The Nhan
• L.O.6.1 - Depict the working steps of sorting
algorithms step-by-steps.
• L.O.6.2 - Describe sorting algorithms by using
pseudocode.
• L.O.6.3 - Implement sorting algorithms using C/C++ . Sorting concepts
Insertion Sort
• L.O.6.4 - Analyze the complexity and develop Straight Insertion Sort
Selection Sort
• L.O.6.5 - Use sorting algorithms for problems in Straight Selection Sort
Heap Sort
real-life. Exchange Sort
• L.O.8.4 - Develop recursive implementations for Bubble Sort
Devide-and-
methods supplied for the following structures: list, tree, Conquer
heap, searching, and graphs. Quick Sort
Merge Sort
1 Sorting concepts
2 Insertion Sort
Straight Insertion Sort
Shell Sort Sorting concepts
Insertion Sort
Straight Insertion Sort
3 Selection Sort Shell Sort
Exchange Sort
4 Exchange Sort Bubble Sort
Devide-and-
Bubble Sort Conquer
Quick Sort
Merge Sort
5 Devide-and-Conquer
Quick Sort
Merge Sort
10.3
Sorting
Sorting concepts
Insertion Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.4
Sorting
Sorting
Luong The Nhan
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.5
Sorting
Sorting
Luong The Nhan
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.6
Sorting
Sorting
Luong The Nhan
Sorting concepts
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.7
Sorting
Sorting
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.8
Sorting
Sorting concepts
Insertion Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.9
Sorting
Straight Insertion Sort
Luong The Nhan
Insertion Sort
unsorted sublist is inserted into the sorted Straight Insertion Sort
Shell Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.10
Sorting
Straight Insertion Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.11
Sorting
Straight Insertion Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.12
Sorting
Straight Insertion Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.13
Sorting
Straight Insertion Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.14
Sorting
Straight Insertion Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.15
Sorting
Straight Insertion Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.16
Sorting
Straight Insertion Sort
Luong The Nhan
Algorithm InsertionSort()
Sorts the contiguous list using straight insertion sort.
Selection Sort
while walker >= 0 AND temp.key < Straight Selection Sort
Exchange Sort
data[walker+1] = data[walker] Bubble Sort
current = current + 1
end
end
End InsertionSort 10.17
Sorting
Shell Sort
Luong The Nhan
elements. Devide-and-
Conquer
• Segments are dispersed throughout the list. Quick Sort
Merge Sort
10.18
Sorting
Shell Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.19
Sorting
Shell Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
• For the value of K in each iteration, sort Straight Selection Sort
Heap Sort
Devide-and-
• After each iteration, K is reduced until it is Conquer
Quick Sort
10.20
Sorting
Example of Shell Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.21
Sorting
Example of Shell Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.22
Sorting
Choosing incremental values
Luong The Nhan
Insertion Sort
Straight Insertion Sort
Selection Sort
of each other, other wise, the same keys Straight Selection Sort
Heap Sort
10.23
Sorting
Choosing incremental values
Luong The Nhan
Selection Sort
Straight Selection Sort
Heap Sort
ki−1 = 2 ∗ ki + 1
t = | log2 n| − 1
10.24
Sorting
Shell Sort
Luong The Nhan
Algorithm ShellSort()
Sorts the contiguous list using Shell sort.
Selection Sort
while segment <= k do Straight Selection Sort
Heap Sort
end Conquer
Quick Sort
Merge Sort
k = next_incremental_value
end
End ShellSort
10.25
Sorting
Shell Sort
Algorithm SortSegment(val segment <int>, val k Luong The Nhan
<int>)
Sorts the segment beginning at segment using insertion
sort, step between elements in the segment is k.
Insertion Sort
while current < count do Straight Insertion Sort
Selection Sort
walker = current - k Straight Selection Sort
Exchange Sort
data[walker].key do Bubble Sort
data[walker + k] = temp
current = current + k
end
End SortSegment
10.26
Sorting
Insertion Sort Efficiency
Luong The Nhan
Insertion Sort
f (n) = n(n + 1)/2 = O(n2 ) Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
• Shell sort: Heap Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.27
Sorting
Sorting concepts
Insertion Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.28
Sorting
Selection Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
In each pass, the smallest/largest item is Selection Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.29
Sorting
Straight Selection Sort
Luong The Nhan
Insertion Sort
smallest element is selected and exchanged Straight Insertion Sort
Shell Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.30
Sorting
Straight Selection Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.31
Sorting
Straight Selection Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.32
Sorting
Straight Selection Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.33
Sorting
Straight Selection Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.34
Sorting
Straight Selection Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.35
Sorting
Straight Selection Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.36
Sorting
Straight Selection Sort
Luong The Nhan
Algorithm SelectionSort()
Sorts the contiguous list using straight selection sort.
current = 0
while current < count - 1 do Sorting concepts
smallest = current Insertion Sort
walker = current + 1 Straight Insertion Sort
Shell Sort
Devide-and-
walker = walker + 1 Conquer
end Quick Sort
Merge Sort
swap(current, smallest)
current = current + 1
end
End SelectionSort
10.37
Sorting
Heap Sort
Luong The Nhan
Selection Sort
Straight Selection Sort
Heap Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.38
Sorting
Heap Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.39
Sorting
Heap Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.40
Sorting
Heap Sort
Luong The Nhan
Algorithm HeapSort()
Sorts the contiguous list using heap sort.
position = count/2 - 1
Sorting concepts
while position >= 0 do
Insertion Sort
ReheapDown(position, count - 1) Straight Insertion Sort
Selection Sort
end Straight Selection Sort
Exchange Sort
while last > 0 do Bubble Sort
end
End HeapSort
10.41
Sorting
Selection Sort Efficiency
Luong The Nhan
Insertion Sort
O(n2 ) Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
• Heap sort: Heap Sort
Exchange Sort
O(nlog2 n) Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.42
Sorting
Sorting concepts
Insertion Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.43
Sorting
Exchange Sort
Luong The Nhan
Sorting concepts
Selection Sort
Straight Selection Sort
Heap Sort
• Exchange is extensively used. Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.44
Sorting
Bubble Sort
Luong The Nhan
Insertion Sort
bubbled from the unsorted sublist and Straight Insertion Sort
Shell Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.45
Sorting
Bubble Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.46
Sorting
Bubble Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.47
Sorting
Bubble Sort
Luong The Nhan
Algorithm BubbleSort()
Sorts the contiguous list using bubble sort.
current = 0
flag = False
Sorting concepts
while current < count AND flag = False do
Insertion Sort
walker = count - 1 Straight Insertion Sort
Selection Sort
while walker > current do Straight Selection Sort
Exchange Sort
flag = False Bubble Sort
end
current = current + 1
end
End BubbleSort 10.48
Sorting
Exchange Sort Efficiency
Luong The Nhan
Sorting concepts
Insertion Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.49
Sorting
Sorting concepts
Insertion Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.50
Sorting
Devide-and-Conquer Sort
Luong The Nhan
Algorithm DevideAndConquer()
if the list has length > 1 then Sorting concepts
Selection Sort
highlist.DevideAndConquer() Straight Selection Sort
Heap Sort
end Devide-and-
Conquer
End DevideAndConquer Quick Sort
Merge Sort
10.51
Sorting
Devide-and-Conquer Sort
Luong The Nhan
Sorting concepts
Selection Sort
Quick Sort hard easy Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.52
Sorting
Quick Sort
Luong The Nhan
Insertion Sort
Sorts the contiguous list using quick sort. Straight Insertion Sort
Shell Sort
Selection Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.53
Sorting
Quick Sort
Algorithm recursiveQuickSort(val left <int>, Luong The Nhan
Selection Sort
Straight Selection Sort
Exchange Sort
pivot_position = Partition(left, right) Bubble Sort
Devide-and-
recursiveQuickSort(left, pivot_position - 1) Conquer
Quick Sort
right)
end
End recursiveQuickSort 10.54
Sorting
Quick Sort
Luong The Nhan
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.55
Sorting
Quick Sort Efficiency
Luong The Nhan
Sorting concepts
Insertion Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.56
Sorting
Merge Sort
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.57
Sorting
Merge Sort
Luong The Nhan
Insertion Sort
Sorts the linked list using merge sort. Straight Insertion Sort
Shell Sort
Selection Sort
recursiveMergeSort(head) Straight Selection Sort
Heap Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.58
Sorting
Merge Sort
Luong The Nhan
Insertion Sort
if sublist is not NULL AND sublist->link is not Straight Insertion Sort
Shell Sort
recursiveMergeSort(second_list) Devide-and-
Conquer
end
End recursiveMergeSort
10.59
Sorting
Merge Sort
Luong The Nhan
second_list = midpoint->link
midpoint->link = NULL
End Divide
10.60
Sorting
Merge two sublists
Luong The Nhan
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Heap Sort
Exchange Sort
Bubble Sort
Devide-and-
Conquer
Quick Sort
Merge Sort
10.61
Sorting
Merge two sublists
Luong The Nhan
Algorithm Merge(ref first <pointer>, ref second
<pointer>)
Merges two sorted lists into a sorted list.
Selection Sort
lastSorted = first Straight Selection Sort
end
end
// ...
10.62
Sorting
Merge two sublists
Luong The Nhan
// ...
Selection Sort
else Straight Selection Sort
Heap Sort
end
Bubble Sort
Devide-and-
End Merge
10.63