**Insertion sort** is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time.

How it works:

- In insertion sort,elements are entered into the array(or list) 1-by-1.
- When the first element is entered, it is placed at the 1st position in the list(0th position in an array).
- When a new element is entered, it is compared to our already entered element and is decided whether to place before or after it in the list(array).
- Now when the third element is entered is entered, it is compared with the greater element of the 2 already existing elements. If smaller, then it is swapped. If not, then the array can be considered sorted.

If swapped, then is compared with the smaller element of the 2 already existing elements and swapped again with it if it is even smaller. - Similarly, all the the numbers to be placed in the list(array) are entered 1-by-1 and placed into the correct position right when they’re entered.

**Example**:

The following table shows the steps for sorting the sequence 5 7 0 3 4 2 6 1. On the left side the sorted part of the sequence is shown in red. For each iteration, the number of positions the inserted element has moved is shown in brackets. Altogether this amounts to 17 steps.

5 | 7 | 0 | 3 | 4 | 2 | 6 | 1 | (0) | |

5 | 7 | 0 | 3 | 4 | 2 | 6 | 1 | (0) | |

0 | 5 | 7 | 3 | 4 | 2 | 6 | 1 | (2) | |

0 | 3 | 5 | 7 | 4 | 2 | 6 | 1 | (2) | |

0 | 3 | 4 | 5 | 7 | 2 | 6 | 1 | (2) | |

0 | 2 | 3 | 4 | 5 | 7 | 6 | 1 | (4) | |

0 | 2 | 3 | 4 | 5 | 6 | 7 | 1 | (1) | |

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | (6) |

**In our program, We have used:**

- double arr[8]: A double datatype array of size 8 to store and sort any 8 numbers. Modify it to increase or decrease the number of elements to be entered and sorted (Also modify the conditions in the for loops.)
- int swaps: Integer variable to count the number of swaps performed.
- 1st for loop: To traverse through the array 8 times, accept 8 numbers and place them in their appropriate place (With the help of a nested while loop).
- int j=i: To start the array index for the while loop from i(location where the latest element has been placed).
- Nested while loop: This loop is nested in the above for loop. It compares and swaps the elements until the newly-inputted element reaches its appropriate place.
- swap(arg1,arg2): It is a predefined function and it swaps the data values of the 2 arguments passed to it.

**The Program:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
//Variables: //double arr[8]: An array to store and sort 8 elements. //int swaps: Integer variable to count the number of swaps performed. #include<iostream> int main() { using namespace std; double arr[8]={0,0,0,0,0,0,0,0}; //Declare our array and initialize to 0 int swaps=0; //Variable to count the number of swaps performed cout<<"Insertion sort Demonstration."<<endl<<endl; for (int i=0;i<8;i++) //To traverse through the array 8 times and accept 8 numbers { cout<<"Enter element number "<<i+1<<": "; //We use i+1 just to start the display of numbers from 1 rather than 0 cin>>arr[i]; int j = i; while(j>0 && arr[j]<arr[j-1]) //Runs until the new number has been placed in its correct place { swap(arr[j],arr[j-1]); //Swap if the elements are out of order. swaps++; //Increase swap counter j--; //decrease array index } } cout<<endl<<"Array After Sorting = "; for(int i=0;i<8;i++) //Loop to print our sorted array. { cout<<arr[i]<<" "; } cout<<endl; cout<<"Total number of swaps performed: "<<swaps<<endl<<endl; cout<<"Press any key to close this window..."; cin.get(); return 0; } |

Tested in Visual C++/Visual Studio

The following two tabs change content below.

#### Lalit Mali

Lalit is a technology enthusiast, a programming lover and currently an Android fan.

#### Latest posts by Lalit Mali (see all)

- Rank Pages in a Directory by Occurrence of a Particular Word in them – Java Data Mining - October 16, 2012
- Java Data Mining: Number of occurrences of a word in a file - October 15, 2012
- Calculate factorial of a number using C++ Recursive function - October 14, 2012