不会吧,一般数据结构书上都有的
我做过作业,这是老师写的参考答案:
#include <stdio.h>
#define MAXSIZE 10void mysort(int L, int R, int *a);void main( )
{
int array[MAXSIZE] = {0};
int L, R, i, iNumber; printf("请输入待排序的元素个数:");
scanf("%d", &iNumber);
if((iNumber <= 0) || (iNumber > 10))
{
printf("元素个数必须大于0且小于%d。\n", MAXSIZE);
return;
} for(i = 0; i < iNumber; i++)
{
printf("请输入第%d个数据:", i+1);
scanf("%d", &array[i]);
}
L = 0;
R = iNumber - 1;
mysort(L, R, array);
printf("排序的结果是:\n");
for(i = 0; i < iNumber; i++) printf("%d ", array[i]);
printf("\n");}void mysort(int L, int R, int *a)
{
int ll, rr, value; ll = L;
rr = R; value = a[L];
while(1)
{
while((L < R) && (a[R] >= value)) R--;
if(L < R)
{
a[L] = a[R];
L++;
}
else break; while((L < R) && (a[L] <= value)) L++;
if(L < R)
{
a[R] = a[L];
R--;
}
else break;
}
a[L] = value; if(ll < L-1) mysort(ll, L-1, a);
if(L+1 < rr) mysort(L+1, rr, a);
}
我做过作业,这是老师写的参考答案:
#include <stdio.h>
#define MAXSIZE 10void mysort(int L, int R, int *a);void main( )
{
int array[MAXSIZE] = {0};
int L, R, i, iNumber; printf("请输入待排序的元素个数:");
scanf("%d", &iNumber);
if((iNumber <= 0) || (iNumber > 10))
{
printf("元素个数必须大于0且小于%d。\n", MAXSIZE);
return;
} for(i = 0; i < iNumber; i++)
{
printf("请输入第%d个数据:", i+1);
scanf("%d", &array[i]);
}
L = 0;
R = iNumber - 1;
mysort(L, R, array);
printf("排序的结果是:\n");
for(i = 0; i < iNumber; i++) printf("%d ", array[i]);
printf("\n");}void mysort(int L, int R, int *a)
{
int ll, rr, value; ll = L;
rr = R; value = a[L];
while(1)
{
while((L < R) && (a[R] >= value)) R--;
if(L < R)
{
a[L] = a[R];
L++;
}
else break; while((L < R) && (a[L] <= value)) L++;
if(L < R)
{
a[R] = a[L];
R--;
}
else break;
}
a[L] = value; if(ll < L-1) mysort(ll, L-1, a);
if(L+1 < rr) mysort(L+1, rr, a);
}
public static void swap(Object[] array, int i, int j){
if(i==j) return;
Object temp=array[i]; array[i]=array[j]; array[j]=temp;
} public static void reverse(Object[] array){
reverse(array,0,array.length-1);
} public static void reverse(Object[] array, int left, int right){
for(int i=left,j=right;i<j;i++,j--){
swap(array,i,j);
}
}
public static void quickSort(Comparable[] array){
qsort(array,0,array.length-1,true);
}
public static void quickSort(Comparable[] array, boolean ascending){
qsort(array,0,array.length-1,ascending);
}
public static void qsort(Comparable[] array, int left, int right, boolean ascending){
int pivot=(left+right)/2;
swap(array,pivot,right);
int k=partition(array,left-1,right,ascending);
swap(array,k,right);
if((k-left)>1) qsort(array,left,k-1,ascending);
if((right-k)>1) qsort(array,k+1,right,ascending);
}
public static int partition(Comparable[] array, int left, int right, boolean ascending){
Comparable pivot=array[right];
do{
while(array[++left].compareTo(pivot)<=0==ascending);
while((right!=0) && (array[--right].compareTo(pivot)>=0==ascending));
swap(array,left,right);
}while(left<right);
swap(array,left,right);
return left;
}
}int[], double[] ==> Integer[], Double[]....
Shine写的是inplace quick sort.较难看懂得一种sort.
看看吧,visual J写的。
http://home.nyc.rr.com/hayai/sorting.htm
有这么烂吗,还想请教一下不知烂在什么地方了