Skip to content

Commit

Permalink
Merge pull request #22 from Manvi07/master
Browse files Browse the repository at this point in the history
Added insertion sort in C++
  • Loading branch information
i-vishi authored Oct 2, 2018
2 parents 600a358 + 0e36d6c commit e7c81bf
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Sorting/insertionSort/insertionSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Insertion Sort implementation in C++
* Author : Manvi Gupta
* Input : array length and elements
* Output : Sorted array elements
*/

#include <iostream>
using namespace std;

int n;
void insertionSort(int A[])
{
int key;
for(int i=0;i<n;i++)
{
key = A[i];
int j = i-1;
while(j>=0 and key<A[j])
{
A[j+1] = A[j];
j--;
}
A[j+1]=key;
}
}

int main()
{
std::cout << "Enter the array length: ";
std::cin >> n;
int A[n];
std::cout << "Enter the array elements :" << '\n';
for(int i=0; i<n; i++)
cin >> A[i];
insertionSort(A);
std::cout << "Sorted Array :" << '\n';
for(int i=0;i<n;i++)
cout << A[i] << endl;
return 0;
}

0 comments on commit e7c81bf

Please sign in to comment.