added insertion sort in c++

pull/8/head
satcasm 2021-01-14 13:06:56 +05:30
parent 0df8cfed1b
commit 55ea5afd8d
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;
}