Skip to content

Instantly share code, notes, and snippets.

@prodhan
Created January 20, 2019 12:58
Show Gist options
  • Save prodhan/0baa902bbe33af6f6f758b7c8079aa10 to your computer and use it in GitHub Desktop.
Save prodhan/0baa902bbe33af6f6f758b7c8079aa10 to your computer and use it in GitHub Desktop.
Insertion sort using c++
/*
Created By Ariful Islam
Student of Dhaka Internationa University
Learn how does work this sort https://visualgo.net/en/sorting
*/
#include<iostream>
using namespace std;
int main(){
int arr[100], p, i, j, temp, n;
cout<<"How many number you want?\n";
cin>>n;
cout<<"Input "<<n<<" numbers\n";
for(p=0; p<n; p++){
cin>>arr[p];
}
for (i = 1; i < n; i++)
{
temp = arr[i];
j = i-1;
while (j >= 0 && arr[j] > temp)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
//output
for(p=0; p<n; p++){
cout<<arr[p];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment