358 33 Powerpoint-Slides 14-Sorting Chapter-14
358 33 Powerpoint-Slides 14-Sorting Chapter-14
SORTING
39 9 45 63 18 81 108 54 72 36
39 9 45 63 18 81 108 54 72 36
© Oxford University Press 2011. All rights reserved.
9 39 45 63 18 81 108 54 72 36
9 39 45 63 18 81 108 54 72 36
9 39 45 63 18 81 108 54 72 36
9 18 39 45 63 81 108 54 72 36
9 18 39 45 63 81 108 54 72 36
9 18 39 45 63 81 108 54 72 36
9 18 39 45 54 63 81 108 72 36
9 18 39 45 54 63 72 81 108 36
9 18 36 39 45 54 63 72 81 108
In Pass N, A[N-1] will be placed in its proper place so that the array A is sorted.
Therefore, we conclude to insert the element A[K] is in the sorted list A[0], A[1], ….
A[K-1], we need to compare A[K] with A[K-1], then with A[K-2], then with A[K-3] until
© Oxford University Press 2011. All rights reserved.
we meet an element A[J] such that A[J] <= A[K].
Insertion sort (ARR, N) where ARR is an array of N elements
PASS LOC ARR[0] ARR[1] ARR[2] ARR[3] ARR[4] ARR[5] ARR[6] ARR[7]
1 1 9 39 81 45 90 27 72 18
2 7 9 18 81 45 90 27 72 39
3 5 9 18 27 45 90 81 72 39
4 7 9 18 27 39 90 81 72 45
5 7 9 18 27 39 45 81 72 90
6 6 9 18 27 39 45 72 81 90
39 9 81 45
90 27 72 18
90 27 72 18
39 9 81 45
90 27 72 18
39 9 81 45
39 9 81 45 90 27 18 72
9 39 45 81 27 90 18 72
9 39 45 81 18 27 72 90
9 18 27 39 45 72 81 90
TEMP
9
INDEX
9 39 45 81 18 27 72 90
BEG I mID J END
TEMP
9 18
INDEX
9 39 45 81 18 27 72 90
BEG I Mid J END
TEMP
9 18 27
INDEX
9 39 45 81 18 27 72 90
BEG I Mid J END
TEMP
9 18 27 39
© Oxford University Press 2011. All rights reserved.
INDEX
9 39 45 81 18 27 72 90
9 18 27 39 45
INDEX
9 39 45 81 18 27 72 90
9 18 27 39 45 72
INDEX
en I is greater than MID copy the remaining elements of the right sub-array in TEMP
9 18 27 39 45 72 72 81 90
INDEX
27 10 36 18 25 45
We choose the first element as the pivot. Set loc = 0, left =
0, right = 5 as,
27 10 36 18 25 45
Loc Righ
Lef t
t
m right to left. Since a[loc] < a[right], decrease the value of right.
27 10 36 18 25 45
Loc Righ
Lef t
t
[loc] > a[right], interchange the two values and set loc = right.
25 10 36 18 27 45
25 10 36 18 27 45
© Oxford University
Lef Press 2011. All rights reserved.
t Right, Loc
ning from left to right. Since, a[loc] > a[right], increment the value of left .
25 10 36 18 27 45
Lef
Right, Loc
t
nce, a[loc] < a[right], interchange the values and set loc = left
25 10 27 18 36 45
Left, Loc
Right
,
m right to left. Since a[loc] < a[right], decrement the value of right.
25 10 27 18 36 45
[loc] > a[right], interchange the two values and set loc = right.
25 10 18 27 36 45
Le Right, Loc
ft
ning from left to right. Since, a[loc] > a[right], increment the value of left.
25 10 18 27 36 45
Step 1: [Initialize] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG = 0
Step 2: Repeat Steps 3 to while FLAG = 0
Step 3: Repeat while ARR[LOC] <= ARR[RIGHT] AND LOC != RIGHT
SET RIGHT = RIGHT – 1
[END OF LOOP]
Step 4: IF LOC == RIGHT, then
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT], then
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5: IF FLAG = 0, then
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step 6: IF LOC == LEFT, then
SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT], then
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7: [END OF LOOP]
Step 8: END
Number 0 1 2 3 4 5 6 7 8 9
345 345
654 654
924 924
123 123
567 567
472 472
555 555
808 808
911 911
472 472
123 123
654 654
924 924
345 345
555 555
567 567
808 808
808 808
911 911
123 123
924 924
345 345
654 654
555 555
567 567
472 472
After this pass, the numbers are collected bucket by bucket. The new
list thus formed is the final sorted result. After the third pass, the list
can be given as,
123, 345, 472, 555, 567, 654, 808, 911, 924.
© Oxford University Press 2011. All rights reserved.
Algorithm for RadixSort ( ARR, N)
HEAPSORT(ARR, N)
• Heap sort algorithm uses two heap operations: insertion and root deletion. Each
element extracted from the root is placed in the last empty location of the array.
• In phase 1, when we build a heap, the number of comparisons to find the right
location of new element in H cannot exceed the depth of H. Since, H is a complete
tree, its depth cannot exceed m where m is the number of elements in the heap H.
• Thus, total number of comparisons g(n) to insert n elements of ARR in H is bounded
as,
g(n) <= nlogn
• Hence, the running time of the first phase of the heap sort algorithm is given as
O(nlogn).
• In phase 2, we have H a complete tree with m elements having the left and right
subtrees as heaps. Assuming L to be the root of the tree, reheaping the tree would
need 4 comparisons to move L one step down the tree H. since, the depth of H
cannot exceed O(log m), reheaping the tree will require a maximum of 4 log m
comparisons to find the right location of L in H.
• Since, n elements will be deleted from the heap H, reheaping will be done n times.
Therefore, h(n) of comparisons to delete n elements is bounded as,
h(n) <= 4n log n
• Hence, the running time of the second phase of the heap sort algorithm is given as
O(nlogn).
• We see that, each phase requires time proportional to O( n log n). Therefore, running
time to sort an array of n elements using heap sort in the worst case is proportional to
O(nlogn).
Arrange the elements of the array in the form of a table and sort the columns
63 19 7 90 81 36 54 45
72 27 22 9 41 59 33
Result
19 7 9 41 36 33 45
63
72 27 22 90 81 59 54
63 19 7 9 41 22 19 7 9 27
36 33 45 72 27 36 33 45 59 41
22 90 81 59 54 63 90 81 72 54
Again arrange the elements of the array in the form of a table and sort
the columns with smaller number of long columns
9 19 7
22 19 7
29 27 36
9 27 36
33 45 54
33 45 59
41 63 59
41 63 90
81 72 90
81 72 54
Again arrange the elements of the array in the form of a table and
sort the columns with smaller
9
number
19
of long columns7 19
7 22 9 22
27 36 27 36
33 45 33 41
54 41 54 45
63 59 63 59
© Oxford University
81 Press
72 2011. All rights reserved. 81 72
rrange the elements of the array in a single column and sort the column
7
Shell_Sort( Arr, n)
19
45
63
59
81
72
90