Merge pull request #8 from satcasm/lBranch

added insertion sort in c++
pull/10/head^2
Ming Tsai 2021-01-15 07:29:54 -04:00 committed by GitHub
commit c34a024605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n)
{
int temp,j;
for (int i = 1; i < n; i++)
{
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp)
{ // Moving the elements of array that are greater than temp, to one position ahead of their current position
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
}
int main()
{
int n;
cout << "Enter the length of array:\n";
cin >> n;
int arr[n];
cout << "Enter the elements of array:\n";
for (int i = 0; i < n; i++)
cin >> arr[i]; // Taking input
insertionSort(arr, n); //Calling of insertionSort function
cout << "Sorted array elements:\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " "; //Printing out elements
return 0;
}