The code on the wiki:
int binary_search (int a[], int key, int imin, int imax) {//Continue searching while [Imin,imax] is no T empty while (IMAX >= imin) {//Calculate the midpoint for roughly equal partition int imid = Midpoint ( Imin, IMAX); if (a[imid] = = key)//key found at index IMiD return imid; Determine which subarray to search else if (A[imid] < keys)//change min index to search upper Subarray Imin = IMiD + 1; else//Change Max index to search lower subarray IMAX = imid-1; }//key is not found return key_not_found;} The
uses two algorithms below.
int Binary_search () {int size; Char keys[] = "9290"; Char words[][6] ={"1", "2", "+", "a", "222", "3", "5", "", "All", "};size", "The" "" "," "929," "" "", "" "" ", );p rintf ("the first algorithm sizeof =%d\n", size); int head = 0; int tail = size;int cursor = (head + tail)/2; int result = 999;int count = 0;printf ("keys= \"%s\ "\ n", keys);p rintf ("words[i][] = \ n"); for (int i = 0; i < size; ++i) printf ("[%d]=\"%s\ "", I, words[i]); do {result = strcmp (keys, words[cursor]);p rintf ("\ncount =%d cursor =%d head =%d, tail =%d, \ n", count++ , cursor, head, tail);p rintf ("words[%d] = \"%s\ ", result =%d \ n", cursor, words[cursor], result); if (Result > 0) {head = cursor; } else if (Result < 0) {tail = cursor; }else{break;//find a matching key key combination}cursor = (head + tail)/2; } while (head < cursor) && (cursor <= tail); ((head <= cursor) && (cursor < tail)) printF ("\ n------------------\ncursor =%d head =%d, tail =%d, \ n", cursor, head, tail);p rintf ("words[%d] = \"%s\ ", result =%d \ n ", cursor, words[cursor], result);p rintf (" ====================\n "); int left = 0, right = size-1, middle = 0; , result = 0;count = 0;printf ("second algorithm sizeof =%d\n", size);p rintf ("keys= \"%s\ "\ n", keys);p rintf ("words[i][] = \ n"); for (int i = 0; i < size; ++i) printf ("[%d]=\"%s\ "", I, Words[i]);p rintf ("\nmiddle =%d, left =%d, right =%d, \ n", mid Dle, left <= right) {middle = (left + right) >> 1;result = strcmp (keys, Words[middle]);p rintf ( "Count =%d middle =%d, left =%d, right =%d, \ n", count++, middle, left, right);p rintf ("words[%d] = \"%s\ ", result =%d \ n ", middle, words[middle], result); if (Result > 0) {left = middle + 1;} else if (Result < 0) right = Middle-1;else break;} printf ("---------------------\nmiddle =%d, left =%d, right =%d, \ n", middle, left, right);p rintf ("words[%d] = \"%s\ " , result =%d \ n ", middle, words[middle], result); return cursor;}
The first algorithm sizeof = 12
keys= "9290"
Words[i][] =
[0]= "1" [1]= "2" [2]= "21" [3]= "22" [4]= "222" [5]= "3" [6]= "5" [7]= "66" [8]= "90" [9]
]= "900" [10]= "91" [11]= "929"
Count = 0 Cursor = 6 head = 0, tail = 12,
WORDS[6] = "5", result = 1
Count = 1 cursor = 9 head = 6, tail = 12,
WORDS[9] = "" ", result = 1
Count = 2 Cursor = Ten head = 9, tail = 12,
WORDS[10] = "a", result = 1
Count = 3 Cursor = One head = ten, tail = 12,
WORDS[11] = "929", result = 1
------------------
cursor = one head = one, tail = 12,
WORDS[11] = "929", result = 1
====================
The second algorithm sizeof = 12
keys= "9290"
Words[i][] =
[0]= "1" [1]= "2" [2]= "21" [3]= "22" [4]= "222" [5]= "3" [6]= "5" [7]= "66" [8]= "90" [9]
]= "900" [10]= "91" [11]= "929"
Middle = 0, left = 0, right = 12,
Count = 0 middle = 6, left = 0, right = 12,
WORDS[6] = "5", result = 1
Count = 1 middle = 9, left = 7, right = 12,
WORDS[9] = "" ", result = 1
Count = 2 middle = one, left = ten, right = 12,
WORDS[11] = "929", result = 1
---------------------
Middle = 12, left =.
WORDS[12] = "hot-hot 9290", result = 1
Please press any key to continue ...
Be busy with your work and have the opportunity to study the problem of efficiency again.
The problem of preventing overflow in the use of binary lookup algorithm