Sorting Algorithms

Heapsort

  1. Make an in-place Heap (Ordered Tree)
  2. Sort by moving root to end (as root is largest)
  3. Repeat steps 1&2 for element [0 to last-1] till value of last is1
QuickSort - 2-3 times faster than Heapsort

  1. Select a Pivot Point ( say last element ) 
  2. Keep swapping the elements and reach position i such that
    1. element [ 0 to i ] < element [i] < element [ i to last]
  3. The element at i is not at its sorted position.
  4. Repeat steps 1&2 for two halves around the pivot point.
  5. The algo performance depends on the selection of the pivot point.
Mergesort
  1. Divide the array till we get 1 element array.
  2. Merge the element arrays while maintaining the sort order.
    1. Highest space complexity O(n)

Insertsort
  1. Insert an element to its correct position in a partially sorted array.
  2. Sorting starts by finding the correct element for position 0,1,2.. so on till last
Bubblesort
  1. Smaller elements slowly bubble up the sorted list.
  2. After first loop the last element is the largest

No comments:

Post a Comment