Sort - Merge and Quick Sort
Sort - Merge and Quick Sort
arrayC
indexC
Merge Sort
arrayA 1 13 24 26
2 < 13 so we insert
indexA arrayB[indexB] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC 1
indexC
Merge Sort
arrayA 1 13 24 26
13 < 15 so we insert
indexA arrayA[indexA] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC 1 2
indexC
Merge Sort
arrayA 1 13 24 26
15 < 24 so we insert
indexA arrayB[indexB] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC 1 2 13
indexC
Merge Sort
arrayA 1 13 24 26
24 < 27 so we insert
indexA arrayA[indexA] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC 1 2 13 15
indexC
Merge Sort
arrayA 1 13 24 26
26 < 27 so we insert
indexA arrayA[indexA] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC 1 2 13 15 24
indexC
Merge Sort
arrayA 1 13 24 26
Since we have exhausted
one of the arrays, arrayA,
we simply copy the
remaining items from the
arrayB 2 15 27 38 other array, arrayB, into
arrayC
indexB
arrayC
1 2 13 15 24 26
indexC
Merge Sort
arrayA 1 13 24 26
arrayB 2 15 27 38
arrayC 1 2 13 15 24 26 27 38
Merge Sort Pseudocode
mergesort(list, first, last)
IF first < last
mid = (first + last)/2;
// Sort the 1st half of the list
mergesort(list, first, mid)
// Sort the 2nd half of the list
mergesort(list, mid+1, last)
// Merge the 2 sorted halves
merge(list, first, mid, last)
END IF
END mergesort
Merge Sort Pseudocode (cont)
// Merge 2 sorted halfs
merge(list, first, mid, last)
// Initialize the first and last indices of our subarrays
firstA = first
lastA = mid
firstB = mid+1
lastB = last
1 4 8 9 0 11 5 10 7 6
The index left starts at the first element and right
starts at the next-to-last element.
1 4 8 9 0 11 5 10 7 6
left right
We want to move all the elements smaller than the
pivot to the left part of the array and all the elements
larger than the pivot to the right part.
Quick Sort
We move left to the right, skipping over elements that are
smaller than the pivot.
1 4 8 9 0 11 5 10 7 6
left right
Quick Sort
We then move right to the left, skipping over
elements that are greater than the pivot.
1 4 8 9 0 11 5 10 7 6
left right
1 4 5 9 0 11 8 10 7 6
left right
The effect is to push a large element to the right and a small
element to the left.
We then repeat the process until left and right cross.
Quick Sort
1 4 5 9 0 11 8 10 7 6
left right
1 4 5 9 0 11 8 10 7 6
left right
1 4 5 0 9 11 8 10 7 6
left right
Quick Sort
1 4 5 0 9 11 8 10 7 6
left right
1 4 5 0 9 11 8 10 7 6
right left
Quick Sort
1 4 5 0 9 11 8 10 7 6
right left
At this point, left and right have crossed so no swap is
performed.
The final part of the partitioning is to swap the pivot
element with left.
1 4 5 0 6 11 8 10 7 9
right left
Quick Sort
Note that all elements to the left of the pivot are less
than (or equal to) the pivot and all elements to the right
of the pivot are greater than (or equal to) the pivot.
Hence, the pivot element has been placed in its final
sorted position.
1 4 5 0 6 11 8 10 7 9
right left
Quick Sort
We now repeat the process using the sub-arrays to the
left and right of the pivot.
1 4 5 0 6 11 8 10 7 9
1 4 5 0 11 8 10 7 9
Quick Sort
A couple of notes about quick sort:
There are more optimal ways to choose the pivot value (such
as the median-of-three method).
Also, when the subarrays get small, it becomes more efficient
to use the insertion sort as opposed to continued use of quick
sort.
References
www.linux.wku.edu
www.vu.edu.pk
M.Azam, COMSATS, ABBOTTABAD