#include<stdio.h> #include<stdlib.h> #include<string.h> void swap(int *a,int *b) { int tmp=*a; *a=*b; *b=tmp; } void insertSort(int *a,int n) { int tmp,i,j; for(i=1;i<n;i++) { tmp=a[i]; for(j=i;j>0&&tmp<a[j-1];j--)//find the location to insert,the smaller a[j]=a[j-1]; a[j]=tmp; } } void shellSort(int *a,int n) { int i,j,incre,tmp; for(incre=n/2;incre>0;incre/=2) { for(i=incre;i<n;i++) { tmp=a[i]; for(j=i;j>=incre;j-=incre) { if(tmp<a[j-incre]) a[j]=a[j-incre]; else break; } a[j]=tmp; } } } int partition(int *a,int low,int high) { int key=a[low]; while(low<high) { while(low<high&&key<=a[high]) high--; swap(&a[low],&a[high]); while(low<high&&key>=a[low]) low++; swap(&a[low],&a[high]); a[low]=key; } return low; } void quickSort(int *a,int low,int high) { int inter; if(low<high) { inter=partition(a,low,high); quickSort(a,low,inter-1); quickSort(a,inter+1,high); } } int main() { int n; while(scanf("%d",&n)!=EOF) { int *nums=(int *)malloc(sizeof(int)*n); for(int i=0;i<n;i++) scanf("%d",&nums[i]); adjustHeap(nums,n); for(i=0;i<n;i++) printf("%d ",nums[i]); printf("\n"); } return 0; }
时间: 2024-10-05 05:01:05