Last active
May 23, 2017 07:19
-
-
Save porimol/4bfab834f255033e1893586d59578815 to your computer and use it in GitHub Desktop.
Insertion Sort Algorithm Implementation Using C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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