Sorting: Gordon College
Sorting: Gordon College
Gordon College
1
Sorting
• Consider a list
x1, x2, x3, … xn
• We seek to arrange the elements of the
list in order
– Ascending or descending
• Some O(n2) schemes
– easy to understand and implement
– inefficient for large data sets
2
Categories of Sorting Algorithms
1. Selection sort
– Make passes through a list
– On each pass reposition correctly
some element
Look for smallest in list and replace 1st element, now start the
process over with the remainder of the list
3
Selection
Recursive Algorithm
If the list has only 1 element ANCHOR
stop – list is sorted
Else do the following:
a. Find the smallest element and place in front
b. Sort the rest of the list
4
Categories of Sorting Algorithms
2. Exchange sort
– Systematically interchange pairs of elements
which are out of order
– Bubble sort does this
5
Bubble Sort Algorithm
1. Initialize numCompares to n - 1
2. While numCompares!= 0, do following
a. Set last = 1 // location of last element in a swap
b. For i = 1 to numPairs
if xi > xi + 1
Swap xi and xi + 1 and set last = i
c. Set numCompares = last – 1
End while
6
Bubble Sort Algorithm
1. Initialize numCompares to n - 1
2. While numCompares!= 0, do following
a. Set last = 1 // location of last element in a swap
b. For i = 1 to numPairs
if xi > xi + 1
Swap xi and xi + 1 and set last = i
c. Set numCompares = last – 1
End while
45 67 12 34 25 39 45 12 34 25 39 67 Allows it to quit if
45 12 67 34 25 39 12 45 34 25 39 67 In order
45 12 34 67 25 39 12 34 45 25 39 67 Try: 23 12 34 45 67
45 12 34 25 67 39 12 34 25 45 39 67
45 12 34 25 39 67 12 34 25 39 45 67
… Also allows us to
Label the highest as
sorted 7
Categories of Sorting Algorithms
3. Insertion sort
– Repeatedly insert a new element into an already
sorted list
8
Example of Insertion Sort
• Given list to be sorted
67, 33, 21, 84, 49, 50, 75
– Note sequence of steps carried out
9
Improved Schemes
• These 3 sorts - have computing time O(n2)
• We seek improved computing times for sorts of large data
sets
• There are sorting schemes which can be proven to have
average computing time
O( n log2n )
10
Comparisons of Sorts
• Sort of a randomly generated list of 500 items
– Note: times are on 1970s hardware
Algorithm Type of Sort Time (sec)
•Simple selection Selection 69
•Heapsort Selection 18
•Bubble sort Exchange 165
•2 way bubble sort Exchange 141
•Quicksort Exchange 6
•Linear insertion Insertion 66
•Binary insertion Insertion 37
•Shell sort Insertion 11
11
Indirect Sorts
• What happens if items being sorted are
large structures (like objects)?
– Data transfer/swapping time unacceptable
• Alternative is indirect sort
– Uses index table to store positions of the objects
– Manipulate the index table for ordering
12
Heaps
A heap is a binary tree with properties: A
1. It is complete
B C
D
E F G
H I J
13
Heaps
Which of the following are heaps?
A B C
14
Maximum and Minimum Heaps Example
55 40
50 52 15 30
25 10 11 5 10
20 22
5 10
10 50 15 30
11 20 52 55 40
25 22
16
Implementing a Heap
• Note the placement of the nodes in the
array
41
17
Implementing a Heap
18
Basic Heap Operations
• Constructor
– Set mySize to 0, allocate array (if dynamic array)
• Empty
– Check value of mySize
• Retrieve max item
– Return root of the binary tree, myArray[1]
19
Basic Heap Operations
• Delete max item (popHeap)
– Max item is the root, replace with last node in
tree
Result
Resultcalled
calledaa
semiheap
semiheap
63 18
v[0] v[0]
30 40 30 40
21
Adjusting the heap for popHeap()
40 40
v[0] v[0]
... 18 ... 38
v[2] v[2]
8 38 8 18
22
Percolate Down Algorithm
converts semiheap to heap
r = current root node
63 63
v[0] v[0]
30 40 30 40
25
Reordering the tree for the insertion
63 63
63
v[0] v[0]
30 ... 50 ... v[0]
v[1]
50 ...
v[1]
... 50 ... v[1]
30
... 30
v[4] v[4]
v[4]
18 25 18 25
18 25
v[9] v[10] v[9] v[10]
v[9] v[10]
Step 1 Compare 50 and 25 Step 2 Compare 50 and 30 Step 3 Compare 50 and 63
(Exchange v[10] and v[4]) (Exchange v[4] and v[1]) (50 in correct location)
26
Heapsort
• Given a list of numbers in an array
– Stored in a complete binary tree
12 17
30 50 20 60
65 4 19
Initial Vector
28
Example of Heapifying a Vector
9
12 17
30 50 20 60
65 4 19
29
Example of Heapifying a Vector
9
12 17
65 50 20 60
30 4 19
12 60
65 50 20 17
30 4 19
31
Example of Heapifying a Vector
9
12 60
9
65 50 20 17
30 4 19
30 50 20 17
12 4 19
65 60
30 50 20 17
65
12 4 19
30 19 20 17
12 4 9
34
Heapsort
• Now swap element 1 (root of tree) with last
element
35
Heapsort
• Again swap root with rightmost leaf
60
60
36
Heapsort Algorithm
1. Consider x as a complete binary tree, use
heapify to convert this tree to a heap
2. for i = n down to 2:
a. Interchange x[1] and x[i]
(puts largest element at end)
b. Apply percolate_down to convert binary
tree corresponding to sublist in
x[1] .. x[i-1]
37
Example of Implementing heap sort
int arr[] = {50, 20, 75, 35, 25};
vector<int> v(arr, 5);
75
35 50
20 25
Heapified Tree
38
Example of Implementing heap sort
Calling popHeap() with last = 5 Calling popHeap() with last = 4
deletes 75 and stores it in h[4] deletes 50 and stores it in h[3]
50 35
35 25 20 25
20 75 50 75
39
Example of Implementing heap sort
Calling popHeap() with last = 3 Calling popHeap() with last = 2
deletes 35 and stores it in h[2] deletes 25 and stores it in h[1]
25 20
20 35 25 35
50 75 50 75
40
Heap Algorithms in STL
• Found in the <algorithm> library
– make_heap() heapify
– push_heap() insert
– pop_heap() delete
– sort_heap() heapsort
41
Priority Queue
• A collection of data elements
– Items stored in order by priority
– Higher priority items removed ahead of lower
Implementation ?
42
Implementation possibilities
list (array, vector, linked list)
insert – O(1)
remove max - O(n)
ordered list
insert - linear insertion sort O(n)
remove max - O(1)
– Heap (Best)
Basic operations have O(log2n) time
43
Priority Queue
Basic Operations
– Constructor
– Insert
– Find, remove smallest/largest (priority) element
– Replace
– Change priority
– Delete an item
– Join two priority queues into a larger one
44
Priority Queue
• STL priority queue adapter uses heap
priority_queue<BigNumber, vector<BigNumber> > v;
cout << "BIG NUMBER DEMONSTRATION" << endl;
for(int k=0;k<6;k++)
{
cout << "Enter BigNumber: "; cin >> a;
v.push(a);
}
cout<<"POP IN ORDER"<<endl;
while(!v.empty())
{
cout<<v.top()<<endl;
v.pop();
} 45
Quicksort
• More efficient exchange sorting scheme
– (bubble sort is an exchange sort)
• Typical exchange: involves elements that are far apart
Fewer interchanges are required to correctly position an
element.
• Quicksort uses a divide-and-conquer strategy
A recursive approach:
– The original problem partitioned into simpler
sub problems
– Each sub problem considered independently.
• Subdivision continues until sub problems
obtained are simple enough to be solved directly
46
Quicksort
Basic Algorithm
• Choose an element - pivot
• Perform sequence of exchanges so that
<elements less than P> <P> <elements greater than P>
– All elements that are less than this pivot are to its left and
– All elements that are greater than the pivot are to its right.
47
Quicksort
recursive
48
Quicksort Example
• Given to sort:
75, 70, 65, 84 , 98, 78, 100, 93, 55, 61, 81, 68
49
Quicksort Example
75, 70, 65, 68, 61, 55, 100, 93, 78, 98, 81, 84
50
Quicksort Example
• Need to sort (independently):
55, 70, 65, 68, 61 and
100, 93, 78, 98, 81, 84
• Let pivot be 55, look from each end for
values larger/smaller than 55, swap
• Same for 2nd list, pivot is 100
• Sort the resulting sublists in the same
manner until sublist is trivial (size 0 or 1)
51
QuickSort Recursive Function
template <typename ElementType>
void quicksort (ElementType x[], int first int last)
{
int pos; // pivot's final position
if (first < last) // list size is > 1
{
split(x, first, last, pos); // Split into 2 sublists
quicksort(x, first, pos - 1); // Sort left sublist
quicksort(x,pos + 1, last); // Sort right sublist
}
} 23 45 12 67 32 56 90 2 15
52
template <typename ElementType>
void split (ElementType x[], int first, int last, int & pos)
{
ElementType pivot = x[first]; // pivot element
int left = first, // index for left search
right = last; // index for right search
while (left < right)
{
while (pivot < x[right]) // Search from right for
right--; // element <= pivot
// Search from left for
while (left < right && // element > pivot
x[left] <= pivot)
left++;
• Visual example of
a quicksort on an array
etc. …
54
QuickSort Example
v = {800, 150, 300, 650, 550, 500, 400, 350, 450, 900}
pivot
500 150 300 650 550 800 400 350 450 900
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
scanUp scanDown
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
scanUp scanDown
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
scanUp scanDown
56
QuickSort Example
Before the exchange
pivot
500 150 300 450 550 800 400 350 650 900
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
scanUp scanDown
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
scanUp scanDown
57
QuickSort Example
pivot Before the exchange
500 150 300 450 350 800 400 550 650 900
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
scanUp scanDown
500 150 300 450 350 400 800 550 650 900
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
scanDown scanUp
58
QuickSort Example
Pivot in its final position
400 150 300 450 350 500 800 550 650 900
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
400 150 300 450 350 500 800 550 650 900
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
v[0] v[1] v[2] v[3] v[4] v[0] v[1] v[2] v[3] v[4]
150 300 350 400 450 500 550 650 800 900
v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] v[8] v[9]
62
Quicksort Performance
• O(n log2n) is the average case computing
time
– If the pivot results in sublists of approximately
the same size.
64
Improvements to Quicksort
• Better method for selecting the pivot is the
median-of-three rule,
– Select the median (middle value) of the first, middle, and
last elements in each sublist as the pivot.
(4 10 6) - median is 6
66
Improvements to Quicksort
• Another improvement aimed at reducing the
overhead of recursion is to use an iterative
version of Quicksort()
67
Improvements to Quicksort
• For small files (n <= 20), quicksort is worse
than insertion sort;
– small files occur often because of recursion.
70
Merge Algorithm
1. Open File1 and File2 for input, File3 for output
2. Read first element x from File1 and
first element y from File2
3. While neither eof File1 or eof File2
If x < y then
a. Write x to File3
b. Read a new x value from File1
Otherwise
a. Write y to File3
b. Read a new y from File2
End while
4. If eof File1 encountered copy rest of of File2 into File3.
If eof File2 encountered, copy rest of File1 into File3
71
Binary Merge Sort
• Given a single file
72
Binary Merge Sort
• Merge first one-element "subfile" of F1 with
first one-element subfile of F2
– Gives a sorted two-element subfile of F
73
Binary Merge Sort
• Split again
• Merge again as before
74
Binary Merge Sort
• Last splitting gives two files each in order
Note
Notewe wealways
alwaysareare
limited
limitedtotosubfiles
subfiles of
of
• Last merging yields a single file,
some
some entirely
power
power of 22 in
of
order
75
Natural Merge Sort
• Allows sorted subfiles of other sizes
– Number of phases can be reduced when file
contains longer "runs" of ordered elements
• Consider file to be sorted, note in order
groups
76
Natural Merge Sort
• Copy alternate groupings into two files
– Use the sub-groupings, not a power of 2
77
Natural Merge Sort
• Merge the corresponding sub files
EOF
EOFforfor F2,
F2, Copy
Copy
remaining
remaininggroups
groupsfrom
from F1
F1
78
Natural Merge Sort
• Split again,
alternating groups
• Merge again, now two subgroups
79
Natural Merge Sort
Split algorithm for natural merge sort
1. Open F for input and F1 and F2 for output
2. While the end of F has not been reached:
a. Copy a sorted subfile of F into F1 as follows: repeatedly
read an element of F and write it to F1 until the next
element in F is smaller than this copied item or the end
of F is reached.
b. If the end of F has not been reached, copy the next
sorted subfile of F into F2 using the method above.
End while.
80
Natural Merge Sort
Merge algorithm for natural merge sort
1. Open F1 and F2 for input, F for output.
2. Initialize numSubfiles to 0
3. While not eof F1 or not eof F2
a. While no end of subfile in F1 or F2 has been reached:
If the next element in F1 is less than the next element in F2
Copy the next element from F1 into F.
Else
Copy the next element from F2 into F.
End While
b. If the eof F1 has been reached
Copy the rest of subfile F2 to F.
Else
Copy the rest of subfile F1 to F.
c. Increment numSubfiles by 1.
End While
4. Copy any remaining subfiles to F, incrementing numSubfiles by 1 for
each.
81
Natural Merge Sort
Mergesort algorithm
Repeat the following until numSubfiles is equal to 1:
1. Call the Split algorithm to split F into files F1 and
F2.
2. Call the Merge algorithm to merge corresponding
subfiles in F1 and F2 back into F.
sublist A sublist B
7 10 19 25 12 17 21 30 48
first mid last
83
Natural MergeSort Review
tempVector
sublist A sublist B
7 10 19 25 12 17 21 30 48
indexA indexB
tempVector
7 10
sublist A sublist B
7 10 19 25 12 17 21 30 48
indexA indexrB
84
Natural MergeSort Review
tempVector
7 10 12
sublist A sublist B
7 10 19 25 12 17 21 30 48
indexA indexB
So forth and so on…
tempVector
7 10 12 17 19 21 25
sublist A sublist B
7 10 19 25 12 17 21 30 48
indexA indexB
85
Natural MergeSort Review
tempVector
7 10 12 17 19 21 25 30 48
sublist A sublist B
7 10 19 25 12 17 21 30 48
last
indexA indexB
tempVector
7 10 12 17 19 21 25 30 48
7 10 12 17 19 21 25 30 48
first last
86
Recursive Natural MergeSort
(25 10 7 19 3 48 12 17 56 30 21)
[3 7 10 12 17 19 21 25 30 48 56]
(25 10 7 19 3 ) (48 12 17 56 30 21 )
[3 7 10 19 25] [12 17 21 30 48 56]
87
Recursive Natural MergeSort
Call msort()
- recusive call1 to msort()
Level 0:
- recusive call2 to msort()
- call1 merge()
Level 2: msort(): n/4 msort (): n/4 msort(): n/4 msort(): n/4
Level 3: msort(): n/8 msort(): n/8 msort(): n/8 msort(): n/8 msort(): n/8 msort(): n/8 msort(): n/8 msort(): n/8
...
Level i:
88
Sorting Fact
• any algorithm which performs sorting using
comparisons cannot have a worst-case
performance better than O(n log n)
– a sorting algorithm based on comparisons
cannot be O(n) - even for its average runtime.
89
Radix Sort
• Based on examining digits in some base-b
numeric representation of items
• Least significant digit radix sort
– Processes digits from right to left
90
Radix Sort
Order ten 2 digit numbers in 10 bins from
smallest number to largest number. Requires 2
calls to the sort Algorithm.
Initial Sequence: 91 6 85 15 92 35 30 22 39
Pass 0: Distribute the cards into bins according
to the 1's digit (100).
35
22 15
30 91 92 85 6 39
0 1 2 3 4 5 6 7 8 9 91
Radix Sort
Final Sequence: 91 6 85 15 92 35 30 22 39
Pass 1: Take the new sequence and distribute
the cards into bins determined by the
10's digit (101).
39
35 92
6 15 22 30 85 91
0 1 2 3 4 5 6 7 8 9
92
Sort Algorithm Analysis
• Selection Sort (uses a swap)
– Worst and average case O(n^2)
– can be used with linked-list (doesn’t require
random-access data)
– Can be done in place
– not at all fast for nearly sorted data
93
Sort Algorithm Analysis
• Bubble Sort (uses an exchange)
– Worst and average case O(n^2)
– Since it is using localized exchanges - can be
used with linked-list
– Can be done in place
– O(n^2) - even if only one item is out of place
94
Sort Algorithm Analysis
sorts actually used
• Merge Sort
– Worst and average case O(n log n)
– Does not require random-access data
– For linked-list - can be done in place
– For an array - need to use a buffer
– It is not significantly faster on nearly sorted data
(but it is still log-linear time)
96
Sort Algorithm Analysis
sorts actually used
• QuickSort
– Worst O(n^2)
– Average case O(n log n) [good time]
– can be done in place
– Additional space for recursion O(log n)
– Can be slow O(n^2) for nearly sorted or reverse
data.
– Sort used for STL sort()
97
End of sorting
98