forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Binary Insertion sort is a variant of Insertion sorting with binary s…
…earch.
- Loading branch information
1 parent
80496da
commit f6940c7
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Binary Insertion sort is a variant of Insertion sorting in which proper location to insert the selected element is found using the binary search. | ||
*/ | ||
|
||
#include<stdio.h> | ||
|
||
int binarySearch(int a[], int item, int low, int high) | ||
{ | ||
if (high <= low) | ||
return (item > a[low])? (low + 1): low; | ||
|
||
int mid = (low + high)/2; | ||
|
||
if(item == a[mid]) | ||
return mid+1; | ||
|
||
if(item > a[mid]) | ||
return binarySearch(a, item, mid+1, high); | ||
return binarySearch(a, item, low, mid-1); | ||
} | ||
|
||
// Function to sort an array a[] of size 'n' | ||
void insertionSort(int a[], int n) | ||
{ | ||
int i, loc, j, k, selected; | ||
|
||
for (i = 1; i < n; ++i) | ||
{ | ||
j = i - 1; | ||
selected = a[i]; | ||
|
||
// find location where selected sould be inseretd | ||
loc = binarySearch(a, selected, 0, j); | ||
|
||
// Move all elements after location to create space | ||
while (j >= loc) | ||
{ | ||
a[j+1] = a[j]; | ||
j--; | ||
} | ||
a[j+1] = selected; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
scanf("%d",&n) ; | ||
int a[n],i; | ||
for(i = 0; i<n; i++) | ||
{ | ||
scanf("%d",&a[i]); | ||
} | ||
|
||
insertionSort(a, n); | ||
|
||
printf("Sorted array: \n"); | ||
for (i = 0; i < n; i++) | ||
printf("%d ",a[i]); | ||
|
||
return 0; | ||
} |