快速排序算法的实现
用C语言实现快速排序 但是基本代码出来之后程序依然无法正常运行 请教大家这个程序要怎么修改才能正常运行,代码如下:
/**An example of quicsort**/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXITEM 1000
#define Cutoff (3)
typedef int ElementType;
/*这是交换函数*/
void
Swap( ElementType *Lhs, ElementType *Rhs )
{
ElementType Tmp = *Lhs;
*Lhs = *Rhs;
*Rhs = Tmp;
}
/*这是插入算法的函数*/
void
InsertionSort( ElementType A[ ], int N )
{
int j, P;
ElementType Tmp;
for( P = 1; P < N; P++ )
{
Tmp = A[ P ];
for( j = P; j > 0 && A[ j - 1 ] > Tmp; j-- )
A[ j ] = A[ j - 1 ];
A[ j ] = Tmp;
}
}
/*这是找出基准比较点的函数*/
ElementType
Median3( ElementType A[ ], int Left, int Right )
{
int Center = ( Left + Right ) / 2;
if( A[ Left ] > A[ Center ] )
Swap( &A[ Left ], &A[ Center ] );
if( A[ Left ] > A[ Right ] )
Swap( &A[ Left ], &A[ Right ] );
if( A[ Center ] > A[ Right ] )
Swap( &A[ Center ], &A[ Right ] );
}
/*这是快速排序算法*/
void
Qsort( ElementType A[ ], int Left, int Right )
{
int i, j;
ElementType Pivot;
if( Left + Cutoff <= Right )
{
Pivot = Median3( A, Left, Right );
i = Left; j = Right - 1;
for( ; ; )
{
while( A[ ++i ] < Pivot ){ }
while( A[ --j ] > Pivot ){ }
if( i < j )
Swap( &A[ i ], &A[ j ] );
else
break;
}
Swap( &A[ i ], &A[ Right - 1 ] ); /* Restore pivot */
Qsort( A, Left, i - 1 );
Qsort( A, i + 1, Right );
}
else /* Do an insertion sort on the subarray */
InsertionSort( A + Left, Right - Left + 1 );
}
int main()
{
int i;
typedef int ElementType;
ElementType A[MAXITEM];
printf( "随机产生待排序数如下:\n ");
for(i=0;i <1000;i++)
printf( "%d \t ",A[i]=rand()%100000);
printf( "\n对随机数进行快速排序结果如下:\n ");
Swap(&A[0],&A[MAXITEM]);
Median3(A[MAXITEM], 0, MAXITEM);
InsertionSort(A[MAXITEM],MAXITEM);
Qsort(A[MAXITEM], 0, MAXITEM);
system( "PAUSE ");
return 0;
}
我主要是想通过产生随机数 对这些随机数进行快速排序的
[解决办法]
帮顶皆粪
[解决办法]
快速排序, 帮你顶。。。
[解决办法]
ElementType Median3( ElementType A[ ], int Left, int Right )
这个函数没有返回值。。。。。。
[解决办法]
我晕。VC中直接编译如上代码错误。等偶改好了再帖上来
[解决办法]
你写的怎么这么长
去我的blog看看吧 呵呵
快速排序的话 很简单的啊
http://blog.csdn.net/DraculaW/archive/2007/04/07/1555911.aspx