Data Structures: Sorting Algorithms
Data Structures: Sorting Algorithms
Sorting Algorithms
1
Sorting means . . .
The values stored in an array have
keys of a type for which the relational
operators are defined. (We also
assume unique keys.)
3
Selection Sort: Pass One
values [ 0 ] 36 U
[1] N
24 S
[2] O
10 R
[3]
6 T
E
[4] D
12
4
Selection Sort: End Pass One
values [ 0 ] 6 SORTED
[1] U
24
N
[2] S
10 O
[3] R
36 T
[4] E
12 D
5
Selection Sort: Pass Two
values [ 0 ] 6 SORTED
[1] U
24
N
[2] S
10 O
[3] R
36 T
[4] E
12 D
6
Selection Sort: End Pass Two
values [ 0 ] 6
SORTED
[1]
10
[2] U
24 N
S
[3] O
36 R
T
[4] E
12 D
7
Selection Sort: Pass Three
values [ 0 ] 6
SORTED
[1]
10
[2] U
24 N
S
[3] O
36 R
T
[4] E
12 D
8
Selection Sort: End Pass Three
values [ 0 ] 6 S
O
[1] R
10 T
[2] E
12 D
[3]
36
UNSORTED
[4]
24
9
Selection Sort: Pass Four
values [ 0 ] 6 S
O
[1] R
10 T
[2] E
12 D
[3]
36
UNSORTED
[4]
24
10
Selection Sort: End Pass Four
values [ 0 ] 6
S
[1] O
10
[2]
R
12 T
[3]
24 E
D
[4]
36
11
Selection Sort:
How many comparisons?
13
Notice that . . .
Sum = (N-1) + (N-2) + . . . + 2 + 1
Sum = N * (N-1)
2 14
For selection sort in general
The number of comparisons when the
array contains N elements is
Sum = N * (N-1) /2
Sum = .5 N2 - .5 N
Sum = O(N2)
15
template <class ItemType >
int MinIndex ( ItemType values [ ] , int start , int end )
// Post: Function value = index of the smallest value in
// values [start] . . values [end].
{
int indexOfMin = start ;
for ( int index = start + 1 ; index <= end ; index++ )
if ( values [ index ] < values [ indexOfMin ] )
indexOfMin = index ;
return indexOfMin;
}
16
template <class ItemType >
void SelectionSort ( ItemType values [ ] , int numValues )
// Post: Sorts array values[0 . . numValues-1 ] into ascending
// order by key
{
int endIndex = numValues - 1 ;
for ( int current = 0 ; current < endIndex ; current++ )
Swap ( values [ current ] ,
values [ MinIndex ( values, current, endIndex ) ] ) ;
}
17
Bubble Sort
Compares neighboring
values [ 0 ] 36 pairs of array elements,
starting with the last array
[1] element, and swaps
24
neighbors whenever they
[2] are not in correct order.
10
[3] On each pass, this causes
6 the smallest element to
[4] “bubble up” to its correct
12 place in the array.
18
template <class ItemType >
void BubbleUp ( ItemType values [ ] , int start , int end )
// Post: Neighboring elements that were out of order have been
// swapped between values [start] and values [end],
// beginning at values [end].
{
for ( int index = end ; index > start ; index-- )
if (values [ index ] < values [ index - 1 ] )
Swap ( values [ index ], values [ index - 1 ] ) ;
}
19
template <class ItemType >
void BubbleSort ( ItemType values [ ] , int numValues )
// Post: Sorts array values[0 . . numValues-1 ] into ascending
// order by key
{
int current = 0 ;
while ( current < numValues - 1 )
BubbleUp ( values , current , numValues - 1 ) ;
current++ ;
}
20
Insertion Sort
21
Insertion Sort
22
Insertion Sort
23
Insertion Sort
24
Insertion Sort
25
template <class ItemType >
void InsertItem ( ItemType values [ ] , int start , int end )
// Post: Elements between values [start] and values [end]
// have been sorted into ascending order by key.
{
bool finished = false ;
int current = end ;
bool moreToSearch = ( current != start ) ;
while ( moreToSearch && !finished )
{
if (values [ current ] < values [ current - 1 ] )
{
Swap ( values [ current ], values [ current - 1 ] ) ;
current-- ;
moreToSearch = ( current != start );
}
else
finished = true ;
} 26
}
template <class ItemType >
void InsertionSort ( ItemType values [ ] , int numValues )
// Post: Sorts array values[0 . . numValues-1 ] into ascending
// order by key
{
for ( int count = 0 ; count < numValues ; count++ )
27
Sorting Algorithms
and Number of Comparisons
Simple Sorts
Straight Selection Sort
Bubble Sort O(N2)
Insertion Sort
28
Recall that . . .
29
The largest element
in a heap is always found in the root node
root
70
60 12
40 30 8 10
30
The heap can be stored
in an array
values
[0] root
70
[1] 60 70
0
[2] 12
60 12
[3] 40
1 2
[4] 30 40 30 8 10
[5] 3 4 5 6
8
[6] 10
31
Heap Sort Approach
First, make the unsorted array into a heap by
satisfying the order property. Then repeat the
steps below until there are no more unsorted
elements.
[1] 60 70
0
[2] 12
60 12
[3] 40
1 2
[4] 30 40 30 8 10
[5] 3 4 5 6
8
[6] 10
33
Swap root element into last place
in unsorted array
values
[0] root
70
[1] 60 70
0
[2] 12
60 12
[3] 40
1 2
[4] 30 40 30 8 10
[5] 3 4 5 6
8
[6] 10
34
After swapping root element
into its place
values
[0] root
10
[1] 60 10
0
[2] 12
60 12
[3] 40
1 2
[4] 30 40 30 8 70
[5] 3 4 5 6
8
[1] 40 60
0
[2] 12
40 12
[3] 10
1 2
[4] 30 10 30 8 70
[5] 3 4 5 6
8
[6] 70
36
Swap root element into last place
in unsorted array
values
[0] root
60
[1] 40 60
0
[2] 12
40 12
[3] 10
1 2
[4] 30 10 30 8 70
[5] 3 4 5 6
8
[6] 70
37
After swapping root element
into its place
values
[0] root
8
[1] 40 8
0
[2] 12
40 12
[3] 10
1 2
[4] 30 10 30 60 70
[5] 3 4 5 6
60
[1] 30 40
0
[2] 12
30 12
[3] 10
1 2
[4] 8 10 8 60 70
[5] 3 4 5 6
60
[6] 70
39
Swap root element into last place
in unsorted array
values
[0] root
40
[1] 30 40
0
[2] 12
30 12
[3] 10
1 2
[4] 8 10 8 60 70
[5] 3 4 5 6
60
[6] 70
40
After swapping root element
into its place
values
[0] root
8
[1] 30 8
0
[2] 12
30 12
[3] 10
1 2
[4] 40 10 40 60 70
[5] 3 4 5 6
60
[1] 10 30
0
[2] 12
10 12
[3] 8
1 2
[4] 40 8 40 60 70
[5] 3 4 5 6
60
[6] 70
42
Swap root element into last place
in unsorted array
values
[0] root
30
[1] 10 30
0
[2] 12
10 12
[3] 8
1 2
[4] 40 8 40 60 70
[5] 3 4 5 6
60
[6] 70
43
After swapping root element
into its place
values
[0] root
8
[1] 10 8
0
[2] 12
10 12
[3] 30
1 2
[4] 40 30 40 60 70
[5] 3 4 5 6
60
[1] 10 12
0
[2] 8
10 8
[3] 30
1 2
[4] 40 30 40 60 70
[5] 3 4 5 6
60
[6] 70
45
Swap root element into last place
in unsorted array
values
[0] root
12
[1] 10 12
0
[2] 8
10 8
[3] 30
1 2
[4] 40 30 40 60 70
[5] 3 4 5 6
60
[6] 70
46
After swapping root element
into its place
values
[0] root
8
[1] 10 8
0
[2] 12
10 12
[3] 30
1 2
[4] 40 30 40 60 70
[5] 3 4 5 6
60
[1] 8 10
0
[2] 12
8 12
[3] 30
1 2
[4] 40 30 40 60 70
[5] 3 4 5 6
60
[6] 70
48
Swap root element into last place
in unsorted array
values
[0] root
10
[1] 8 10
0
[2] 12
8 12
[3] 30
1 2
[4] 40 30 40 60 70
[5] 3 4 5 6
60
[6] 70
49
After swapping root element
into its place
values
[0] root
8
[1] 10 8
0
[2] 12
10 12
[3] 30
1 2
[4] 40 30 40 60 70
[5] 3 4 5 6
60
52
if ( leftChild <= bottom ) // ReheapDown continued
{
if ( leftChild == bottom )
maxChild = leftChild;
else
{
if (values [ leftChild ] <= values [ rightChild ] )
maxChild = rightChild ;
else
maxChild = leftChild ;
}
if ( values [ root ] < values [ maxChild ] )
{
Swap ( values [ root ] , values [ maxChild ] ) ;
ReheapDown ( maxChild, bottom ) ;
}
}
}
53
Heap Sort:
How many comparisons?
In reheap down, an element root
is compared with its 2
children (and swapped 24
with the larger). But
0
only one element at
each level makes 60 12
this comparison,
and a complete 1 2
binary tree with 30 40 8 10
N nodes has
only O(log2N) 3 4 5 6
levels. 15 6 18 70
7 8 9 10
54
Heap Sort of N elements:
How many comparisons?
O ( N * log N) comparisons
55
Using quick sort algorithm
A..Z
A..L M..Z
56
// Recursive quick sort algorithm
splitVal = 9
GOAL: place splitVal in its proper position with
all values less than or equal to splitVal on its left
and all larger values on its right
9 20 6 18 14 3 60 11
values[first] [last]
58
After call to function Split
splitVal = 9
smaller values larger values
in left part in right part
6 3 9 18 14 20 60 11
values[first] [last]
splitVal = 9
GOAL: place splitVal in its proper position with
all values less than or equal to splitVal on its left
and all larger values on its right
9 20 26 18 14 53 60 11
values[first] [last]
62
After call to function Split
splitVal = 9
no smaller values larger values
empty left part in right part with N-1 elements
9 20 26 18 14 53 60 11
values[first] [last]
Strategy:
Repeatedly (right-to-left) organizes the data into
groups according to the ith character in each
element.
64
Radix Sort (2/5)
Begin by organizing the data into groups according to their
rightmost letters.
– The string in each group end with the same letter.
– The groups are order by that letter.
– The string within each group retain their relative order
from the original list of string.
Example:
You can pad numbers on the left with zeros,
making them all appear to the same length.
65
Radix Sort (3/5)
66
Radix Sort (4/5)
68
Mergesort
Strategy:
Divide an array into halves.
Sort each half (by calling itself recursively).
Merge the sorted halves into one sorted
array.
...
sorted array: 1 4 8 2 3
Merge the halves
tempArray: 1 2 3 4 8
copy
theArray: 1 2 3 4 8
70
Mergesort
void mergesort(DataType theArray[], int first, int last)
{
if (first < last)
{ int mid = (first + last)/2; // index of midpoint
mergesort(theArray, first, mid);
mergesort(theArray, mid+1, last);
71
const int MAX_SIZE = 10000;
73
Mergesort
void mergesort(DataType theArray[], int first, int last)
{
if (first < last)
{ int mid = (first + last)/2;
mergesort(theArray, first, mid);
mergesort(theArray, mid+1, last);
merge(theArray, first, mid, last);
}
} mergesort(theArray,0,5)
mergesort(theArray,0,2) mergesort(theArray,3,5)
mergesort(theArray,0,1) mergesort(theArray,1,1)
74
Mergesort
75
Mergesort
Level 0
n Merge n items:
3*n-1 operations
or O(n)
Level 1
n/2 n/2 Merge two n/2 items:
2*(3*n/2-1) operations
3n-2 operations
76