UNIT4
UNIT4
Sorting
39 9 45 63 18 81 108 54 72 36
39 9 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 we meet an element A[J] such that A[J] <= A[K].
In order to insert A[K] in its correct position, we need to move each element A[K-1],
A[K-2], …., A[J] by one position and then A[K] is inserted at the (J+1)th location.
© Oxford University Press 2014. All rights reserved.
Insertion Sort
• Similarly, the worst case of the insertion sort algorithm occurs when the array is
sorted in reverse order. In the worst case, the first element of the unsorted set
has to be compared with almost every element in the sorted set. Furthermore,
every iteration of the inner loop will have to shift the elements of the sorted set
of the array before inserting the next element. Therefore, in the worst case,
insertion sort has a quadratic running time (i.e., O(n2)).
• Even in the average case, the insertion sort algorithm will have to make at least
(K-1)/2 comparisons. Thus, the average case also has a quadratic running time.
• Divide means partitioning the n-element array to be sorted into two sub-arrays of n/2 elements in each
sub-array. (If A is an array containing zero or one element, then it is already sorted. However, if there are
more elements in the array, divide A into two sub-arrays, A1 and A2, each containing about half of the
elements of A).
• Conquer means sorting the two sub-arrays recursively using merge sort.
• Combine means merging the two sorted sub-arrays of size n/2 each to produce the sorted array of n
elements.
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 00 27 18 72
9 39 45 81 27 90 18 72
9 39 45 81 18 27 72 90
9 18 39 45 72 81 90
TEMP
INDEX
9 39 45 81 18 27 72 90
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
INDEX
9 18 27 39 45
INDEX
9 39 45 81 18 27 72 90
9 18 27 39 45 72
INDEX
When 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
• Set the index of the first element in the array to loc and left variables. Also set the index of
the last element of the array to the right variable.
• That is, loc =0, left = 0 and right = n-1 (where n in the number of elements in the array)
• Start from the element pointed by right and scan the array from right to left, comparing each
element on the way with the element pointed by variable loc.
• That is, a[loc]should be less than a[right]. If that is the case then simply continue comparing
until right becomes equal to loc. Because once right = loc, then it means the pivot has been
placed in its correct position.
• If that is the case then simply continue comparing until left becomes equal to loc. Because once left
= loc, then it means the pivot has been placed in its correct position.
• However, if at any point we have a[loc]<a[left] then, interchange the two values and jump to step 2
• Set loc = left.
Loc Right
Left
Scan from right to left. Since a[loc] < a[right], decrease the value of right.
27 10 36 18 25 45
Loc Right
Left
Since, a[loc] > a[right], interchange the two values and set loc = right.
25 10 36 18 27 45
25 10 36 18 27 45
Left
Right, Loc
Left
Right, Loc
Now since, a[loc] < a[right], interchange the values and set loc = left
25 10 27 18 36 45
Left, Loc
Right,
Since, a[loc] > a[right], interchange the two values and set loc =
right. 25 10 18 27 36 45
Start scanning 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