Binary Insert Sort

Source: Internet
Author: User

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

Contact Us

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.