Insertion Sort

Insertion Sort is a simple sorting algorithm that build final sorted element in the array. The insertion sort give more efficient way and give more advantage than other sort like quick sort, merge sort and heap sort. 

It’s specialties are :

  • Simple
  • More efficient ( Time Complexity O(n2) )
  • Only required constant amount of memory requirement. ( O(1) )

Algorithm
————————————————————————————————————
Insertion_Sort(array){
     for i=1 to n-1{
          element = array[i];
          j=i;
          while(j>0 and array[j-1] > element ){
                 array[j] = array[j-1];
                 j=j-1;
          } 
          array[j] = element;
     }
}

Implementation is below : 
 

https://gist.github.com/nifrasismail/1a40493f0f3cee654955.js

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s