#include <cstdio>
#include <cstdint>
#include <ctime>
#include <cstring>
#include <cstdlib> void GetPivot(uint8_t In_ui8A[], int64_t In_i64Begin, int64_t In_i64End)
{
uint8_t ui8Tmp = ;
uint64_t ui64PivotIdx = ;
if (In_ui8A == NULL || In_i64Begin >= In_i64End)
{
goto fun_ret;
} ui64PivotIdx = (In_i64End - In_i64Begin + ) / ;
ui8Tmp = In_ui8A[In_i64End];
In_ui8A[In_i64End] = In_ui8A[ui64PivotIdx + In_i64Begin];
In_ui8A[ui64PivotIdx + In_i64Begin] = ui8Tmp;
fun_ret:
return;
} void QSort(uint8_t In_ui8A[], int64_t In_i64Begin, int64_t In_i64End)
{
int64_t i = , j = ;
uint8_t ui8Pivot = , ui8Tmp = ;;
if (In_ui8A == NULL || In_i64Begin >= In_i64End)
{
goto fun_ret;
}
GetPivot(In_ui8A, In_i64Begin, In_i64End); i = In_i64Begin - ;
j = In_i64Begin;
ui8Pivot = In_ui8A[In_i64End]; do
{
if (In_ui8A[j] <= ui8Pivot)
{
ui8Tmp = In_ui8A[++ i];
In_ui8A[i] = In_ui8A[j];
In_ui8A[j] = ui8Tmp;
}
j ++;
} while (j < In_i64End); ui8Tmp = In_ui8A[i + ];
In_ui8A[i + ] = ui8Pivot;
In_ui8A[In_i64End] = ui8Tmp; QSort(In_ui8A, In_i64Begin, i);
QSort(In_ui8A, i + , In_i64End); fun_ret:
return;
} int compare (const void *p1, const void *p2)
{
if (*(uint8_t*)p1 < *(uint8_t*)p2) return -;
else if (*(uint8_t*)p1 == *(uint8_t*)p2) return ;
else return ;
} void main(int argc, char **argv)
{
uint8_t *pui8Buf1 = NULL, *pui8Buf2 = NULL;
FILE *pf = NULL;
long lFileSize = ;
clock_t ctBegin = , ctQSortTime = , ctMyTime = ; pf = fopen(argv[], "rb");
fseek(pf, , SEEK_END);
lFileSize = ftell(pf);
fseek(pf, , SEEK_SET);
pui8Buf1 = (uint8_t *)malloc(lFileSize);
pui8Buf2 = (uint8_t *)malloc(lFileSize);
memset(pui8Buf1, , lFileSize);
memset(pui8Buf2, , lFileSize);
fread(pui8Buf1, sizeof(pui8Buf1[]), lFileSize, pf);
memcpy(pui8Buf2, pui8Buf1, lFileSize);
fclose(pf);
pf = NULL; ctBegin = clock();
QSort(pui8Buf1, , lFileSize - );
ctMyTime = clock() - ctBegin; ctBegin = clock();
qsort((void *)pui8Buf2, lFileSize, sizeof(pui8Buf2[]), compare);
ctQSortTime = clock() - ctBegin; if (!memcmp((void *)pui8Buf1, (void *)pui8Buf2, lFileSize))
{
printf("MyQSort::%lu\nqsort::%lu\n", ctMyTime, ctQSortTime);
}
else
{
printf("FUCK!!!\n");
}
}

排序一个7,680字节的PE,自己实现的用了10+毫秒,qsort用了不到1毫秒,尼玛伤自尊了。

04-16 21:44