Date Archives January 28, 2019

Sorting Algorithms

Time complexity of sorting algorithms


Selection Sort – Youtube Link

  • From the beginning of an array, it finds minimum element in unsorted subarray (right side) and puts the element at the end of sorted subarray (left side) until it gets to the end.

e.g. Given unsorted array: 64  25  12  22  11. Red colored number is minimum in unsorted subarray, and underline represents a sorted subarray.

     64  25  12  22  11

     11  25  12  22  64

     11  12  25  22  64

     11  12  22  25  64

     11  12  22  25  64   (Completed)


Bubble Sort Youtube Link

  • It repeatedly swapping the adjacent elements if they are in wrong order.

e.g. Given unsorted array: 5  1  4  2  8. Two red colored numbers get compared and swapped.

   [ 1st Pass ] 

      5  1  4  2  8

     1  5  4  2  8

     1  4  5  2  8

     1  4  2  5  8

   [ 2nd Pass ]

     1  4  2  5  8

     1  4  2  5  8

     1  2  4  5  8

     1  2  4  5  8

Now the array is sorted, but it goes one more step checking whether whole pass doesn’t have any swap.

   [ 3rd Pass ]

     1  2  4  5  8

     1  2  4  5  8

     1  2  4  5  8

     1  2  4  5  8


Insertion Sort Youtube Link

  • It works the way we sort playing cards in our hands.
  • From the beginning of an array, next element gets compares with numbers from the end of sorted subarray and gets placed in right position.


Heap Sort Youtube Link

  • It builds a max heap (a parent node is larger than all children nodes) from the input data.
  • It is similar to selection sort, but it finds max instead of minimum. However by using max heap, sorting time is faster than selection sort.


Quick Sort Youtube Link

  • It is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
    1. Always pick first element as pivot.
    2. Always pick last element as pivot.
    3. Pick a random element as pivot.
    4. Pick median as pivot.

    The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.


Merge SortYoutube Link

  • It 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.
    • merge(): merging two halves, O(n).
    • Mergesort(): recursively calls itself to divide the array till size becomes one, O(logn).




P vs. NP

The P vs. NP problem is a major unsolved problem in computer science. It asks whether every problem whose solution can be quickly verified (technically, verified in polynomial time) can also be solved quickly (again, in polynomial time).

P (Polynomial Time)

  • Some algorithm can provide an answer in polynomial time (opposed to exponential time).

NP (Nondeterministic Polynomial Time)

  • There is no known way to find an answer quickly, but if one is provided with information showing what the answer is, it is possible to verify the answer quickly (in polynomial time).
  • E.g. Sudoku – Any proposed solution is easily verified, and the time to check a solution grows slowly (polynomially) as the grid gets bigger. However, all known algorithms for finding solutions take, for difficult examples, time that grows exponentially as the grid gets bigger.

NP – Complete

  • A problem in NP is NP-complete if every other problem in NP can be transformed (or reduced) into p in polynomial time.
  • It is not known whether every problem in NP can be quickly solved—this is called the P vs. NP problem. But if any NP-complete problem can be solved quickly, then every problem in NP can be solved quickly because of above definition.


P = NP: Problems that can be verified in polynomial time can also be solved in polynomial time.  

P ≠ NP: Problems can not be solved in polynomial time, but the answer could be verified in polynomial time.


Here is a helpful Youtube video.