From 1aa24e66159235a77ff9bb7b9dbbf28ddb9af476 Mon Sep 17 00:00:00 2001 From: Sayanta <42710260+Sayan-dev@users.noreply.github.com> Date: Thu, 20 Jun 2019 12:00:09 +0530 Subject: [PATCH] Update insertion.cpp solving the issue #15 though some changes --- insertion.cpp | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/insertion.cpp b/insertion.cpp index cbffd13..2df1ce0 100644 --- a/insertion.cpp +++ b/insertion.cpp @@ -1,17 +1,45 @@ - #include using namespace std; +int bin_srch(int arr[],int low,int high,int n) +{ + if(high<=low){ + return (n>arr[low]?(low+1):(low)); + } + int mid=(high+low)/2; + if(n==arr[mid]){ + return mid+1; + } + else if(n>arr[mid]) + { + return bin_srch(arr,mid+1,high,n); + } + else if(n= 0 && arr[j] > k) { + + //finding the place where the selected element will be placed within the sorted array on the left side + place=bin_srch(arr,0,j,current); + + //shifting the elements one place right to make room for the selected element + while (j >=place) { arr[j+1] = arr[j]; j--; } - arr[j+1] = k; + //assigning the place for the selected element + arr[j+1] = current; } }