Quick, Merge and Counting Sort of Array
Quick, Merge and Counting Sort of Array
Step 2 − Take two variables to point left and right of the list
excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left
and right
Step 8 − if left ≥ right, the point where they met is new pivot
Quick Sort
15 20 35 10 25 55 50 45 40 30
15 20 10 25 30 35 55 50 45 40
10 15 20 25 30 35 40 45 50 55
10 15 20 25 30 35 40 45 50 55
10 15 20 25 30 35 40 45 50 55
Quick sort Algorithm
Merge Sort
• Merging a two lists of one element each is the
same as sorting them.
• Merge sort divides up an unsorted list until
the above condition is met and then sorts the
divided parts back together in pairs.
• Specifically, this can be done by recursively
dividing the unsorted list in half, merge sorting
the right side then the left side and then
merging the right and left back together.
Pseudo Code of Merge Sort
Given a list L with a length k:
• If k == 1 → the list is sorted
• Else:
– Recursively Sort the left side (0 to k/2)
– Recursively Sort the right side (k/2+1 to k)
– Merge the right side with the left side
Merge Sort
Merge Sort
Merge sort (A)
{
n=length(A)
if n<2 Return
mid=n/2
left=array of size (mid)
right=array of size (n-mid)
merge sort(left)
merge sort(right)
merge(left, right, A)
}
Merge Sort
merge(left, right, A)
{
nL=len(left)
nR=len(right)
i=j=k=0