void swap(const void* a, const void* b, int size)
{
assert(a != NULL && b != NULL);
char tmp = 0;
int i = 0;
while (size > 0) {
tmp = *((char*)a + i);
*((char*)a + i) = *((char*)b + i);
*((char*)b + i) = tmp;
++i;
--size;
}
}
void Qsort(void* base, int left, int right, int size, int(*cmp)(const void* a, const void* b))
{
assert(base != NULL && size >= 1 && cmp != NULL); /* left may be < 0 because of the last - 1 */
if (left >= right) return;
char* pleft = (char*)base + left * size;
char* pkey = (char*)base + (left + (right - left) / 2) * size;
swap(pleft, pkey, size);
int last = left;
char* plast = (char*)base + last * size;
for (int i = left + 1; i <= right; ++i) {
char* pi = (char*)base + i * size;
if (cmp(pi, pleft) < 0) {
++last;
plast = (char*)base + last * size;
swap(pi, plast, size);
}
}
swap(pleft, plast, size);
Qsort(base, left, last - 1, size, cmp);
Qsort(base, last + 1, right, size, cmp);
}
void* Bsearch(void* base, int len, int size, const void* key, int(*cmp)(const void* a, const void* b))
{
assert(base != NULL && len >= 0 && size >= 1 && cmp != NULL);
int low = 0;
int high = len - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
char* pmid = (char*)base + mid * size;
if (cmp(pmid, key) < 0) {
low = mid + 1;
}
else if (cmp(pmid, key) > 0) {
high = mid - 1;
}
else {
return pmid;
}
}
return NULL;
}
int cmp_string(const void* a, const void* b)
{
assert(a != NULL && b != NULL);
const char** lhs = (const char**)a;
const char** rhs = (const char**)b;
return strcmp(*lhs, *rhs);
}
int cmp_int(const void* a, const void* b)
{
assert(a != NULL && b != NULL);
const int* lhs = (const int*)a;
const int* rhs = (const int*)b;
if (*lhs < *rhs) {
return -1;
}
else if (*lhs == *rhs) {
return 0;
}
else {
return 1;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[] = { -2, 0, 5, 1, 10, 8, 5, 4, 3, 9 };
int len1 = sizeof(a) / sizeof(a[0]);
printf("before sort:\n");
for (int i = 0; i < len1; ++i) {
printf("%d ", a[i]);
}
printf("\n");
Qsort(a, 0, len1 - 1, sizeof(a[0]), cmp_int);
printf("after sort:\n");
for (int i = 0; i < len1; ++i) {
printf("%d ", a[i]);
}
printf("\n");
const char* b[] = { "what", "chenwei", "skyline", "wel", "dmr" };
int len2 = sizeof(b) / sizeof(b[0]);
printf("before sort:\n");
for (int i = 0; i < len2; ++i) {
printf("%s-->", b[i]);
}
printf("\n");
Qsort(b, 0, len2 - 1, sizeof(b[0]), cmp_string);
printf("after sort:\n");
for (int i = 0; i < len2; ++i) {
printf("%s-->", b[i]);
}
printf("\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[] = { -2, 0, 1, 3, 4, 5, 5, 8, 9, 10 };
int len1 = sizeof(a) / sizeof(a[0]);
int tmp = 5;
int* res1 = (int*)Bsearch(a, len1, sizeof(a[0]), &tmp, cmp_int);
if (res1 != NULL) {
printf("found it\n");
}
else {
printf("Not found\n");
}
const char* str[] = { "chenwei", "dmr", "skyline", "wel", "what" };
int len2 = sizeof(str) / sizeof(str[0]);
const char* p = "chenwei";
char* res2 = (char*)Bsearch(str, len2, sizeof(str[0]), &p, cmp_string);
if (res2 != NULL) {
printf("found it\n");
}
else {
printf("Not found\n");
}
}
qsort与bsearch
原创xingyuan1hao ©著作权
©著作权归作者所有:来自51CTO博客作者xingyuan1hao的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:VT100控制码进行终端扩展显示
下一篇:C陷阱与缺陷(三)--语义陷阱2
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【C语言加油站】qsort函数的模拟实现
【C语言加油站】qsort函数的模拟实现本文详细介绍了使用冒泡排序的逻辑模拟实现可以排序任意类型数组的qsort函数……
C语言 qsort排序函数 冒泡排序 回调函数 void*指针 -
Python函数与模块的精髓与高级特性
本文将介绍 Python 中函数和模块的基本使用方法,并提供一些代码实例。
Python 函数式编程 高级特性 -
库函数qsort、bsearch、snprintf
1.qsort库函数——数组排序void qsort(void *base, size_t nitems, size_t size, in
qsort bsearch snprintf 数组 #include -
QSort
qsort 功 能: 使用快速排序例程进行排序
iostream random pascal numbers struct -
C 库函数 - bsearch()
描述C 库函数 void *bsearch(const void *key, cons
数组 类型转换 #include