SORTING(Merge, Quick and Insertion)
SORTING(Merge, Quick and Insertion)
Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself
for the two halves and then merges the two sorted halves. The merge() function is used for
merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and
arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.
5. QUICK SORT
Quick sort is an algorithm of divide and conquer type. That is, the problem of sorting a
set is reduced to the problem of sorting two smaller sets. We illustrate this “Reduction
step” by means of a specific example.
Suppose A is a following list of 12 numbers
44 33 11 55 70 90 40 60 99 22 88 66
The reduction step of the quick sort algorithm finds the final position of one of the
numbers; in this illustration, we use the first number 44. This is accomplished as follows.
Beginning with the last number 66, scan the list from right to left, comparing each
number with 44 and stopping at the first number less than 44. The number is 22.
Interchange 44 and 22 to obtain the list.
22 33 11 55 70 90 40 60 99 44 88 66
(Observe that the number 88 and 66 to the right of 44 are each greater than 44).
Beginning with 22, next scan the list in the opposite direction from left to right,
comparing each number with 44 and stopping at the first number greater than 44. The
number is 55. Interchange 44 and 55 to obtain the list.
22 33 11 44 70 90 40 60 99 55 88 66
(Observe that the number 22, 33 and 11 to the left of 44 are each less than 44). Beginning
with 55, now scan the list in the original direction from right to left, comparing each
number with 44 and stopping at the first number less than 44. The number is 40.
Interchange 44 and 40 to obtain the list.
22 33 11 40 70 90 44 60 99 55 88 66
(Observe that the numbers to the right of 44 are greater than 44). Beginning with 40, next
scan the list in the opposite direction from left to right, comparing each number with 44
and stopping at the first number greater than 44. The number is 77. Interchange 44 and 77
to obtain the list.
22 33 11 40 44 90 70 60 99 55 88 66
(Observe that the numbers to the left of 44 are each less than 44). Beginning with 77,
now scan this list from right to left, until meeting the first number less than 44. We don’t
meet such a number before meeting 44. This means all numbers have been scanned and
compared with 44. Furthermore all numbers less than 44 now from the sub list of
numbers to the left of 44, and all numbers greater than 44 now from the sub list of
numbers to the right of 44, as shown below.
22 33 11 40 44 90 70 60 99 55 88 66
22 33 11 40 44 90 70 60 99 55 88 66
22 33 11 40 90 70 60 99 55 88 66
11 33 22 40 66 70 60 99 55 88 90
11 22 33 40 66 70 60 90 55 88 99
22 66 70 60 88 55 90 99
11 66 70 60 88 55 90 99
33 40 66 70 60 88 55 99
33 55 70 60 88 66
40 55 66 60 88 70
55 60 66 88 70
55 60 66 88 70
55 60 88 70
55 70 88
60 88
70
11 22 33 40 44 55 60 66 70 88 90 99
INSERTION SORT