DSA Chapter 07 (Sorting)
DSA Chapter 07 (Sorting)
Manish Aryal
Sorting
8 23 32 45 56 78
2
General Sort Concepts
3
General Sort Concepts
Sorts
Internal External
• Natural
• Balanced
Insertion Selection Exchange • Polyphase
4
General Sort Concepts
8 8 32 45 56 78
5
General Sort Concepts
Sort efficiency: a measure of the relative
efficiency of a sort = number of comparisons + number
of moves
Sort pass: each traversal of the data being
sorted
6
Insertion Sorts
7
Straight Insertion Sort
The list is divided into two parts: sorted and
unsorted.
unsorted
8
Straight Insertion Sort
23 78 45 8 32 56
9
Straight Insertion Sort
23 78 45 8 32 56
23 78 45 8 32 56
10
Straight Insertion Sort
23 78 45 8 32 56
23 78 45 8 32 56
23 45 78 8 32 56
11
Straight Insertion Sort
23 78 45 8 32 56
23 78 45 8 32 56
23 45 78 8 32 56
8 23 45 78 32 56
12
Straight Insertion Sort
23 78 45 8 32 56
23 78 45 8 32 56
23 45 78 8 32 56
8 23 45 78 32 56
8 23 32 45 78 56
13
Straight Insertion Sort
23 78 45 8 32 56
23 78 45 8 32 56
23 45 78 8 32 56
8 23 45 78 32 56
8 23 32 45 78 56
8 23 32 45 56 78
14
Straight Insertion Sort
Algorithm insertionSort (ref list <array>, val last <index>)
Sorts list[1..last] using straight insertion sort
Pre list contains at least one element
last is index to last element in the list
Post list has been rearranged
1 current = 2
2 loop (current <= last)
1 hold = list[current]
2 walker = current - 1
3 loop (walker >= 1 AND hold.key < list[walker].key)
1 list[walker + 1] = list[walker]
2 walker = walker - 1
4 list[walker + 1] = hold
5 current = current + 1
3 return
End insertionSort
15
Shell Sort
16
Shell Sort
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
K=3
Segment 1
[2] [2 + K] [2 + 2*K]
Segment 2
[3] [3 + K] [3 + 2*K]
Segment 3
17
Shell Sort
K=3 25 57 48 37 12 92 86 33
[1] [1 + K] [1 + 2*K]
Segment 1 25 37 86
[2] [2 + K] [2 + 2*K]
Segment 2 57 12 33
[3] [3 + K] [3 + 2*K]
Segment 3 48 92
18
Shell Sort
19
Shell Sort
Algorithm shellSort (ref list <array>, val last <index>)
Sorts list[1..last] using shell sort
Pre list must contain at least one element
last is index to last element in the list
Post list has been rearranged
1 K = last/2
2 loop (K not 0)
1 seg = 1
2 loop (seg <= K)
1 sortSegment (seg)
2 seg = seg + 1
3 K = K/2
3 return
End shellSort
20
Shell Sort
Algorithm shellSort (ref list <array>, val last <index>)
Sorts list[1..last] using shell sort
Pre list must contain at least one element
last is index to last element in the list
Post list has been rearranged
1 K = last/2
2 loop (K not 0)
1 seg = 1
2 loop (seg <= K)
1 current = seg + K
2 loop (current <= last)
1 hold = list[current]
2 walker = current - K
3 loop (walker >= 1 AND hold.key < list[walker].key)
1 list[walker + K] = list [walker]
2 walker = walker - K
4 list[walker + K] = hold
5 current = current + K
3 seg = seg + 1
3 K = K/2
3 return
End shellSort 21
Insertion Sort Efficiency
22
Insertion Sort Efficiency
Shell sort:
23
General Sort Concepts
Sorts
Internal External
• Natural
• Balanced
Insertion Selection Exchange • Polyphase
24
Selection Sorts
25
Straight Selection Sort
unsorted
26
Straight Selection Sort
23 78 45 8 32 56
27
Straight Selection Sort
23 78 45 8 32 56
8 78 45 23 32 56
28
Straight Selection Sort
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
29
Straight Selection Sort
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
8 23 32 78 45 56
30
Straight Selection Sort
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
8 23 32 78 45 56
8 23 32 45 78 56
31
Straight Selection Sort
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 56 78
32
Straight Selection Sort
Algorithm selectionSort (ref list <array>, val last <index>)
Sorts list[1..last] using straight selection sort
Pre list contains at least one element
last is index to last element in the list
Post list has been rearranged
1 current = 1
2 loop (current < last)
1 smallest = current
2 walker = current + 1
3 loop (walker <= last)
1 if (list[walker] < list[smallest])
1 smallest = walker
2 walker = walker + 1
4 exchange (list, current, smallest)
5 current = current + 1
3 return
End selectionSort
33
Heap Sort
A heap is a binary tree structure such that:
The tree is complete or nearly complete
The key value of each node is >= the key value of each of
its descendents (max-heap)
The unsorted sublist is organized into a heap.
In each pass, in the unsorted sublist, the largest
element is selected and exchanged with the last
element.
Then the heap is reheaped.
heap
34
Heap Sort
23 78 45 8 32 56
23
78
[0] 45
[1] [2]
8 32 56
35
Heap Sort
23 78 45 8 32 56 78 32 56 8 23 45
build heap
23 78
78
[0] 45 32
[0] 56
36
Heap Sort
78 32 56 8 23 45 45 32 56 8 23 78
78 45
[0] 32
[0] 56
32 56
37
Heap Sort
Algorithm heapSort (ref heap <array>, val last <index>)
Sorts list[0..last] using heap sort
Pre array is filled
last is index to last element in the list
Postarray has been sorted
Creat a heap
1 walker = 1
2 loop (walker <= last)
1 reheapUp (heap, walker)
2 walker = walker + 1
Sort the list
3 sorted = last
4 loop (sorted > 0)
1 exchange (heap, 0, sorted)
2 sorted = sorted - 1
3 reheapDown (heap, 0, sorted)
5 return
End heapSort 38
Selection Sort Efficiency
O(n2)
Heap sort:
O(n log2n)
39
General Sort Concepts
Sorts
Internal External
• Natural
• Balanced
Insertion Selection Exchange • Polyphase
40
Exchange Sorts
41
Bubble Sort
unsorted
42
Bubble Sort
23 78 45 8 56 32
43
Bubble Sort
23 78 45 8 56 32
23 78 45 8 32 56
23 78 45 8 32 56
23 78 8 45 32 56
23 8 78 45 32 56
8 23 78 45 32 56
44
Bubble Sort
Algorithm bubbleSort (ref list <array>, val last <index>)
Sorts list[1..last] using bubble sort
Pre list must contain at least one element
last is index to last element in the list
Post list has been rearranged
1 current = 1
2 sorted = false
3 loop (current <= last AND sorted false)
1 walker = last
2 sorted = true
3 loop (walker > current)
1 if (list[walker] < list[walker - 1])
1 sorted = false
2 exchange (list, walker, walker - 1)
2 walker = walker -1
4 current = current + 1
4 return
End bubbleSort
45
Quick Sort
Developed by C. A. Hoare (1962).
<k >k
46
Quick Sort
Pivot selection:
C. A. Hoare (1962): the first element in the list.
R. C. Singleton (1969): the median of the left, right and
middle elements of the list.
Pivot location:
Use left and right walls.
Exchange the two elements at the left and right wall
positions if they are out of order with respect to the pivot.
47
Quick Sort
pivot
pivot
48
Quick Sort
49
Quick Sort
D.E. Knuth suggested that when the sort
partitions becomes small, straight insertion sort
should be used to complete the sorting.
pivot
k
<k >k
50
Quick Sort
Algorithm quickSort (ref list <array>, val left <index>, val right <index>)
Sorts list[left..right] using quick sort
Pre list must contain at least one element
left and right are indices to first and last elements in the list
Post list has been rearranged
1 if (right - left > minsize)
quick sort
1 medianLeft (list, left, right)
2 pivot = list[left]
3 sortLeft = left + 1
4 sortRight = right
5 loop (sortLeft <= sortRight)
Find key on left that belongs on right
1 loop (list[sortLeft].key < pivot.key)
1 sortLeft = sortLeft + 1
Find key on right that belongs on left
1 loop (list[sortRight].key >= pivot.key)
1 sortRight = sortRight - 1
51
Quick Sort
Find key on left that belongs on right
1 loop (list[sortLeft].key < pivot.key)
1 sortLeft = sortLeft + 1
Find key on right that belongs on left
2 loop (list[sortRight].key >= pivot.key)
1 sortRight = sortRight - 1
3 if (sortLeft <= sortRight)
1 exchange (list, sortLeft, sortRight)
2 sortLeft = sortLeft + 1
3 sortRight = sortRight - 1
Prepare for next phase
6 list[left] = list[sortLeft - 1]
7 list[sortLeft - 1] = pivot
8 if (left < sortRight)
1 quickSort (list, left, sortRight - 1)
9 if (sortLeft < right)
1 quickSort (list, sortLeft, right )
2 else
1 insertionSort (list, left, right)
End quickSort
52
Quick Sort
Algorithm medianLeft (ref sortData <array>, val left <index>, val right <index>)
Finds the median of an array sortData[left..right], and places it in the location sortData[left]
Pre sortData is an array of at least three elements
left and right are the boundaries of the array
Post median value located and placed at sortData[left]
1 mid = (left + right)/2
2 if (sortData[left].key > sortData[mid].key)
1 exchange (sortData, left, mid)
3 if (sortData[left].key > sortData[right].key)
1 exchange (sortData, left, right)
4 if (sortData[mid].key > sortData[right].key)
1 exchange (sortData, mid, right)
5 exchange (sortData, left, mid)
6 return
End medianLeft
53
Exchange Sort Efficiency
Bubble sort:
f(n) = n(n + 1)/2 = O(n2)
Quick sort:
O(n log2n)
54
General Sort Concepts
Sorts
Internal External
• Natural
• Balanced
Insertion Selection Exchange • Polyphase
55
External Sorts
56
Merging Ordered Files
File 3
File 1 1 File 2
1 2 2
3 3 4
5 4 6
5 8
6 10
8
10
57
Merging Ordered Files
Algorithm mergeFiles
Merges two files into one file
Pre Input files are ordered
Post Input files sequentially combined in output file
1 open files
2 read (file1 into record1)
3 read (file2 into record2)
4 loop (not end file1 OR not end file2)
1 if (record1.key <= record2.key)
1 write (file3 from record1)
2 read (file1 into record1)
3 if (end of file1)
1 record1.key = +
2 else
1 write (file3 from record2)
2 read (file2 into record2)
3 if (end of file2)
1 record2.key = +
5 close files
End mergeFiles
58
File Sorting Process
Sort phase.
Merge phase.
59
Sort Phase
2,300 records
Input file
Sort
Merge 1
501-1,000 1,501-2,000
Merge 2
60
Merge Phase
Natural merge
Balanced merge
Polyphase merge
61
Polyphase Merge input
mrg
records 1-1000
3 records 1,001-2000
records 2.001-2,300
distribution
mrg mrg records 1,001-2,000
records 1-1000 1 2
records 2,001-2,300 merge
distribution
mrg mrg
records 1-2,000 records 2,001-2,300
1 2
merge
mrg
3 records 1-2,300 62
Polyphase Merge
input
mrg mrg
records 1-2,000 2 records 2,001-2,300
1
merge
mrg
3 records 1-2,300
63
Polyphase Merge
input
records 501-1000
records 1-500 sort phase
mrg records 1,501-2000
records 1,001-1,500 mrg
1 2
records 2.001-2,300 merge
records 1-1000
mrg records 1,001-2000
3
mrg
records 1-1000
3 records 1,001-2,000
mrg
3 records 1,001-2,000 64