Top Coding Algorithm — Sorting

Merge Sort, Bubble Sort, and Quick Sort

In this post, we will go through some common sorting algorithms and implement them in python (code).

Merge Sort

MergeSort(arr[], l,  r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)

There are basically 2 subproblems in merge sort,

  1. Divide the array into 2 halves
  2. Merge 2 sorted array

Question 1 is simple, let’s try to solve question 2. How to merge 2 sorted array into 1 sorted array?

Suppose we have 2 sorted array:
a = [2, 13, 32]
b = [1, 5, 11, 22]
Design an algorithm to merge them into 1 sorted array.

The problem is solved by using 3 pointers. i, j, k are pointers for left array, right array and result array.

Take the above array as example,

  1. at the beginning, we initialise the result array as [0, 0, 0, 0, 0, 0, 0] and i = j = k = 0
  2. then at the first loop, a[0] = 2 > b[0] = 1 , so we fill the result[0] = 1 and move the pointer j of array b to the second element, meanwhile increase result index k by 1
  3. at the second loop, a[0] = 2 < b[1] = 5 , so we fill the result[1] = 2 and move pointer i of array a to the next element, and continue to increase result index by 1
  4. repeat the step until one of the array is exhaustive and in the end we fill the rest of result array with the left elements

Solving this problem solves half of the merge sort, next we just need to recursively imbed the code into merge sort.

The first halve, we split the array into 2 and recursively call merge sort, and the second half we merge 2 sorted array into 1.

Bubble Sort

Example:

First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

So the key subproblem to solve here is:

Implement an algorithm that is able to swap unordered element pairs in an array.

This should be simple. Besides swapping, at the end of the function it also returns an indicator of whether the array is sorted or not.

Now we just need to wrap the swap in a while loop to sort an array:

Quick Sort

  1. Select a pivot point from the array and put elements smaller than the pivot point on its left, and elements larger than pivot point on its right.
  2. For each left and right part repeat the process until the sub-left or sub-right array has less or equal than one element.

The problem is divided into solving repeated subproblems, and the key to the subproblem is:

Implement an algorithm that selects a pivot point, and rearrange the array by the selected pivot point, where the left part is smaller than the pivot point and right part is larger than the pivot point.

Consider an array l = [5, 1, 3] , and low = 0, high = 2 , then

  1. in the first loop, l[0] = 5 > 3 = pivot , nothing was done, l = [5, 1, 3]
  2. in the second loop, l[1] = 1 < 3 = pivot , the element 1 was swapped with the first element in the list, and l = [1, 5, 3] , and the index of smallest element was moved by 1: i = 1
  3. in the third loop, same as the first one, nothing was done.
  4. in the end we swap the last element with the element at the index i , l[2], l[1] = l[1], l[2] , the result became l = [1, 3, 5]

Now quick sort would be to repeat the process above:

Reference

[1] https://www.geeksforgeeks.org/merge-sort/

[2] https://www.geeksforgeeks.org/bubble-sort/

[3] https://www.geeksforgeeks.org/quick-sort/

Hmm…I am a data scientist looking to catch up the tide…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store