Flexible application of Binary Search

Source: Internet
Author: User

Binary Search
1. Find any subscript with a key
Train of Thought: If the mid position is smaller than the key, the system moves low to the right. If it is greater than the key, the system moves high to the left. If it is equal to the key, the system returns the mid.
2. Find the first subscript with a key
Train of Thought: If the mid position is smaller than the key, low shifts right. If the value is greater than or equal to the key, high shifts left. If the low position is not out of bounds and the value is equal to the key, low is returned directly. Otherwise, this value does not exist.
3. Find the largest subscript smaller than the key
Train of Thought: If the mid position is smaller than the key, low shifts right. If the value is greater than or equal to the key, high shifts left. If the value is not out of bounds, high is returned. Otherwise, this value does not exist.
4. Find the subscript of the last key
Train of Thought: If the mid position is less than or equal to the key, the low operation shifts right. If the mid position is greater than the key, the high operation shifts left. If the high field is not out of bounds and the value of this position is equal to the key, the high operation is returned. Otherwise, this indicates
5. Find the smallest subscript greater than the key

Train of Thought: If the mid position is less than or equal to the key, low shifts right. If it is greater than the key, high shifts left. If it is not out of bounds, low is returned. Otherwise, this value does not exist.


Code:

# Include "stdafx. H "# include <iostream> using namespace STD; // Binary Search // 1. find any subscript with a key: If the mid position is smaller than the key, the system moves low to the right. If the mid position is greater than the key, the system moves high to the left. If the mid position is greater than the key, the system returns the midint binarysearchanykey (INT narr, int key) {int nlow = 0; int nhigh = nlength-1; int nmid = 0; while (nlow <= nhigh) {nmid = nlow + (nhigh-nlow)> 1); If (narr [nmid]> key) {nhigh = nmid-1 ;} else if (narr [nmid] <key) {nlow = nmid + 1;} else {return nmid ;}} return-1;} // 2. find the subscript with the first key. // train of thought: If the mid position is smaller than the key, low shifts right. If the mid position is greater than or equal to the key, high shifts left. If the low position is not out of bounds and the value of this position is equal to the key, otherwise, int binarysearchfirstkey (INT narr [], int nlength, int key) {int nlow = 0; int nhigh = nlength-1; int nmid = 0; while (nlow <= nhweigh) {nmid = nlow + (nhweigh-nlow)> 1); If (narr [nmid]> = key) // shift to the left of the equal {nhigh = nmid-1;} else {nlow = nmid + 1 ;}} if (nlow> = 0 & nlow <nlength & narr [nlow] = Key) {return nlow;} else {return-1 ;}// 3. find the largest subscript smaller than the key // train of thought: If the mid position is smaller than the key, low shifts right. If it is greater than or equal to the key, high shifts left. If the high value does not cross the border, high is returned directly, otherwise, int binarysearchmaxindexlessthankey (INT narr [], int nlength, int key) {int nlow = 0; int nhigh = nlength-1; int nmid = 0; while (nlow <= nhweigh) {nmid = nlow + (nhweigh-nlow)> 1); If (narr [nmid]> = key) // shift to the left of the equal direction {nhweigh = nmid-1;} else {nlow = nmid + 1 ;}} if (nhweigh >=0 & nhweigh <nlength) {return nhweigh ;} else {return-1 ;}// 4. find the subscript of the last key. // train of thought: If the mid position is less than or equal to the key, low shifts right. If the mid position is greater than the key, high shifts left. If the high position is not out of bounds and the value is equal to the key, otherwise, int binarysearchlastkey (INT narr [], int nlength, int key) {int nlow = 0; int nhigh = nlength-1; int nmid = 0; while (nlow <= nhweigh) {nmid = nlow + (nhweigh-nlow)> 1); If (narr [nmid]> key) {nhigh = nmid-1;} else // shifts to the right even {nlow = nmid + 1 ;}} if (nhweigh> = 0 & nhweigh <nlength & narr [nhweigh] = Key) {return nhweigh;} else {return-1 ;}// 5. find the smallest subscript greater than the key. // train of thought: If the mid position is less than or equal to the key, low shifts right. If it is greater than the key, high shifts left. If low does not cross the border, low is returned directly, otherwise, int binarysearchminindexmorethankey (INT narr [], int nlength, int key) {int nlow = 0; int nhigh = nlength-1; int nmid = 0; while (nlow <= nhweigh) {nmid = nlow + (nhweigh-nlow)> 1); If (narr [nmid]> key) {nhweigh = nmid-1 ;} else // equal shift to the right {nlow = nmid + 1 ;}}if (nlow >=0 & nlow <nlength) {return nlow ;}else {return-1 ;}} void mix (INT narr [], int nlength, int key) {cout <"locate any" <key <"Location:" <binarysearchanykey (narr, nlength, key) <Endl; cout <"locate the location where" <key <"appears for the first time:" <binarysearchfirstkey (narr, nlength, key) <Endl; cout <"locate the location where the maximum value is less than" <key <":" <binarysearchmaxindexlessthankey (narr, nlength, key) <Endl; cout <"locate the last occurrence" <key <"Location:" <binarysearchlastkey (narr, nlength, key) <Endl; cout <"locate the location where the smallest value is greater than" <key <":" <binarysearchminindexmorethankey (narr, nlength, key) <Endl ;} int _ tmain (INT argc, _ tchar * argv []) {int narr [8] = {2, 3, 3, 3, 5, 6, 7}; mix (narr, 8, 3 ); mix (narr, 8, 4); System ("pause"); Return 0 ;}

Running result:



Related Article

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.