Print? // BSearch. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
Template <class T>
Void PrintfNum (T a [], const int & n );
/**
* Search n in a [], return the index, if not find, return-1.
*/
Template <class T>
Int BSearch (T a [], const int & length, const int & n ){
Int left = 0, right = length-1;
While (left <= right ){
Int middle = (left + right)/2;
// Cout <"middle:" <middle <", left:" <left <", right:" <right <endl;
If (n <a [middle]) {
Right = middle-1;
} Else if (n> a [middle]) {
Left = middle + 1;
} Else {
Return middle;
}
}
Return-1;
}
/**
* A better one
* Search n in a [], return the index, if not find, return-1.
*/
Template <class T>
Int BetterBSearch (T a [], const int & length, const int & n ){
Int left =-1, right = length-1;
While (left + 1! = Right) {// left <right & a [left] <n <= a [right]
Int middle = (left + right)/2;
If (a [middle] <n) {// a [left] <n
Left = middle;
} Else {// a [right]> = n
Right = middle;
}
}
If (right> = length | a [right]! = N) // no find
Return-1;
Return right;
}
/**
* Test function
*/
Bool TestBSearch (){
Const int NUM = 20;
Int BeginNum = 10;
Int t [NUM];
For (int I = 0; I <NUM; ++ I ){
T [I] = BeginNum;
++ BeginNum;
}
Bool result = true;
For (int j = 0; j <NUM; ++ j ){
If (BSearch (t, NUM, t [j])! = J ){
Result = false;
}
}
// Test no find
If (BSearch (t, NUM, t [0]-1 )! =-1 | BSearch (t, NUM, t [NUM-1] + 1 )! =-1 ){
Result = false;
}
Return result;
}
/**
* Test function
*/
Bool TestBetterBSearch (){
Const int NUM = 20;
Int BeginNum = 10;
Int t [NUM];
For (int I = 0; I <NUM; ++ I ){
T [I] = BeginNum;
++ BeginNum;
}
Bool result = true;
For (int j = 0; j <NUM; ++ j ){
If (BetterBSearch (t, NUM, t [j])! = J ){
Result = false;
}
}
// Test no find
If (BetterBSearch (t, NUM, t [0]-1 )! =-1 | BetterBSearch (t, NUM, t [NUM-1] + 1 )! =-1 ){
Result = false;
}
Return result;
}
Int main (int argc, char * argv [])
{
Const int NUM = 10;
Int t [NUM] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 20 };
PrintfNum (t, NUM );
For (int I = 0; I <NUM; ++ I ){
Cout <t [I] <"was at index:" <BSearch (t, NUM, t [I]) <endl;
}
Cout <"searching 100 in array t, result:" <BSearch (t, NUM, 100) <endl;
Cout <endl;
Cout <"BSearch test result:" <TestBSearch () <endl;
Cout <"BetterBSearch test result:" <TestBetterBSearch () <endl;
Return 0;
}
Template <class T>
Void PrintfNum (T a [], const int & n ){
For (int I = 0; I <n; ++ I ){
Cout <a [I] <",";
}
Cout <endl;
}
// BSearch. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
Template <class T>
Void PrintfNum (T a [], const int & n );
/**
* Search n in a [], return the index, if not find, return-1.
*/
Template <class T>
Int BSearch (T a [], const int & length, const int & n ){
Int left = 0, right = length-1;
While (left <= right ){
Int middle = (left + right)/2;
// Cout <"middle:" <middle <", left:" <left <", right:" <right <endl;
If (n <a [middle]) {
Right = middle-1;
} Else if (n> a [middle]) {
Left = middle + 1;
} Else {
Return middle;
}
}
Return-1;
}
/**
* A better one
* Search n in a [], return the index, if not find, return-1.
*/
Template <class T>
Int BetterBSearch (T a [], const int & length, const int & n ){
Int left =-1, right = length-1;
While (left + 1! = Right) {// left <right & a [left] <n <= a [right]
Int middle = (left + right)/2;
If (a [middle] <n) {// a [left] <n
Left = middle;
} Else {// a [right]> = n
Right = middle;
}
}
If (right> = length | a [right]! = N) // no find
Return-1;
Return right;
}
/**
* Test function
*/
Bool TestBSearch (){
Const int NUM = 20;
Int BeginNum = 10;
Int t [NUM];
For (int I = 0; I <NUM; ++ I ){
T [I] = BeginNum;
++ BeginNum;
}
Bool result = true;
For (int j = 0; j <NUM; ++ j ){
If (BSearch (t, NUM, t [j])! = J ){
Result = false;
}
}
// Test no find
If (BSearch (t, NUM, t [0]-1 )! =-1 | BSearch (t, NUM, t [NUM-1] + 1 )! =-1 ){
Result = false;
}
Return result;
}
/**
* Test function
*/
Bool TestBetterBSearch (){
Const int NUM = 20;
Int BeginNum = 10;
Int t [NUM];
For (int I = 0; I <NUM; ++ I ){
T [I] = BeginNum;
++ BeginNum;
}
Bool result = true;
For (int j = 0; j <NUM; ++ j ){
If (BetterBSearch (t, NUM, t [j])! = J ){
Result = false;
}
}
// Test no find
If (BetterBSearch (t, NUM, t [0]-1 )! =-1 | BetterBSearch (t, NUM, t [NUM-1] + 1 )! =-1 ){
Result = false;
}
Return result;
}
Int main (int argc, char * argv [])
{
Const int NUM = 10;
Int t [NUM] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 20 };
PrintfNum (t, NUM );
For (int I = 0; I <NUM; ++ I ){
Cout <t [I] <"was at index:" <BSearch (t, NUM, t [I]) <endl;
}
Cout <"searching 100 in array t, result:" <BSearch (t, NUM, 100) <endl;
Cout <endl;
Cout <"BSearch test result:" <TestBSearch () <endl;
Cout <"BetterBSearch test result:" <TestBetterBSearch () <endl;
Return 0;
}
Template <class T>
Void PrintfNum (T a [], const int & n ){
For (int I = 0; I <n; ++ I ){
Cout <a [I] <",";
}
Cout <endl;
}