qsort()는 N 항목의 선형 목록을 제공함으로써 노래를 부릅니다. 여기서 주어진 항목 (n) 주소는 각 항목의 기준 주소 + 크기를 사용하여 계산할 수 있습니다. 그래서 간단한 것으로부터 시작하고, 간단히 말해 포인터 목록을 의미합니다.
먼저 버퍼의 원형은 복제본을 원본 (간단히 말하자면 1 바이트 미만)에 붙여 넣음으로써 에뮬레이트 될 수 있지만 약 1 바이트를 말하지 않을 것입니다. 나는.
"qwer" ==> "qwerqwer"
이
을 수행 할 수 있습니다 : 이제 당신이
char *buff = malloc(2 * blocksize);
memcpy(buff, to_sort, blocksize);
memcpy(buff+blocksize, to_sort, blocksize);
오프셋 0 .. (블록 크기-1), 각각의없이 서로 비교 될 수 문자의 블록 크기, 인 특수 포인터 수학.
다음에, 실제로 정렬이 경우,
char** ptrs = malloc(sizeof(char*) * blocksize);
for (i=0;i<blocksize;i++)
ptrs[i] = buff+i;
다음에, 두 개의 "아이템"비교 함수 포인터의리스트를 작성. 주소로 전달 된 항목은 문자열에 대한 포인터입니다. 다시, 주소는 왼쪽 및 오른쪽으로 과거로 우리가 두 개의 char *를 찾을 수있는 메모리 위치입니다.
int block_compare(const void *left, const void *right)
{
// memcmp would work for most platforms, but not all, so...
return strncmp(*(char **)left, *(char **)right, blocksize);
}
마지막
은 (이 qsort를 보낼) 등 :
qsort(ptrs, blocksize, sizeof(char*), block_compare);
최종 출력 포인터의 블록 크기 길이리스트는 제조로 될 주소 자체 하지 CHAR * 아르 순환 버퍼, 각각은 블록 크기의 청크를 참조합니다. 위에서 설명한 모든의 전문은 다음과 같습니다
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
size_t blocksize = 0;
int block_compare(const void *left, const void *right)
{
// memcmp would work for most platforms, but not all, so...
return strncmp(*(char **)left, *(char **)right, blocksize);
}
int main(int argc, char* argv[])
{
char to_sort[] = "qwer";
size_t i = 0;
// set blockize
blocksize = strlen(to_sort);
char *buff = malloc(2 * blocksize);
memcpy(buff, to_sort, blocksize);
memcpy(buff+blocksize, to_sort, blocksize);
char ** ptrs = malloc(blocksize * sizeof(char*));
for (i=0;i<blocksize;++i)
ptrs[i] = buff+i;
// now send the pointer list to qsort()
qsort(ptrs, blocksize, sizeof(*ptrs), block_compare);
// ptrs is sorted. do with it what you will.
for (i=0;i<blocksize;i++)
{
fwrite(ptrs[i], sizeof(char), blocksize, stdout);
fwrite("\n", sizeof(char), 1, stdout);
}
fflush(stdout);
free(ptrs);
free(buff);
return EXIT_SUCCESS;
}
는 "qwer"를 사용하여 생성합니다 "asubstantiallylongerstringtest"
allylongerstringtestasubstanti
antiallylongerstringtestasubst
asubstantiallylongerstringtest
bstantiallylongerstringtestasu
erstringtestasubstantiallylong
estasubstantiallylongerstringt
gerstringtestasubstantiallylon
gtestasubstantiallylongerstrin
iallylongerstringtestasubstant
ingtestasubstantiallylongerstr
llylongerstringtestasubstantia
longerstringtestasubstantially
lylongerstringtestasubstantial
ngerstringtestasubstantiallylo
ngtestasubstantiallylongerstri
ntiallylongerstringtestasubsta
ongerstringtestasubstantiallyl
ringtestasubstantiallylongerst
rstringtestasubstantiallylonge
stantiallylongerstringtestasub
stasubstantiallylongerstringte
stringtestasubstantiallylonger
substantiallylongerstringtesta
tantiallylongerstringtestasubs
tasubstantiallylongerstringtes
testasubstantiallylongerstring
tiallylongerstringtestasubstan
tringtestasubstantiallylongers
ubstantiallylongerstringtestas
ylongerstringtestasubstantiall
남자가 내가 그했다 희망 사용
erqw
qwer
rqwe
werq
또 다른 샘플 무엇을 찾고 있었다. (아휴).
나는'xi'와'yi'를 역 참조하려고 할 때 충돌하지 않는다는 것에 놀랐습니다. 32 비트 또는 64 비트 시스템을 사용하고 계십니까? –
'q'가 'r'앞에 오면 예제의 결과는'(2, 0, 3, 1)'이어야합니다. –
@AdamRosenfield 예, 저도 저를 놀라게했습니다. 예상대로 내 (64 비트) 상자에서 충돌이 발생했습니다. –