自己动手编程 :双向气泡排序

 
#include
#include

#define Elem int 

typedef struct sqlist{
    Elem key ;
    int data ;
        
}sqlist ;

void doublesort(sqlist R[] , int n){
    int i , j ;
    int temp ;
    int exchange  = 1 ;
    i = 0 ;
    while(exchange==1){
        exchange = 0 ;
        for(j = n-i-1 ; j>i ;j--){
            if(R[j].key < R[j-1].key){
                exchange = 1 ;
                temp = R[j-1].key ;
                R[j-1].key = R[j].key ;
                R[j].key = temp ;
            }
        }

        for( j = i ; j< n-1 ; j++){
            if(R[j].key > R[j+1].key){
                exchange = 1 ;
                temp = R[j+1].key ;
                R[j+1].key = R[j].key ;
                R[j].key = temp ;
            }
        }
        i++ ;

    }
}


void main(){
    sqlist dataSort[10] ;
    int i = 0 ;
    for(i = 0 ; i < 10 ; i++){
        dataSort[i].key = 10 - i ;
        dataSort[i].data = i ;
    }
    doublesort(dataSort , 10) ;
    for(i = 0 ; i < 10 ; i++){
        printf("key:%d  data:%d \n",dataSort[i].key,dataSort[i].data) ;
    }
    
}


你可能感兴趣的:(C,Program,编程,exchange,struct)