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; } }