【改良的选择排序 】

/*
改良的选择排序,需要堆积树 
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define MAX 10
#define SWAP(x, y) {int t; t = x; x = y; y = t;}

void createheap(int []);
void heapsort(int []);

int main(void){
    int number[MAX + 1] = {-1};
    int i, num;
    
    srand(time(NULL));
    printf("排序前: ");
    for(i = 1; i <= MAX; i++){
        number[i] = rand()%100;
        printf("%d ", number[i]);
    }
    
    printf("\n建立堆积数:");
    createheap(number);
    for(i = 1; i <= MAX; i++){
        printf("%d ", number[i]);
    }
    printf("\n");
    
    heapsort(number);
    
    printf("\n"); 
    return 0;
} 

void createheap(int number[]){
    int i, s, p;
    int heap[MAX + 1] = {-1};
    for(i = 1; i <= MAX; i++){
        heap[i] = number[i];
        s = i; 
        p = i / 2;
        while(s >= 2 && heap[p] > heap[s]){
            SWAP(heap[p], heap[s]);
            s = p;
            p = s / 2; 
        } 
    }
    
    for(i = 1; i <= MAX; i++){
        number[i] = heap[i];
    } 
} 

void heapsort(int number[]){
    int i, m, p, s;
    
    m =  MAX;
    while(m > 1){
        SWAP(number[1], number[m]);
        m--;
        
        p = 1;
        s = 2 * p;
        
        while(s <= m){
            if(s < m && number[s+1] < number[s]){
                s++;
            }
            if(number[p] <= number[s]){
                break;
            }
            SWAP(number[p], number[s]);
            p = s;
            s = 2 * p;
        }
        printf("\n排序中:");
        for(i = MAX; i > 0; i--){
            printf("%d ", number[i]);
        }
    } 
}

 

运行结果:

 

posted @ 2017-02-05 10:45  天秤libra  阅读(1199)  评论(0编辑  收藏  举报