经典算法之-整数奇偶排序C

发布时间 2024-01-07 11:45:56作者: 神奇的萝卜丝

#include <stdio.h>
int divide(int A[],int head,int tail){
    if(head==tail) return head;
    int t=A[head];
    while(head<tail){
        while(head<tail && 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 = divide(A,head,tail);
    if(t>head) quicksort(A,head,t-1);
    if(t<tail) quicksort(A,t+1,tail);
}

int main(){
    int A[10]={-1};
    int B[10]={-1};
    int n = 0 , t = 0 ,i = 0 ,j = 0 ;
    while( scanf("%d",&t) != EOF ){
        if(i==10) i=0;
        if(j==10) j=0;
        if(t%2==0){
            A[i++]=t;
        }else {
            B[j++] = t;
        }
        n++;
        if(n==10){
            n=0;
            quicksort(A,0,i-1);
            quicksort(B,0,j-1);
            for(int x = j-1 ; x >=0  ; x--){
                printf("%d ",B[x]);
            }
            for(int x = 0 ; x < i ; x++){
                printf("%d ",A[x]);
            }
            printf("\n");
        }
    }
    return 0;
}

结果如下: