Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions insertion.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@


#include<iostream>
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<arr[mid])
{
return bin_srch(arr,low,mid-1,n);
}


}
int insertion(int arr[], int n) {
int i, j, k;
for (i=0; i<n; i++) {
k = arr[i];
int i, j, k,place,current;



for (i=1; i<n; ++i) {
current= arr[i];
j=i-1;
while (j >= 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;
}
}

Expand Down