-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathBinary_Insertion_Sort.cpp
76 lines (69 loc) · 1.8 KB
/
Binary_Insertion_Sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
We can use binary search to reduce the number of comparisons in normal insertion sort.
Binary Insertion Sort uses binary search to find the proper location to insert the selected item at each iteration.
In normal insertion sort, it takes O(n) comparisons (at nth iteration) in the worst case.
We can reduce it to O(log n) by using binary search.
*/
#include<bits/stdc++.h>
using namespace std;
//searching element in array a by binary search algoritm
int binarySearch(int a[], int element, int low, int high)
{
if (high <= low){
return (element > a[low]) ? (low + 1) : low;
}
int mid = (low + high) / 2;
if (element == a[mid]){
return mid + 1;
}
if (element > a[mid]){
return binarySearch(a, element, mid + 1, high);
}
return binarySearch(a, element, low, mid - 1);
}
// sorting using insertion sort
void insertionSort(int arr[], int n)
{
// initializing variables
int i, place, j, k, selected;
for (i = 1; i < n; ++i)
{
j = i - 1;
selected = arr[i];
// location where selected sould be inseretd
place = binarySearch(a, selected, 0, j);
// Move all elements after location to create space
while (j >= place)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = selected;
}
}
signed main()
{
//Taking Input
int n; cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
//Applying Sort
//calling sort function
insertionSort(a, n);
cout<<"Sorted Array is :-";
for (int i = 0; i < n; i++){
cout<<arr[i]<<" ";
}
return 0;
}
/* Sample Input
11
35 23 12 17 12 72 31 46 180 88 54
*/
/* Sample Output
Sorted Array is:- 12 12 17 23 31 35 46 54 72 88 180
*/
// Time Complexity -O(n^2)
//Space Complexity -O(n)