# Binary Insert Sort

Source: Internet
Author: User

Main.cpp

Binaryinsertsort

Created by Jason on 16/9/22.

#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:

binary insertion Sorting principle

Baidu Encyclopedia:

**/

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

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.