Professional Documents
Culture Documents
201 DSA Chapter 4 Sorting
201 DSA Chapter 4 Sorting
Dept. Computer
Science
Insertion Sort
Straight Insertion Sort
Shell Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Dept. Computer Science Quick Sort
Sorting.1
Sorting
Overview
Dept. Computer
Science
1 Sorting concepts
2 Insertion Sort
Straight Insertion Sort Sorting concepts
Insertion Sort
Shell Sort Straight Insertion Sort
Shell Sort
5 Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.2
Sorting
Course learning outcomes
Dept. Computer
Science
L.O.2 Manipulate basic data structures such as list, tree and graph Insertion Sort
Straight Insertion Sort
L.O.2.1 Describe and present basic data structures such as: array, Shell Sort
array, linked list, stack, queue, tree, and graph Exchange Sort
Bubble Sort
Sorting.3
Sorting
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.4
Sorting
Sorting
Dept. Computer
Science
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.5
Sorting
Sorting
Dept. Computer
Science
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.6
Sorting
Sorting
Dept. Computer
Science
Sorting concepts
Insertion Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.7
Sorting
Sorting
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.8
Sorting
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.9
Sorting
Straight Insertion Sort
Dept. Computer
Science
Selection Sort
sublist. Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.10
Sorting
Straight Insertion Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.11
Sorting
Straight Insertion Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.12
Sorting
Straight Insertion Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.13
Sorting
Straight Insertion Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.14
Sorting
Straight Insertion Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.15
Sorting
Straight Insertion Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.16
Sorting
Straight Insertion Sort
1 Algorithm InsertionSort() Dept. Computer
Science
2 Sorts the contiguous list using straight insertion
sort.
data[walker].key do Devide-and-Conquer
Quick Sort
10 walker = walker - 1
11 end
12 data[walker+1] = temp
13 current = current + 1
14 end
15 end Sorting.17
Sorting
Shell Sort
Dept. Computer
Science
Selection Sort
increment). Straight Selection Sort
Exchange Sort
• Each segment contains N/K or more Bubble Sort
elements. Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.18
Sorting
Shell Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.19
Sorting
Shell Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
• For the value of K in each iteration, sort Straight Selection Sort
Exchange Sort
the K segments. Bubble Sort
Devide-and-Conquer
Quick Sort
Sorting.20
Sorting
Example of Shell Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.21
Sorting
Example of Shell Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.22
Sorting
Choosing incremental values
Dept. Computer
Science
Selection Sort
• Incremental values should not be Straight Selection Sort
Exchange Sort
multiples of each other, other wise, the Bubble Sort
Sorting.23
Sorting
Choosing incremental values
Dept. Computer
Science
Exchange Sort
Devide-and-Conquer
kt = 1
ki−1 = 2 ∗ ki + 1
t = | log2 n| − 1
Sorting.24
Sorting
Shell Sort
Dept. Computer
1 Algorithm ShellSort() Science
Insertion Sort
4 while k >= 1 do Straight Insertion Sort
Shell Sort
Devide-and-Conquer
9 end
10 k = next_incremental_value
11 end
12 End ShellSort
Sorting.25
Sorting
Shell Sort
1 Algorithm SortSegment(val segment <int>, val k Dept. Computer
Science
<int>)
2 Sorts the segment beginning at segment using
insertion sort, step between elements in the
segment is k. Sorting concepts
Insertion Sort
Straight Insertion Sort
3 current = segment + k Shell Sort
8 data[walker + k] = data[walker]
9 walker = walker - k
10 end
11 data[walker + k] = temp
12 current = current + k
13 end Sorting.26
Sorting
Insertion Sort Efficiency
Dept. Computer
Science
Sorting concepts
Selection Sort
Straight Selection Sort
Sorting.27
Sorting
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.28
Sorting
Selection Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.29
Sorting
Straight Selection Sort
Dept. Computer
Science
• The list is divided into two parts: sorted
and unsorted.
Sorting concepts
• In each pass, in the unsorted sublist, the Insertion Sort
Straight Insertion Sort
Selection Sort
exchanged with the first element. Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.30
Sorting
Straight Selection Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.31
Sorting
Straight Selection Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.32
Sorting
Straight Selection Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.33
Sorting
Straight Selection Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.34
Sorting
Straight Selection Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.35
Sorting
Straight Selection Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.36
Sorting
Straight Selection Sort
1 Algorithm SelectionSort() Dept. Computer
Science
2 Sorts the contiguous list using straight selection
sort.
then Devide-and-Conquer
Quick Sort
10 end
11 walker = walker + 1
12 end
13 swap(current, smallest)
14 current = current + 1
15 end Sorting.37
Sorting
Selection Sort Efficiency
Dept. Computer
Science
Sorting concepts
Insertion Sort
O(n2 )
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.38
Sorting
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.39
Sorting
Exchange Sort
Dept. Computer
Science
Sorting concepts
Selection Sort
sorted. Straight Selection Sort
Exchange Sort
Bubble Sort
Sorting.40
Sorting
Bubble Sort
Dept. Computer
Science
Selection Sort
moved to the sorted sublist. Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.41
Sorting
Bubble Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.42
Sorting
Bubble Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.43
Sorting
Bubble Sort
1 Algorithm BubbleSort() Dept. Computer
Science
2 Sorts the contiguous list using bubble sort.
3 current = 0
4 flag = False Sorting concepts
then Devide-and-Conquer
Quick Sort
11 swap(walker, walker - 1)
12 end
13 walker = walker - 1
14 end
15 current = current + 1
16 end Sorting.44
Sorting
Exchange Sort Efficiency
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.45
Sorting
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.46
Sorting
Devide-and-Conquer Sort
Dept. Computer
Science
1 Algorithm DevideAndConquer()
2 if the list has length > 1 then Sorting concepts
Selection Sort
Exchange Sort
5 highlist.DevideAndConquer() Bubble Sort
Devide-and-Conquer
6 combine(lowlist, highlist) Quick Sort
Merge Sort
7 end
8 End DevideAndConquer
Sorting.47
Sorting
Devide-and-Conquer Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Partition Combine Straight Insertion Sort
Shell Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.48
Sorting
Quick Sort
Dept. Computer
Science
Sorting concepts
1 Algorithm QuickSort() Insertion Sort
Straight Insertion Sort
Selection Sort
Straight Selection Sort
Sorting.49
Sorting
Quick Sort
Dept. Computer
1 Algorithm recursiveQuickSort(val left Science
Insertion Sort
in the list Straight Insertion Sort
Shell Sort
Exchange Sort
5 if left < right then Bubble Sort
Devide-and-Conquer
6 pivot_position = Partition(left, right) Quick Sort
Merge Sort
7 recursiveQuickSort(left,
pivot_position - 1)
8 recursiveQuickSort(pivot_position +
1, right)
9 end Sorting.50
Sorting
Quick Sort
Dept. Computer
Science
Insertion Sort
the entries in the list as the following figure: Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.51
Sorting
Quick Sort Efficiency
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.52
Sorting
Merge Sort
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.53
Sorting
Merge Sort
Dept. Computer
Science
Sorting concepts
1 Algorithm MergeSort() Insertion Sort
Straight Insertion Sort
Selection Sort
Straight Selection Sort
Sorting.54
Sorting
Merge Sort
Dept. Computer
1 Algorithm recursiveMergeSort(ref sublist Science
<pointer>)
2 Sorts the linked list using recursive merge
Sorting concepts
sort. Insertion Sort
Straight Insertion Sort
Shell Sort
Devide-and-Conquer
6 recursiveMergeSort(second_list)
7 Merge(sublist, second_list)
8 end
9 End recursiveMergeSort
Sorting.55
Sorting
Merge Sort
Dept. Computer
Science
1 Algorithm Divide(val sublist <pointer>, ref
second_list <pointer>)
2 Divides the list into two halves.
Sorting concepts
3 midpoint = sublist Insertion Sort
4 position = sublist->link Straight Insertion Sort
Shell Sort
5 while position is not NULL do Selection Sort
6 position = position->link Straight Selection Sort
10 end
11 end
12 second_list = midpoint->link
13 midpoint->link = NULL
14 End Divide
Sorting.56
Sorting
Merge two sublists
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Shell Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.57
Sorting
Merge two sublists
1 Algorithm Merge(ref first <pointer>, ref second Dept. Computer
Science
<pointer>)
2 Merges two sorted lists into a sorted list.
4 while first is not NULL AND second is not NULL Insertion Sort
Straight Insertion Sort
do Shell Sort
10 lastSorted->link = second
11 lastSorted = second
12 second = second->link
13 end
14 end
Sorting.58
Sorting
Merge two sublists
Dept. Computer
Science
1 // ...
Sorting concepts
2 if first is NULL then Insertion Sort
3 lastSorted->link = second Straight Insertion Sort
Shell Sort
8 first = combined.link
9 End Merge
Sorting.59
Sorting
Dept. Computer
Science
Sorting concepts
Insertion Sort
Straight Insertion Sort
Selection Sort
Straight Selection Sort
Exchange Sort
Bubble Sort
Devide-and-Conquer
Quick Sort
Merge Sort
Sorting.60