《ASCE1885的源码分析》の基于冒泡排序的二分查找模板
##########################################################################
ASCE1885的声明:本文源代码归属于:
author: Gonzales Cenelia
homepage: www.ai-search.4t.com
有增改!
##########################################################################
本代码实现二分查找的功能,查找前对数组排序使用的是冒泡排序算法。使用的开发环境是Dev C++ 4.9.9.2。
程序头文件是bisearch.h,实现了程序的主体功能:
namespace ASCE1885 {
//二分查找函数,注意参数array中的元素是排好序的
template <typename T>
int biSearch(int low, int high, T *array,
T search, int (*comp)(T a, T b))
{
int mid = 0;
while(low <= high)
{
mid = (low + high)/2;
if(comp(array[mid], search) == 0) //等于要找的数
{
return mid;
} else if(comp(array[mid], search) < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
//交换函数
template <typename T>
void swap(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
//比较函数
template <typename T>
int comp(T a, T b)
{
if(a == b)
return 0;
else if(a > b)
return 1;
return -1;
}
//冒泡排序
template <typename T>
void bubbleSort(T *array, size_t arraySize, int (*comp)(T a, T b))
{
int cmpResult;
for(int i=arraySize-1; i>0; --i)
{
for(int j=0; j<i; ++j)
{
cmpResult = comp(array[j], array[j+1]);
if(cmpResult > 0)
{
swap(array[j], array[j+1]);
}
}
}
}
}
调用上面函数的文件是Template Binary Search.cpp:
#include <iostream>
#include <string>
#include "bisearch.h"
using namespace ASCE1885;
int _comp(char *a, char *b);
template<typename T>
void print_array(T *array, size_t array_size)
{
for(int i = 0; i < array_size; ++i)
{
std::cout << " " << array[i] << std::endl;
}
}
void example1();
void example2();
void example3();
int main()
{
example2();
system("pause");
return 0;
}
int _comp(char *a, char *b)
{
return strcmp(a, b);
}
void example1()
{
int array[] = {-1, 7, 9, 0, 17, 13, 73};
int array_size = sizeof(array)/sizeof(array[0]);
bubbleSort<int>(array, array_size, comp);
std::cout << "Array elements:/n";
print_array(array, array_size);
std::cout << "/nBinary Search/n";
std::cout << " search: 13/n";
std::cout << " search result: " << biSearch<int>(0, array_size, array, 13, comp);
std::cout<<std::endl;
}
void example2()
{
char array[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
int array_size = sizeof(array)/sizeof(array[0]);
std::cout << "Array elements:/n";
print_array(array, array_size);
std::cout << "/nBinary Search:/n";
std::cout << " enter your search: ";
char search;
std::cin >> search;
std::cout << " search result: " << biSearch<char>(0, array_size, array, search, comp);
std::cout<<std::endl;
}
void example3()
{
char *array[] = {"Michelle", "Laetitia", "Cindy", "Christina", "Monica", "Claudia"};
int array_size = sizeof(array)/sizeof(array[0]);
std::cout << "/nArray elements /n/nbefore sorting:/n";
print_array(array, array_size);
bubbleSort(array, array_size, _comp);
std::cout << "/nafter:/n";
print_array(array, array_size);
std::cout << "/nBinary Search/n";
std::string search = "Claudia";
std::cout << " search: " << search;
std::cout << "/n search result:= " << biSearch(0, array_size, array, (char*)search.c_str(), _comp);
std::cout<<std::endl;
}
posted on 2010-09-15 16:30 android开发实例 阅读(275) 评论(0) 编辑 收藏 举报