Main.cpp
Binaryinsertsort
Created by Jason on 16/9/22.
copyright©2016 years Jason. All rights reserved.
#include <iostream>
Using namespace std;
#define ARR_SIZE (Array,len) (len = (sizeof (ARR)/sizeof (arr[0] ));
The binary lookup code is as follows:
/**
binary Find principle:
Baidu Encyclopedia:Http://baike.baidu.com/link?url=uxMHSYOcdy9RUc6ooMe_K4rbeg6zTIcaoQ3Jkcuve_ Np89p-atvjg16zociaw40xjgh9tmnkhflhwl7bln1z2nb-na-zbivtczksuuc65xzjouieiuagdjhrkeoet2vinr-f3cji92xwtmbzka0mc23t7sfqbptfgfl KYLT-EYKY6GJN7UPR6NITRMXDRF2B
binary insertion Sorting principle
Baidu Encyclopedia:
http://baike.baidu.com/link?url= 7hiqinalychnjnhobbuea3xyivln5hpidrv8nvpyjsnlkiybpk4gx-pemzbknlpyyzezvwwnitny81xpj1b_nq
**/
void binaryinsertsort (int array[],int N)// pass array and number of elements
{
int i,j,mid,low,high,temp;
for(i = 1; i < n; ++i)// I read some other people write binary the insertion algorithm starts with subscript 1,i = 2;i <=n, andSave the array [i] to array[0]
{
temp = Array[i]; // Assign the i+1 element to temp ( array starts with subscript 0 )
Low = 0; Initialize Low ,Array[low] represents the first 1 elements of an array
High = i; // Initialize high ,Array[high] represents the last element that has been inserted
while(Low <= High) // constant binary ... .
{
Mid = (low + high)/ 2; Calculate middle position
if (Temp > Array[mid])
{
// Insert value greater than middle value
Low = mid + 1;
}Else{
// insertion value is less than intermediate value
High = mid- 1;
}
}
for(j=i-1; j >= Low;--j)
{
//Move the array that needs to be moved backwards
array[j+1] = array[j];
}
// inserts a value into the specified position
Array[low] = temp;
}
}
int Main (int argc, const Char * argv[]) {
int arr[] = {6,8,1, 3,7, 4};
int len;
arr_size(arr,len);
binaryinsertsort(arr,len);
for(int i=0; i<len;i++)
{
cout << arr[i] << "";
}
cout << endl;
}
Binary Insert Sort