• 【合并排序法】


    /*
    合并排序法 
    */ 
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX1 10
    #define MAX2 10
    
    #define SWAP(x,y) {int t; t = x; x = y; y = t;} 
    
    int partition(int[], int, int);
    void quicksort(int[], int, int);
    void mergesort(int[], int, int[], int, int[]);
    
    int main(void) {
        int number1[MAX1] = {0};
        int number2[MAX1] = {0};
        int number3[MAX1+MAX2] = {0};
        int i, num;
        
        srand(time(NULL));
        printf("排序前:");
        printf("
    number1[]:");
        
        for(i = 0; i < MAX1; i++) {
            number1[i] = rand() % 100;
            printf("%d ", number1[i]);
        }
        
        printf("
    number2[]:");
        for(i = 0; i < MAX2; i++) {
            number2[i] = rand() % 100;
            printf("%d ", number2[i]);
        }
        // 先排序两笔资料
        quicksort(number1, 0, MAX1-1);
        quicksort(number2, 0, MAX2-1);
        
        printf("
    排序后:");
        printf("
    number1[]:");
        for(i = 0; i < MAX1; i++)
            printf("%d ", number1[i]);
            
        printf("
    number2[]:");
        for(i = 0; i < MAX2; i++)
            printf("%d ", number2[i]);
        // 合并排序
        mergesort(number1, MAX1, number2, MAX2, number3);
        printf("
    合并后:");
        for(i = 0; i < MAX1+MAX2; i++)
            printf("%d ", number3[i]); 
        printf("
    ");
        return 0;
    }
    int partition(int number[], int left, int right) {
        int i, j, s;
        s = number[right];
        i = left - 1;
        for(j = left; j < right; j++) {
            if(number[j] <= s) {
                i++;
                SWAP(number[i], number[j]);
            }
        }
        SWAP(number[i+1], number[right]);
        return i+1;
    }
    void quicksort(int number[], int left, int right) {
        int q;
        if(left < right) {
            q = partition(number, left, right);
            quicksort(number, left, q-1);
            quicksort(number, q+1, right);
        }
    }
    
    void mergesort(int number1[], int M, int number2[], int N, int number3[]) {
        int i = 0, j = 0, k = 0;
        while(i < M && j < N) {
            if(number1[i] <= number2[j])
                number3[k++] = number1[i++];
            else
                number3[k++] = number2[j++];
        }
        while(i < M)
            number3[k++] = number1[i++]; 
        while(j < N)
            number3[k++] = number2[j++];
    }

    运行结果:

  • 相关阅读:
    04面向对象编程-01-创建对象 和 原型理解(prototype、__proto__)
    03标准对象-02-RegExp 正则表达式
    03标准对象-01-Date和JSON
    广度优先搜索
    Java虚拟机(三) —— 类加载
    业务开发(一)—— MySQL
    Java并发编程(一) —— Java内存模型JMM
    深度优先搜索
    Spark学习(一)
    清泉白石
  • 原文地址:https://www.cnblogs.com/libra-yong/p/6390270.html
Copyright © 2020-2023  润新知