Binary Search and quick sorting, binary search and sorting

Source: Internet
Author: User

Binary Search and quick sorting, binary search and sorting

The central idea of fast sorting is to select an element to change to the front greater than this element through a sort, and change the element smaller to the back. Then proceed to the sorting.

Void sort (int datas [], int low, int high) {if (low> = high) {return;} int first = low; int last = high; int key = datas [first]; while (first <last) {while (first <last & datas [last]> = key) {last --; // 10, 3, 5, 23, 9,12, 22} datas [first] = datas [last]; // [9, 12, 22 --------> 10 = 9 last = 4 from the end, find something smaller than the key (the first element, then, put the bigger key into the header while (first <last & datas [first] <= key) {first ++; // 9, 3, 5, 23, 9, 12, 22 first = 3} datas [last] = datas [first]; // [9], found from the header bigger than the key (the first element, then put the smaller key to the end} datas [first] = key; sort (datas, low, first-1); sort (datas, first + 1, high );}
The idea of binary search is to compare the intermediate element with the target element. If it is larger than the target, it is lower than the lower limit. If it is smaller than the target, it is lower than the upper limit.
int find(int datas[],int dst){int low = 0;int high = datas.length -1;while (low <= high) {int middle = (low + high) >> 1;if(datas[middle] == dst){return middle;}else if (datas[middle] < dst) {low = middle;}else {high = middle;}}return -1;
}

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.