读书人

c代码-快速排序有关问题

发布时间: 2012-03-14 12:01:12 作者: rapoo

c代码--快速排序问题
高手们帮我看看这段代码的错误,我是个c语言的初学者,大家不要笑话,cpx()要用快速排序实现,我的是乱写的,大家帮帮忙啊~~不胜感激

[解决办法]

void __cdecl qsort (
void *base,
unsigned num,
unsigned width,
int (__cdecl *comp)(const void *, const void *)
)
{
char *lo, *hi; /* ends of sub-array currently sorting */
char *mid; /* points to middle of subarray */
char *loguy, *higuy; /* traveling pointers for partition step */
unsigned size; /* size of the sub-array */
char *lostk[30], *histk[30];
int stkptr; /* stack for saving sub-array to be processed */

/* Note: the number of stack entries required is no more than
1 + log2(size), so 30 is sufficient for any array */

if (num < 2 || width == 0)
return; /* nothing to do */

stkptr = 0; /* initialize stack */

lo = base;
hi = (char *)base + width * (num-1); /* initialize limits */

/* this entry point is for pseudo-recursion calling: setting
lo and hi and jumping to here is like recursion, but stkptr is
prserved, locals aren 't, so we preserve stuff on the stack */
recurse:

size = (hi - lo) / width + 1; /* number of el 's to sort */

/* below a certain size, it is faster to use a O(n^2) sorting method */
if (size <= CUTOFF) {
shortsort(lo, hi, width, comp);
}
else {
/* First we pick a partititioning element. The efficiency of the
algorithm demands that we find one that is approximately the
median of the values, but also that we select one fast. Using
the first one produces bad performace if the array is already
sorted, so we use the middle one, which would require a very
wierdly arranged array for worst case performance. Testing shows
that a median-of-three algorithm does not, in general, increase
performance. */

mid = lo + (size / 2) * width; /* find middle element */
swap(mid, lo, width); /* swap it to beginning of array */


loguy = lo;
higuy = hi + width;

/* Note that higuy decreases and loguy increases on every iteration,
so loop must terminate. */
for (;;) {
/* lo <= loguy < hi, lo < higuy <= hi + 1,
A[i] <= A[lo] for lo <= i <= loguy,
A[i] > = A[lo] for higuy <= i <= hi */

do {
loguy += width;
} while (loguy <= hi && comp(loguy, lo) <= 0);

/* lo < loguy <= hi+1, A[i] <= A[lo] for lo <= i < loguy,
either loguy > hi or A[loguy] > A[lo] */

do {
higuy -= width;
} while (higuy > lo && comp(higuy, lo) > = 0);

/* lo-1 <= higuy <= hi, A[i] > = A[lo] for higuy < i <= hi,
either higuy <= lo or A[higuy] < A[lo] */

if (higuy < loguy)
break;

/* if loguy > hi or higuy <= lo, then we would have exited, so
A[loguy] > A[lo], A[higuy] < A[lo],
loguy < hi, highy > lo */

swap(loguy, higuy, width);

/* A[loguy] < A[lo], A[higuy] > A[lo]; so condition at top


of loop is re-established */
}



swap(lo, higuy, width); /* put partition element in place */



if ( higuy - 1 - lo > = hi - loguy ) {
if (lo + width < higuy) {
lostk[stkptr] = lo;
histk[stkptr] = higuy - width;
++stkptr;
} /* save big recursion for later */

if (loguy < hi) {
lo = loguy;
goto recurse; /* do small recursion */
}
}
else {
if (loguy < hi) {
lostk[stkptr] = loguy;
histk[stkptr] = hi;
++stkptr; /* save big recursion for later */
}

if (lo + width < higuy) {
hi = higuy - width;
goto recurse; /* do small recursion */
}
}
}


--stkptr;
if (stkptr > = 0) {
lo = lostk[stkptr];
hi = histk[stkptr];
goto recurse; /* pop subarray from stack */
}
else
return; /* all subarrays done */
}
[解决办法]
void quicksort(sqlist l,int low,int high)
{int i,j;
if(low <high)
{i=low;j=high;l.r[0]=l.r[i];
do
{
while(i <j&&l.r[j].key> l.r[0].key)
--j;
if(i <j)
{l.r[i]=l.r[j];++i;}
while(i <j&&l.r[i].key <=l.r[0].key)
++i;
if(i <j){
l.r[j]=l.r[i];--j;
}
}while(i!=j);
l.r[i]=l.r[0];
quicksort(l,low,i-1);
quicksort(l,i+1,high);
}
}
[解决办法]
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int compare( const void *arg1, const void *arg2 );

int main( int argc, char **argv )
{
int i;
/* Eliminate argv[0] from sort: */
argv++;
argc--;

/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );

/* Output sorted list: */
for( i = 0; i < argc; ++i )
printf( " %s ", argv[i] );
printf( "\n " );
}

int compare( const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}

读书人网 >C语言

热点推荐