当前位置:编程学习 > C/C++ >>

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

#include<stdio.h> 
#include<malloc.h> 
 
#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) ; 
    } 
     

w397090770的专栏

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,