Data Structures: Sorting Algorithms Overview
Data Structures: Sorting Algorithms Overview
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
Sorting
Classification
Sorting
Internal External
36 36 24 10 6 6
24 24 36 24 10 10
10 10 10 36 24 12
6 6 6 6 36 24
12 12 12 12 12 36
Insertion Sort (Steps)
Our strategy is to search for insertion point from the
beginning of the array and shift the element down to
make room for new element
1 2 3 4 5 6
77 42 35 12 101 5
Bubble Sort
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
Bubble Sort
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 101 Swap 5
5 101
Bubble Sort
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
42 35 12 77 5 101
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Bubble Sort Algorithm
Bubble_Sort(A,n)
{
for(i=1 to n-1)
{
for(j=1 to n-i)
{
if(A[j]>A[j+1)
{
temp= A[j]
A[j]= A[j+1]
A[j+1]= temp
} //end of if condition
} //end of inner for loop
} //end of outer for loop
Insertion Sort
Selection Sort
Bubble Sort
Summary
The insertion sort is a good middle-of-the-road
choice for sorting lists of a few thousand items or less.
37 23 6 89 15 12 2 19
Merge Sort
Mid = (0+6)/2 = 3
Mid = (0+1)/2 = 0
Mid = (4+5)/2 = 4
Mid = (2+3)/2 = 2
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 Mid = (0+7)/2 = 3
Mid = 1 98 23 45 14 6 67 33 42
Mid = 5
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Mid = 1 Mid = 5
98 23 45 14
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
Mid = 0
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
23 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23 45 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42 Mid = 5
98 23 45 14 6 67 33 42
98 23 45 14
23 98 14 45
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
Mid = 4
Mid = 6
23 98 14 45
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45 6
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45 6 67
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
98 23 45 14 6 67 33 42
6 14 23 33 42 45 67 98
Merge Sort Algorithm
Merge_Sort(A,first,last)
{
d= last-first+1
if(d<= 1)
exit;
if(first<last)
{
mid= (first+last)/2
Merge_Sort(A,first,mid)
Merge_Sort(A,mid+1,last)
Merge(A,first,mid,last)
} // end of if
} // end of Merge_Sort function
Merge(A,first,mid,last)
{
i=first, j=mid+1, k=first
while(i<= mid && j<= last)
{
if(A[i]< A[j])
{
temp[k]= A[i]
i++
} //end of if
else
{
temp[k]= A[j]
j++
} //end of else
k++
} // end of while loop
while(i<= mid)
{
temp[k]= A[i]
i++
k++
} //end of while loop
while(j<= last)
{
temp[k]= A[j]
j++
k++
} //end of while loop
for (i= first to last)
{
A[i]= temp[i]
} //end of for loop
} // end of Merge function
Quicksort
▪ Quick sort is a divide and conquer algorithm.
▪ Quick sort first divides a large list into two smaller
sub list.
▪ Quick sort can then recursively sort the sub-lists.
2. Reorder the list so that all elements with values less than
the pivot come before the pivot, while all elements with
values greater than the pivot come after it (equal values can
go either way). After this partitioning, the pivot is in its
final position. This is called the partition operation.
1 9. 09
6 54. 55
4 36. 36
4 36. 36
1 9. 09
4 36. 36
7 63. 64
5 45. 45
0 0. 00
3 27. 27
0 0. 00
0 0. 00
4 36. 36
3 27. 27
5 45. 45
6 54. 55
1 1 1 00.
0 0
2 1 8. 1 8
0 0. 00
1 0 90. 91
1 9. 09
3 27. 27
8 72. 73
4 36. 36
5 45. 45
5 45. 45
0 0. 00
0 0. 00
9 81 . 82
0 0. 00
8 72. 73
2 1 8. 1 8
5 45. 45
9 81 . 82
1 1 1 00.
0
0
0 0. 00
1 9. 09
9 81 . 82
4 36. 36
3 27. 27
2 1 8. 1 8
6 54. 55
0 0. 00
8 72. 73
2 1 8. 1 8
1 9. 09
0 0. 00
0 0. 00
2 1 8. 1 8
1 9. 09
1 9. 09
0 0. 00
0 0. 00
1 9. 09
4 36. 36
1 9. 09
7 63. 64
2 1 8. 1 8
3 27. 27
0 0. 00
0 0. 00
4 36. 36
2 1 8. 1 8
1 1 1 00.
0 0
0 0. 00
0 0. 00
0 0. 00
2 1 8. 1 8
4 36. 36
2 1 8. 1 8
4 36. 36
7 63. 64
0 0. 00
3 27. 27
0 0. 00
6 54. 55
3 27. 27
5 45. 45
1 1 1 00.
0
0
0 0. 00
0 0. 00
5 45. 45
8 72. 73
1 1 1 00.
0
0
9 81 . 82
0 0. 00
0 0
Example
Index 1 2 3 4 5 6 7 8 9
Ele 55 44 99 77 11 88 33 22 66
Key low high
Ele 55 44 99 77 11 88 33 22 66
Key low Exchange high
Ele 55 44 22 77 11 88 33 99 66
Key Low Exchange high
Ele 55 44 22 33 11 88 77 99 66
Key Low high
Index 1 2 3 4 5 6 7 8 9
Ele 55 44 22 33 11 88 77 99 66
Key high Low
Partition Exchange
Ele 11 44 22 33 55 88 77 66 99
Key high Low
Ele 11 44 22 33 55 66 77 88 99
11 33 22 44 55 66 77 88 99
11 22 33 44 55 66 77 88 99
Quick Sort Algorithm
Heap Sort
A sorting algorithm that works by first organizing
the data to be sorted into a special type of binary tree
called a heap
Procedures on Heap
Heapify
Build Heap
Heap Sort
Heapify: Heapify picks the largest child key and compare it to the
parent key. If parent key is larger than heapify quits, otherwise it swaps the
parent key with the largest child key. So that the parent is now becomes
larger than its children.
19
12 16
1 4 7
Heap Sort Algorithm
Heap_Sort(A,n)
{
BuildHeap(A,n);
for (i=n; i>=2; i--)
{
t= A[i];
A[i]= A[1];
A[1]= t;
Heapify(A,1,i-1);
}
BuildHeap(A,n)
{
for (i=n/2; i>=1; i--)
Heapify(A,i,n);
}
Heapify(A,i,n)
{
j= 2*i;
item= A[i];
while(j<=n)
{
if (j<n && A[j]< A[j+1])
j++;
if(item>= A[j])
break;
A[j/2]= A[j];
j= j*2;
}
A[j/2]= item;
}
Example: Convert the following array to a heap
16 4 7 1 12 19
16
4 7
1 12 19
16 16
4 7 4 19
swap
12 19 1 12 7
1
16 19
swap
12 19 12 16
swap
4 7 1 4 7
1
Example of Heap Sort
Take out biggest
19
12 16
Move the last element
to the root
1 4 7
Sorted:
Array A
12 16 1 4 7 19
7
swap
HEAPIFY()
12 16
1 4
Sorted:
Array A
7 12 16 1 4 19
16
12 7
1 4
Sorted:
Array A
16 12 7 1 4 19
Take out biggest
16
12 7
1 4
Sorted:
Array A
12 7 1 4 16 19
4
12 7
Sorted:
Array A
4 12 7 1 16 19
4
swap
HEAPIFY()
12 7
Sorted:
Array A
4 12 7 1 16 19
12
4 7
Sorted:
Array A
12 4 7 1 16 19
Take out biggest
12
Move the last
element to the
root
4 7
Sorted:
Array A
4 7 1 12 16 19
1
swap
4 7
Sorted:
Array A
1 4 7 12 16 19
7
4 1
Sorted:
Array A
7 4 1 12 16 19
Take out biggest
7
Move the last
element to the
root
4 1
Sorted:
Array A
1 4 7 12 16 19
1
swap
HEAPIFY()
Sorted:
Array A
4 1 7 12 16 19
Take out biggest
Move the last 4
element to the
root
Sorted:
Array A
1 4 7 12 16 19
Take out biggest
1
Sorted:
Array A
1 4 7 12 16 19
Time Analysis
Build Heap function will run in O(n) time
Heapify function will run in O(log n) time
There are n-1 calls to Heapify each call requires O(log n)
time
Heap sort program combine Build Heap program and
Heapify, therefore it has the running time of O(n log n)
time
Total time complexity: O(n log n)
Comparison with Quick Sort and Merge Sort
Quick sort is typically somewhat faster, due to better cache
behavior and other factors, but the worst-case running time
for quick sort is O (n2), which is unacceptable for large data
sets and can be deliberately triggered given enough knowledge
of the implementation, creating a security risk.
Heap sort also competes with merge sort, which has the same
time bounds, but requires Ω(n) auxiliary space, whereas heap
sort requires only a constant amount. Heap sort also typically
runs more quickly in practice. However, merge sort is simpler
to understand than heap sort, is a stable sort, parallelizes
better, and can be easily adapted to operate on linked lists and
very large lists stored on slow-to-access media such as disk
storage or network attached storage. Heap sort shares none of
these benefits; in particular, it relies strongly on random access.
Possible Application
When we want to know the task that carry the highest
priority given a large number of things to do
Counting sort
Bucket sort
Radix sort
Sorted list