写个快排就完事了。实在不行,写个选择排序也很简单。
#include<stdio.h>
int devide(int A[],int head,int tail){
if(head==tail) return head;
int t = A[head] ;
while(head < tail){
while(tail > head && A[tail] > t) tail--;
if(head!=tail) A[head++]=A[tail];
while(head < tail && A[head] < t) head++;
if(head!=tail) A[tail--]=A[head];
}
A[head]=t;
return head;
}
void quicksort(int A[],int head,int tail){
if(head >= tail) return;
int t=devide(A,head,tail);
if(t>head) quicksort(A,head,t-1);
if(t<tail) quicksort(A,t+1,tail);
}
int main(){
int A[101]={0};
int n = 0 ;
while(scanf("%d",&n) != EOF ){
for(int i = 0 ; i < n ; i++ ){
scanf("%d",&A[i]);
}
quicksort(A,0,n-1);
for(int i = 0 ; i < n ; i++ ){
printf("%d ",A[i]);
}
printf("\n");
}
return 0;
}
标签:head,return,int,tail,while,算法,经典,排序
From: https://www.cnblogs.com/llllmz/p/17950187