From 2d5fdfabf07012c14bb78357eea5f8773751aaf3 Mon Sep 17 00:00:00 2001 From: sriharikarthik08 <73272009+sriharikarthik08@users.noreply.github.com> Date: Thu, 22 Oct 2020 14:52:12 +0530 Subject: [PATCH] updated code with change plz check --- cpp/insertion_sort.cpp | 51 ++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/cpp/insertion_sort.cpp b/cpp/insertion_sort.cpp index 99212f1..0089f73 100644 --- a/cpp/insertion_sort.cpp +++ b/cpp/insertion_sort.cpp @@ -1,27 +1,30 @@ -#include -using namespace std; +#include +#include -void insSort(vector &a){ - int j,n=a.size(),k; - for(int i=1;i=0 && a[j]>k){ - a[j+1]=a[j]; - j--; - } - a[j+1]=k; - } +void insertionSort(int arr[], int length) { + int i, j, tmp; + for (i = 1; i < length; i++) { + j = i; + while (j > 0 && arr[j - 1] > arr[j]) { + tmp = arr[j]; + arr[j] = arr[j - 1]; + arr[j - 1] = tmp; + j--; + } + } } -int main() { - int n,e; - vector a; - cout<<"Enter length of array: "; - cin>>n; - cout<<"Enter elements in array\n"; - for(int i=0;i>e,a.emplace_back(e); - insSort(a); - cout<<"Sorted array is: "; - for(int i=0;i