Data Structure and algorithm binary insert sorting

Source: Internet
Author: User
 

I.AlgorithmThoughts:

Insert sort algorithm idea: Compare the number of inserts with the number of sorted arrays and insert the values according to the size relationship to keep the original size order. (Traverse from the end of the array to the array header)

Binary insert: first, compare the original ordered intermediate number with the new inserted number, then Insert the new number size into the half of the array, and then take the number in the middle of the array again, until the number of arrays obtained is one, the number after the last number is removed, and the new number is placed at the index.

2. simulation source code:

# Include <iostream> using namespace STD; // directly Insert the sorted int arr [15] = {11, 22, 45, 71 }; int list [15]; void binaryinsertsort (INT arr [], int size) {int low, high, temp, value; For (INT I = 0; I <size; I ++) {value = arr [I]; Low = 0; high = I-1; while (low <= high) {temp = (low + high)/2; if (list [temp] <value) Low = temp + 1; elsehigh = temp-1;} For (int K = I; k> = low; k --) {list [k] = list [k-1];} list [low] = value ;}} void show () {for (Int J = 0; j <15; j ++) cout <list [J] <""; cout <Endl;} int main () {binaryinsertsort (ARR, 15 ); cout <"after half insert:" <Endl; show ();}

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.