以下是引用han2y在2006-6-25 10:25:52的发言:
main()
{int i,j,temp;
int a[8]={7,10,8,12,14,5,3,20};
printf("sort:");
for(i=0;i<=6;i++)
for(j=i+1;j<=7;j++)
{if(a[i]>a[j])
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<8;i++)
printf("%4d",a[i]);
}
这是冒泡排序.
#include "Stdio.h"
#include "Conio.h"
void QSort(int key[],int low,int high)
{ /*快速排序*/
int i=low,j=high,tag;
if (low<high)
{
tag=key[low];
do
{
while (j>i && key[j]>=tag)
j--;
if (i<j)
{
key[i]=key[j];
i++;
}
while (i<j && key[i]<=tag)
i++;
if (i<j)
{
key[j]=key[i];
j--;
}
}while (i<j);
key[i]=tag;
QSort(key,low,j-1);
QSort(key,j+1,high);
}
}
int main(void)
{
int key[8]={7,10,8,12,14,5,3,20};
int i=0;
QSort(key,0,7);
printf("Quick sort: ");
for (i=0;i<8;i++)
printf("%d ",key[i]);
getch();
return 0;
}
这是快速排序.