Skip to content

Instantly share code, notes, and snippets.

@porimol
Last active May 23, 2017 07:19
Show Gist options
  • Save porimol/4bfab834f255033e1893586d59578815 to your computer and use it in GitHub Desktop.
Save porimol/4bfab834f255033e1893586d59578815 to your computer and use it in GitHub Desktop.
Insertion Sort Algorithm Implementation Using C++
/**
* @file insertion_sort.cpp
* @author Porimol Chandro, CSE 32D, World University of Bangladesh(WUB)
* @date 23/05/2017
*
* @brief Insertion Sort Algorithm Implementation.
*/
#include <iostream>
using namespace std;
int main()
{
int data[100], n, i, hole, value;
cout << "Please enter the size of an array: ";
cin >> n;
for(i = 0; i<n; i++){
cout << "Please enter the value of index[" <<i <<"]: ";
cin >> data[i];
}
for(i = 0; i<n; i++){
hole = i;
value = data[i];
while(hole > 0 && data[hole-1] > value){
data[hole] = data[hole-1];
hole = hole-1;
}
data[hole] = value;
}
cout << "\n Following is the sorted values are:\n-------------------------------------" <<endl;
for(i = 0; i<n; i++){
cout << "The value of index[" <<i <<"] is " <<data[i] <<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment