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_kylin