题解 | #排序#
排序
https://www.nowcoder.com/practice/508f66c6c93d4191ab25151066cb50ef
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define len 1009 #define maxint 1<<31-1 int list[len]; void swap(int ind1,int ind2){ int temp = list[ind1]; list[ind1] = list[ind2]; list[ind2] = temp; } void mpsort_list(int n){//冒泡排序 for(int i = 1;i<=n;i++){ for(int j = 1;j<=n-i;j++){ if(list[j]>list[j+1]){ swap(j,j+1); } } } } int piv(int l,int r){ int temp = list[l]; while(l<r){ while(l<r&&list[r]>temp){ r--; } list[l] = list[r]; while(l<r&&list[l]<temp){ l++; } list[r] = list[l]; } list[l] = temp; return l; } void quicksort_list(int l,int r){//快排 if(l<r){ int mid = piv(l,r); quicksort_list(l,mid-1); quicksort_list(mid+1,r); } } void build_leap(int ind,int n){ list[0] = list[ind]; for(int i = 2*ind;i<=n;i*=2){ if(i<n&&list[i+1]>list[i]){ i++; } if(list[0]>=list[i])break; else{ list[ind] = list[i]; ind = i; } } list[ind] = list[0]; } void buildmax_leap(int n){ for(int i = n/2;i>=1;i--){ build_leap(i,n); } } void leapsort_list(int n){//堆排序 buildmax_leap(n); for(int i = n;i>=1;i--){ swap(1,i); build_leap(1,i-1); } } int cmp(const void*s1,const void*s2){ int a1 = *(int*)s1; int a2 = *(int*)s2; return a1-a2; } int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i = 1;i<=n;i++){ scanf("%d",&list[i]); } //mpsort_list(n); //quicksort_list(1,n); //leapsort_list(n); qsort(list+1,n,sizeof(int),cmp); for(int i = 1;i<=n;i++){ printf("%d ",list[i]); } printf("\n"); } }