zoukankan      html  css  js  c++  java
  • 归并排序-总结

    关键代码:

    for()

    C语言
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    #include <stdlib.h>
    #include <stdio.h>
     
    void Merge(int sourceArr[],int tempArr[], int startIndex, int midIndex, int endIndex)
    {
        int i = startIndex, j=midIndex+1, k = startIndex;
        while(i!=midIndex+1 && j!=endIndex+1)
        {
            if(sourceArr[i] > sourceArr[j])
                tempArr[k++] = sourceArr[j++];
            else
                tempArr[k++] = sourceArr[i++];
        }
        while(i != midIndex+1)
            tempArr[k++] = sourceArr[i++];
        while(j != endIndex+1)
            tempArr[k++] = sourceArr[j++];
        for(i=startIndex; i<=endIndex; i++)
            sourceArr[i] = tempArr[i];
    }
     
    //内部使用递归
    void MergeSort(int sourceArr[], int tempArr[], int startIndex, int endIndex)
    {
        int midIndex;
        if(startIndex < endIndex)
        {
            midIndex = startIndex + (endIndex-startIndex) / 2;//避免溢出int
            MergeSort(sourceArr, tempArr, startIndex, midIndex);
            MergeSort(sourceArr, tempArr, midIndex+1, endIndex);
            Merge(sourceArr, tempArr, startIndex, midIndex, endIndex);
        }
    }
     
    int main(int argc, char * argv[])
    {
        int a[8] = {50, 10, 20, 30, 70, 40, 80, 60};
        int i, b[8];
        MergeSort(a, b, 0, 7);
        for(i=0; i<8; i++)
            printf("%d ", a[i]);
        printf(" ");
        return 0;
    }
    PHP
  • 相关阅读:
    ContactManager示例解析
    CubeLiveWallpaper例子解析
    BluetoothChat例子解析
    推荐一个模板引擎 templateengine
    jQuery plugin: Autocomplete
    乐从网站建设、域名、主机-www.lecong.me-www.lecong.mobi
    C#操作注册表
    .NET模板引擎
    [转]模版引擎AderTemplate源代码分析笔记
    windows服务器文件同步,网站同步镜像
  • 原文地址:https://www.cnblogs.com/lj520fj/p/11449391.html
Copyright © 2011-2022 走看看