Monday 6 August 2012

How to implement insertion sort in C++

Program to implement Insertion Sort



Insertion Sort in Descending Order - 


#include<iostream>

#include<conio.h>

using namespace std;

int main()
{
       int numbers[8], counter = 0, temp = 0, element = 0;

       cout << "\n\n\t\t\t ___ Insertion Sort ___ ";

       for(counter = 0; counter < 8; counter++)
       {
              cout << "\n\n\t\t" << counter + 1 << " Number  -  ";

              cin >> numbers[counter];
       }

       //____ Insertion sort in Descending Order _____

       for(counter = 0; counter < 8; counter++)
       {
              element = numbers[counter];

              for(temp = counter - 1; (temp >= 0) && (numbers[temp] < element); temp--)
              {
                     numbers[temp + 1] = numbers[temp];

                     numbers[temp] = element;
                    
              }

       }

       cout << "\n\n\t Sorted List  -  ";

       for(counter = 0; counter < 8; counter++)
       {
              cout << numbers[counter] << " , ";
       }

       cout << "\b\b  ";

       getch();

       return 0;
}

Insertion Sort in Ascending Order - 


#include<iostream>

#include<conio.h>

using namespace std;

int main()
{
       int numbers[8], counter = 0, temp = 0, element = 0;

       cout << "\n\n\t\t\t ___ Insertion Sort ___ ";

       for(counter = 0; counter < 8; counter++)
       {
              cout << "\n\n\t\t" << counter + 1 << " Number  -  ";

              cin >> numbers[counter];
       }

       //____ Insertion sort in Descending Order _____

       for(counter = 0; counter < 8; counter++)
       {
              element = numbers[counter];

              for(temp = counter - 1; (temp >= 0) && (numbers[temp] > element); temp--)
              {
                     numbers[temp + 1] = numbers[temp];

                     numbers[temp] = element;
                    
              }

       }

       cout << "\n\n\t Sorted List  -  ";

       for(counter = 0; counter < 8; counter++)
       {
              cout << numbers[counter] << " , ";
       }

       cout << "\b\b  ";

       getch();

       return 0;
}



Insertion Sort is a sorting algorithm that is efficient for small list of data items. This algorithm is pretty expensive in a sense that is requires all the elements to be shifted one by one to insert the element in the correct order.

Figure - Insertion Sort Demonstration in Ascending Order


  • An example of insertion sort. Check each element and put it in the right place in the sorted list.

2 comments:

  1. Hey , buddy .. why posting C++ codes in Algorithms and there's not much difference between ascending and descending ....just post pseudo codes and total cost gaining method and time complexity . btw I really like this demonstration graph copied from wikipedia :P :P

    ReplyDelete
    Replies
    1. It shows that you used to check out Wikipedia a lot, but what actually is more important is to make it easy to learn thing whether u learn here or there and as far as the time complexity and cost gaining method is concerned we will come to that also after some time.

      Delete