// sort.c by 乐观次品
// 以下常用的排序算法都在这里了,希望能帮到你。
#include
#define N 15
#define swap(A,B) {int temp; temp = A; A = B; B = temp;}
#define min(A,B) ((A/*int partition(int *a,int l,int r)
{
int v = a[l];
int left = l, right = r+1;
while(1)
{
while(a[++left]
if(left
}
swap(a[l],a[right]);
return right;
}
void quicksort(int *a,int l,int r)
{
int mid;
if(l >= r) return;
mid = partition(a,l,r);
quicksort(a,l,mid-1);
quicksort(a,mid+1,r);
}
void merge(int *a,int l,int m,int r)
{
int b[N];
int i =l, j=m+1, k =0;
while(i<=m && j<=r)
{
if(a[i] else b[k++] = a[j++];
}
while(i<=m) b[k++] = a[i++];
while(j<=r) b[k++] = a[j++];
for(k=0,i=l;i<=r;i++,k++) a[i] = b[k];
}
void mergesort(int *a,int l,int r)
{
int step=1;
int i;
for(step=1;step
merge(a,i,min(i+step-1,r),min(i+2*step-1,r));
}
void mergesort(int *a,int l,int r)
{
int mid = (l+r)/2;
if(l>=r) return;
mergesort(a,l,mid);
mergesort(a,mid+1,r);
merge(a,l,mid,r);
}
void shellsort(int *a, int len)
{
int step;
int i,j;
int temp;
for(step = len/2; step>=1; step /=2)
{
for(i=step;i
j = i;
temp = a[i];
while(j-step>=0 && a[j-step] > temp)
{
a[j] = a[j-step];
j -= step;
}
a[j] = temp;
}
}
}
void insertsort(int *a, int len)
{
int i,j;
int temp;
for(i=1;i
j=i;
temp = a[i];
while(j>0 && a[j-1]>temp)
{
a[j] = a[j-1];
j--;
}
a[j]=temp;
}
}
void selectsort(int *a, int l,int r)
{
int i,j,min;
for(i=l;i
min=i;
for(j=i+1;j<=r;j++)
{
if(a[j] }
swap(a[i],a[min]);
}
} */
void heapadjust(int *a,int k,int n)
{
int j;
while(2*k <= n)
{
j = 2*k;
if(a[j] if(a[k]>=a[j]) break;
swap(a[k],a[j]);
k = j;
}
}
void heapsort(int *a,int l,int r)
{
int k,n=r-l+1;
for(k=n/2;k>=l;k--)
heapadjust(a,k,n);
while(r>l)
{
swap(a[l],a[r]);
r--;
heapadjust(a,l,r);
}
}
void main()
{
int a[N] = {65,70,80,65,50,90,44,65,65,33,22};
int i;
printf("\n before sort :\n");
for(i=0;i
//mergesort(a,0,N-1);
//shellsort(a,N);
//insertsort(a,N);
//selectsort(a,0,N-1);
heapsort(a,0,N-1);
printf("\n after sort :\n");
for(i=0;i
1.交换
2.冒泡
2.插入排序
#include
void input(int x[],int n)
{
int i;
for(i=0;i
printf("\n");
}
void output(int x[],int n)
{
int i;
for(i=0;i
}
void sort(int x[],int n)
{
int temp;
int i,j;
for(j=0;j<10;j++)
for(i=0;i<10;i++)
if(x[i]>x[i+1])
{temp=x[i];x[i]=x[i+1];x[i+1]=temp;}
}
void main()
{
int a[10];
printf("please input ten numbers:");
input(a,10);
sort(a,10);
output(a,10);
}