Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/cpp/algorithms/sorting/InsertionSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
using namespace std;

//Function to print array.
void display(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout<< arr[i]<<"\t";
cout<<"\n";
}

//Main function to run the program.
int main()
{
int array[] = {5, 3, 1, 9, 8, 2, 4,7};
int size = sizeof(array)/sizeof(array[0]);

cout<<"Before Insertion sort: \n";
display(array, size);//Using dispaly function to print unsorted array.

int i, target, j;
for (i = 1; i < size; i++)
{
target = array[i];
j = i - 1;

/* Here the elements in b/w arrary[0 to i-1]
which are greater than target are moved
ahead by 1 position each*/
while (j >= 0 && array[j] > target)
{
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = target;
}
cout<<"After Insertion sort: \n";
display(array, size);//Using dispaly function to print sorted array.
return 0;
}