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;
}