Professional Documents
Culture Documents
Chapter 7 Sorting
2
OBJECTIVE
• Introduces:
Sorting Concept
Sorting Types
Sorting Implementation
Techniques
3
CONTENTS
7.1 Introductions
7.2 Sorting Methods
7.2.1 Priority Queue Sorting Method (Selection Sort and
Heap Sort)
7.2.2 Insert and Keep Method (Insertion Sort and Tree Sort)
7.2.3 Divide and Conquer Method (Quick Sort and Merge
Sort)
7.2.4 Diminishing Increment Sort Method (Shell Sort)
4
Introduction
• One of the most common applications in computer
science.
• The process through which data are arranged in
either ascending or descending order according to
their values.
• Sorts are generally classified as either internal or
external:
Internal Sort – all of the data are held in primary
storage during the sorting process.
External sort – uses primary storage for the data
currently being sorted and secondary storage for any
data that will not fit in primary memory.
5
Sorting Methods
Priority Queue Sorting Method
• Method :
An array A is divided into sub-array P represents priority
queue and sub-array Q represents output queue.
The idea is to find the largest key among unsorted keys in P
and place it at the end of queue of Q. This will result a
sorted queue Q in increasing order.
• Two sorting techniques, which use this method, are
Selection Sort and Heap Sort.
7
• Example :
27 80 02 46 16 12 50
Path:
• Brief Analysis:
~ The inner loop executes 1 + 2 + ... + N – 1 times
or N-1 + N-2 + N-3 + …+ 3 + 2 + 1 which is equal to N(N – 1)/2 ~ O(N2)
~ The sorted input still need N(N-1)/2 comparisons, so it is not good and wasting
time.
A selection sort of an array of integers
into ascending order
Exercise
Use selection sort technique for the following keys:
27 80 02 46 16 12 54
Show the elements in the array for each iteration
Path 1: 02 80 27 46 16 12 54
Find the smallest in unsorted array and swap
Path 2: 02 80 27 46 16 12 54
Path 3: 02 12 27 46 16 80 54
Path 4: 02 12 16 46 27 80 54
Path 5: 02 12 16 27 46 80 54
Path 6: 02 12 16 27 46 54 80
13
Path 1: 02 80 27 46 16 12 54
Find the smallest in unsorted array and
swap
Path 2: 02 80 27 46 16 12 54
Path 3: 02 12 27 46 16 80 54
Path 4: 02 12 16 46 27 80 54
Path 5: 02 12 16 27 46 80 54
Path 6: 02 12 16 27 46 54 80
14
• Try this:
An array contains the elements shown below. The
first two elements have been sorted using a straight
selection sort. What would be the value of the
elements in the array after three more path/passes of
the selection sort algorithm?
7 8 26 44 13 23 98 57
15
Height log2 n
heap-size n
• Example :
Insert 02
(heap? yes) 19
19
02
19
Insert 46
(heap? no => re-heap) 46
19
02 46 02 19
Insert 16 46
46
(heap? no => re-heap)
02 19
16 19
16
02
20
46 Insert 54
Insert 12 46
(heap? no => re-heap)
(heap? Yes)
16 19
16 19
02 12
02 12 54
Insert 64
64
54
(heap? no => re-heap)
16 46 16 54
02 02 12 19 46
12 19 64
21
Insert 22
(heap? no => re-heap)
64 64
16 54 22 54
02 12 19 46 16 12 19 46
22 02
22
Insert 17
(heap? no => re-heap)
64 64
22 54 22 54
16 12 19 46 17 12 19 46
02 17 02 16
23
Insert 66
(heap? no => re-heap)
64 66
22 54 64 54
17 12 19 46 17 22 19 46
02 16 66 02 16 12
24
Insert 37
(heap? no => re-heap)
66 66
64 54 64 54
17 22 19 46 17 37 19 46
02 16 12 37 02 16 12 22
25
Insert 35
(heap? no => re-heap)
66
66
64 54
64 54
17 37 35 46
17 37 19 46
02 16 12 22 19
02 16 12 22 35
In array format :
26
66
64 54
17 37 35 46
02 16 12 22 19
27
64 54
17 37 35 46
After
02 16 12 22 19
19
64 54
17 37 35 46
02 16 12 22 66
28
before 19
17 37 35 46
02 16 12 22 66
after
64
37 54
17 22 35 46
02 16 12 19 66
29
exchange the root element with the last leaf, which is not,
yet highlighted
64
37 54
17 22 35 46
after
02 16 12 19 66 19
37 54
17 22 35 46
02 16 12 64 66
30
then, do the all three processes until all nodes are sorted.
19
before
then re-heap if necessary 37 54
17 22 35 46
after 02 16 12 64 66
54
37 46
17 22 35 19
02 16 12 64 66
31
37 46
17 22 35 19
02 16 12 64 66 12
37 46
17 22 35 19
02 16 54 64 66
32
17 22 35 19
02 16 54 64 66
46
37 35
17 22 12 19
02 16 54 64 66
33
exchange the root element with the last leaf, which is not
yet highlighted.
46
37 35
17 22 12 19
16
02 16 54 64 66
37 35
17 22 12 19
02 46 54 64 66
34
37
22 35
17 16 12 19
02 46 54 64 66
35
02
22 35
17 16 12 19
37 46 54 64 66
36
35
22 19
17 16 12 02
37 46 54 64 66
37
exchange the root element with the last leaf which is not
yet highlighted
02
22 19
17 16 12 35
37 46 54 64 66
38
22
17 19
02 16 12 35
37 46 54 64 66
39
12
17 19
02 16 22 35
37 46 54 64 66
40
19
17 12
02 16 22 35
37 46 54 64 66
41
exchange the root element with the last leaf which is not
yet highlighted.
16
17 12
02 19 22 35
37 46 54 64 66
42
17
16 12
02 19 22 35
37 46 54 64 66
43
02
16 12
17 19 22 35
37 46 54 64 66
44
16
02 12
17 19 22 35
37 46 54 64 66
45
exchange the root element with the last leaf which is not
yet highlighted.
12
02 16
17 19 22 35
37 46 54 64 66
46
02
12 16
17 19 22 35
37 46 54 64 66
47
02
12 16
17 19 22 35
37 46 54 64 66
48
02
12 16
17 19 22 35
37 46 54 64 66
• In an array form:
Sorted!
Algorithm: Heap Sort
1 void sort(int[] a) {
2 for (int i = (a.length-1)/2; i >= 0; i--) build heap, heapify the array a
3 heapify(a, i, a.length); Heap property: ai<=a(i-1)/2 for all i>0
4 for (int j = a.length-1; j > 0; j--) { //n-1
5 swap(a, 0, j);
6 heapify(a, 0, j); root is swapped with last element
re-heap the new root node
7 }
8 }
49
50
25
22 17
19 22 14 15
18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
22 22 17 19 21 14 15 18 14 11 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
9 22 17 19 22 14 15 18 14 21 3 22 25
Exercise
• Build heap sort for the following keys:
5, 3, 1, 9, 8, 2, 4, 7
55
…
Path 4: 02 16 27 46 80 12 54
Path 5: 02 12 16 27 46 80 54
..
Path 6: 02 12 16 27 46 54 80
Copyright ©2012 by
Pearson Education, Inc.
All rights reserved
▫ Sum is
Try this:
An array contains the elements shown below. The first two
elements have been sorted using a straight insertion sort. What
would be the value of the elements in the array after two more
passes of the straight insertion sort algorithm?
3 13 7 26 44 23 98 57
64
• Solve the left hand side first, then the right hand side
• Selection of partition (pivot)
▫ The first element in the list (Hoare, 1962)
▫ The median value of the three elements (left, right, and middle) in the
list (Singleton, 1969)
67
8, 2, 13, 5, 14, 3, 7
3, 2, 7, 5 8 14, 13
3, 2, 7, 5 8 14, 13 3, 2, 7, 5 8 14, 13
2 3 7,5 2 3 7,5
68
8, 2, 13, 5, 14, 3, 7
8, 2, 13, 5, 14, 3, 7
3, 2, 7, 5 8 14, 13
3, 2, 7, 5 8 14, 13
2 3 7,5
2 3 7,5
5 7
5 7
8, 2, 13, 5, 14, 3, 7
3, 2, 7, 5 8 14, 13
2 3 7,5
5 7
69
8, 2, 13, 5, 14, 3, 7
8, 2, 13, 5, 14, 3, 7
3, 2, 7, 5 8 14, 13
3, 2, 7, 5 8 14, 13
2 3 7,5 13 14
2 3 7,5 13 14
5 7
5 7
8, 2, 13, 5, 14, 3, 7
3, 2, 7, 5 8 14, 13
2 3 7,5 13 14 Sorted!
5 7 2, 3, 5, 7, 8, 13, 14
• One entry called the “pivot”
▫ Pivot in position that it will occupy in final sorted
array
▫ Entries in positions before pivot less than or equal to
the pivot
▫ Entries in positions after pivot are greater than or
equal to the pivot
?
Entries should be <= to the Entries should be >= to the
pivot pivot
The separation is achieved by a rotation of elements that moves the small to the left and the
large to the right.
8}
73
12 }
13 a[j] = pivot;
14 return j;
15 }
The separation is achieved by a rotation of elements that moves the small to the left and the
large to the right. The elements are located using 2 index i and j, initialized to p and q.
74
a[0]=Pivot=5
[0] [1] [2] [3] [4] [5] [6] [7] [8]
5 8 6 4 9 3 7 1 2
a[++i] ≤ pivot i j a[--j] ≥ pivot
a[0]=Pivot=2
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 2 3 4 5 9 7 6 8
a[5]=Pivot=9
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 2 3 4 5 8 7 6 9
a[5]=Pivot=8
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 2 3 4 5 6 7 8 9
Algorithm: another example
Useful links:
Exercise
• Based on the following contents of an array
{E, A, F, D, C, B}
▫ Draw diagrams to illustrate quick sorting
technique
▫ Show the contents of the array each time the
quick sort method change it while sorting the
array.
79
• Example : 75, 55, 15, 20, 85, 30, 35, 10, 60, 40, 50, 25, 45, 80, 70, 65
F : 75 55 15 20 85 30 35 10 60 40 50 25 45 80 70 65
Original data F1: 75 | 15 |85|35| 60| 50| 45| 70
F2: 55 | 20 |30|10| 40| 25| 80| 65
F3: 55 75 | 15 20 | 30 85 | 10 35 | 40 60 | 25 60 | 45 80 | 65 70
F1: 55 75 | 30 85 | 40 60 | 45 80 |
F2: 15 20 | 10 35 | 25 50 | 65 70 |
F3: 15 20 55 75 | 10 30 35 85 | 25 40 50 60 | 45 65 70 80 |
F1: 15 20 55 75 | 25 40 50 60
F2: 10 30 35 85 | 45 65 70 80
F3: 10 15 20 30 35 55 75 85 | 25 40 45 50 60 65 70 80
F1: 10 15 20 30 35 55 75 85
F2: 25 40 45 50 60 65 70 80
Sorted data
F3: 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85
Merging two sorted arrays into one sorted array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by
Pearson Education, Inc.
All rights reserved
Merge Sort
• Divide array into two halves
▫ Sort the two halves
▫ Merge them into one sorted array
• Uses strategy of “divide and conquer”
▫ Divide problem up into two or more distinct,
smaller tasks
• Good application for recursion
The major steps in a merge sort
Steps 6-7 repeatedly divide the array into smaller subarrays until they get
down to size 0 or 1
85
Try with this a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
example: 66 33 99 55 88 22 44 77
Other example: Merge Sort Algorithm
Exercise
• Show the stages of merge sort for the following
numbers:
13, 57, 39, 85, 99, 70, 22, 48, 64
88
• Subfiles: (5-increment)
Initial : 27 80 02 46 16 12 54 64 22 17 66 37 35
12 37 02 22 16 27 54 35 46 17 66 80 64
91
12 37 02 22 16 27 54 35 46 17 66 80 64
• Subfiles: (2-increment)
(12, 02, 16, 54, 46, 66, 64) ^ 02 37 12 22 16 27 46
35 54 17 64 80 66
(37, 22, 27, 35, 17, 80) ^ 02 17 12 22 16 27 46 35
54 37 64 80 66
• Subfiles: (1-increment)
02 17 12 22 16 27 46 35 54 37 64 80 66
02 12 16 17 22 27 35 37 46 54 64 66 80
92
Algorithm
The iSort() Method
1 void iSort(int[] a, int c, int d) {
2 for (int i = c+d; i < a.length; i+=d) {
3 int ai = a[i], j = i;
4 while (j > c && a[j-d] > ai) {
5 a[j] = a[j-d];
6 j -= d;
7 }
8 a[j] = ai;
9 }
10 }
93
Exercise
• Show the stages of Shell sort for the following
numbers (starting with 3 increment/apart) :
13, 57, 39, 85, 99, 70, 22, 48, 64
98
Useful link:
http://
www.youtube.com/watch?v=qzXAVXdd
cPU
99
Exercise:
1. Show which of the structures in below figure is a heap and
which is not.
23
23
12 15
12 15
11
(a) 11 (b)
100
23 23
12 15 13 15
13 11 12 11
(c) (d)
101
Summary
• Sorting is one of the most common data-
processing applications
• Sorting Methods
▫ Priority Queue Sorting Method - Selection Sort and
Heap Sort
▫ Insert and Keep Method - Insertion Sort and Tree Sort
▫ Divide and Conquer Method - Quick Sort and Merge
Sort
▫ Diminishing Increment Sort Method - Shell Sort