好久没写这些底层算法了,趁这个周末有空,顺便实现了下。程序里的指针是否为空都没有判断,毕竟这些玩意都是追求速度的嘛!
如有不当之处请指出,欢迎讨论!
下面的程序在VS2008下测试通过,以下是运行结果:
1.头文件 myalgorithm.h
#ifndef _MYALGORITHM_H_13551534
#define _MYALGORITHM_H_13551534
typedef bool (*FuncCompare)(void *p1, void *p2);
typedef void (*FuncUser)(void *p);
void ForEach(void *begin, void *end, int nTypeSize, FuncUser func);
void* PreProcess(void *begin, void *end, int nTypeSize, FuncCompare func);
void Qsort(void *begin, void *end, int nTypeSize, FuncCompare func);
#endif
2.源文件 myalgorithm.cpp
#include "myalgorithm.h"
#include "string.h"
void ForEach(void *begin, void *end, int nTypeSize, FuncUser func)
{
char *p = (char*)begin;
while (p != end)
{
func(p);
p += nTypeSize;
}
}
void* PreProcess(void *begin, void *end, int nTypeSize, FuncCompare func)
{
char* i = (char*)begin;
char* j = (char*)end;
j -= nTypeSize;
char* pivot = new char[nTypeSize];
memcpy(pivot, begin, nTypeSize);
while(i < j)
{
while(i < j && func(pivot, j)) j -= nTypeSize;
if (i < j)
{
memcpy(i, j, nTypeSize); i += nTypeSize;
}
while(i < j && func(i, pivot)) i += nTypeSize;
if (i < j)
{
memcpy(j, i, nTypeSize); j -= nTypeSize;
}
}
memcpy(i, pivot, nTypeSize);
delete pivot;
pivot = NULL;
return i;
}
void Qsort(void *begin, void *end, int nTypeSize, FuncCompare func)
{
if (begin != end)
{
char *pivot = (char*)PreProcess(begin, end, nTypeSize, func);
Qsort(begin, pivot, nTypeSize, func);
Qsort(pivot + nTypeSize, end, nTypeSize, func);
}
}
3. 客户端实现qsort.cpp
#include "stdio.h"
#include "stdlib.h"
#include "myalgorithm.h"
typedef struct _PERSON
{
int nID;
char *szName;
}PERSON;
bool CompareInt(void *p1, void *p2)
{
int a = *(int*)p1;
int b = *(int*)p2;
return a < b;
}
bool CompareStr(void *p1, void *p2)
{
char *c1 = *(char**)p1;
char *c2 = *(char**)p2;
while (*c1!= '\0' && *c2!= '\0')
{
if (*c1 > *c2)
{
return false;
}
else if (*c1 < *c2)
{
return true;
}
++c1;
++c2;
}
return true;
}
bool ComparePersonById(void *p1, void *p2)
{
PERSON *person1 = (PERSON*)p1;
PERSON *person2 = (PERSON*)p2;
return person1->nID < person2->nID;
}
bool ComparePersonByName(void *p1, void *p2)
{
PERSON *person1 = (PERSON*)p1;
PERSON *person2 = (PERSON*)p2;
return CompareStr(&person1->szName, &person2->szName);
}
void PrintInt(void *p)
{
int a = *(int*)p;
printf("%d ", a);
}
void PrintStr(void *p)
{
char **str = (char**)p;
printf("%s ", *str);
}
void PrintPersonInfo(void *p)
{
PERSON *person = (PERSON*)p;
printf("ID: %d Name: %s\n", person->nID, person->szName);
}
int _tmain(int argc, _TCHAR* argv[])
{
//整型数组排序
printf("************整型数组排序************\n");
int arr[] = {5, 1, 3, 6, 2, 1, 9, 7, 3, 0};
printf("原始数据:\n");
ForEach(arr, arr + 10, 4, PrintInt);
printf("\n");
Qsort(arr, arr+10, 4, CompareInt);
printf("排序后:\n");
ForEach(arr, arr + 10, 4, PrintInt);
printf("\n\n");
//字符串排序
printf("************字符串排序************\n");
printf("原始数据:\n");
char* szName[] = {"jack", "liufeng", "zhangsan", "aniu", "wangqiang", "hanmei"};
ForEach(szName, szName+6, 4, PrintStr);
printf("\n");
Qsort(szName, szName+6, 4, CompareStr);
printf("排序后:\n");
ForEach(szName, szName+6, 4, PrintStr);
printf("\n\n");
//结构体排序
PERSON person[6] = {34, "zhangsan", 23, "lisi", 53, "hamei", 12, "zhangqiang", 67, "huyong", 45, "qianqian"};
// 1. 按ID排序
printf("************结构体排序************\n");
printf("原始数据:\n");
ForEach(person, person + 6, sizeof(PERSON), PrintPersonInfo);
printf("\n按ID排序:\n");
Qsort(person, person + 6, sizeof(PERSON), ComparePersonById);
ForEach(person, person + 6, sizeof(PERSON), PrintPersonInfo);
printf("\n");
// 2. 按名称排序
printf("\n按名称排序:\n");
Qsort(person, person + 6, sizeof(PERSON), ComparePersonByName);
ForEach(person, person + 6, sizeof(PERSON), PrintPersonInfo);
printf("\n");
system("pause");
return 0;
}