二分查找
C++
#include "iostream"
using namespace std;
void binarySearch(int data_array[], int element, int len)
{
/* 二分查找算法,查找长度为len的数组data_array[]中是否有元素element*/
int low = 0;
int high = len;
while (low <= high)
{
int mid = (low + high)/2;
int guess = data_array[mid];
if (guess == element)
{
cout<<"Element found at "<<mid<<" th index"<<endl ;
return ;
}
else if (guess > element)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
cout<<"Element Not Found"<<endl ;
return ; //number not found
}
int main()
{
int data_array[] = {2,10,23,44,100,121};
int length = sizeof(data_array) / sizeof(int);
binarySearch(data_array, 3, length) ; // not found case
binarySearch(data_array, 2, length) ; // found at corner case
binarySearch(data_array, 44, length) ; //found at middle case
return 0;
}
C++11
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
template <typename T>
int binary_search(const std::vector<T>& list, const int& item) {
int low = 0;
int high = list.size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
T guess = list[mid];
if (guess == item) {
return mid;
}
if (guess > item) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
// this function returns pointer to the found element rather than array index
template <typename T>
const T* binary_search2(const std::vector<T>& list, const T& item) {
const T* low = &list.front();
const T* high = &list.back();
while (low <= high) {
// "guess" is the element in the middle between "high" and "low"
const T* guess = low + ((high - low) / 2);
if (*guess == item)
return guess;
if (*guess > item) {
high = guess - 1;
} else {
low = guess + 1;
}
}
return nullptr;
}
int main() {
std::vector<int> my_list = {1, 3, 5, 7, 9};
const int* binary_search2_result = binary_search2(my_list, 9);
const int* binary_search2_null = binary_search2(my_list, 4); // test finding element that is not in the list
cout << "Binary search for number 3: " << binary_search(my_list, 3) << endl;
cout << "Binary search 2 for number 9 (memory address): " << binary_search2_result << endl;
cout << "Binary search 2 for number 9 (value): " << *binary_search2_result << endl;
if (binary_search2_null == nullptr) {
cout << "4 was not found in the list" << endl;
}
return 0;
}
C:
#include <stdio.h>
int binarySearch(int[], int, int);
int main()
{
int myList[] = {1, 3, 5, 7, 9};
int len = sizeof(myList) / sizeof(myList[0]);
printf("%d\n", binarySearch(myList, 3, len)); // 1
printf("%d\n", binarySearch(myList, -1, len)); //-1
return 0;
}
int binarySearch(int list[], int item, int len)
{
int low = 0;
int high = len;
while (low <= high)
{
int mid = (low + high)/2;
int guess = list[mid];
if (guess == item)
{
return mid;
}
else if (guess > item)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return -1; //number not found
}
Python
def binary_search(list, item):
# low and high keep track of which part of the list you'll search in.
low = 0
high = len(list) - 1
# While you haven't narrowed it down to one element ...
while low <= high:
# ... check the middle element
mid = (low + high) // 2
guess = list[mid]
# Found the item.
if guess == item:
return mid
# The guess was too high.
elif guess > item:
high = mid - 1
# The guess was too low.
else:
low = mid + 1
# Item doesn't exist
return None
my_list = [1, 3, 5, 7, 9]
print(binary_search(my_list, 3)) # => 1
# 'None' means nil in Python. We use to indicate that the item wasn't found.
print(binary_search(my_list, -1)) # => None
占位
pass