- 论坛徽章:
- 0
|
/*已知文件中每条记录长度为50byte, 共20条记录*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OUT_FN "sorted"
void q_sort(FILE *outfp, int left, int right)
{
unsigned char templ[50];
unsigned char tempr[50];
unsigned char tempp[50];
unsigned char temp[50];
int l_hold = left;
int r_hold = right;
int pivot;
fseek(outfp,l_hold*(50l),0);
fread(templ,50,1,outfp);
fseek(outfp,r_hold*(50l),0);
fread(tempr,50,1,outfp);
strcpy(tempp,templ);
while (left < right)
{
while ((strcmp(tempr,tempp) >= 0) && (left < right))
{
right--;
fseek(outfp,right*(50l),0);
fread(tempr,50,1,outfp);
}
if (left != right)
{
fseek(outfp,left*(50l),0);
fwrite(tempr,1,50,outfp);
left++;
}
while ((strcmp (templ,tempp) <= 0) && (left < right))
{
left++;
fseek(outfp,left*(50l),0);
fread(templ,50,1,outfp);
}
if (left != right)
{
fseek(outfp,right*(50l),0);
fwrite(templ, 1,50,outfp);
right--;
}
}
fseek(outfp,left*(50l),0);
fwrite(tempp,1,50,outfp);
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(outfp, left, pivot-1);
if (right > pivot)
q_sort(outfp, pivot+1, right);
}
int main(void)
{
FILE * outfp = fopen(OUT_FN, "rb+");
q_sort(outfp, 0, 19);
fclose(outfp);
} |
|