Web - Amazon

We provide Linux to the World


We support WINRAR [What is this] - [Download .exe file(s) for Windows]

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
SITEMAP
Audiobooks by Valerio Di Stefano: Single Download - Complete Download [TAR] [WIM] [ZIP] [RAR] - Alphabetical Download  [TAR] [WIM] [ZIP] [RAR] - Download Instructions

Make a donation: IBAN: IT36M0708677020000000008016 - BIC/SWIFT:  ICRAITRRU60 - VALERIO DI STEFANO or
Privacy Policy Cookie Policy Terms and Conditions
归并排序 - Wikipedia

归并排序

维基百科,自由的百科全书

归并排序(Merge Sort,台灣譯作:合併排序)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。

目录

[编辑] 归并操作

归并操作(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作。

[编辑] 算法描述

归并操作的工作原理如下:

  1. 申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
  2. 设定两个指针,最初位置为别为两个已经排序序列的起始位置
  3. 比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
  4. 重复步骤3直到某一指针达到序列尾
  5. 将另一序列剩下的所有元素直接复制到合并序列尾

[编辑] 示例代码

以下示例代码实现了归并操作。array[]是元素序列,其中从索引p开始到q位置,按照升序排列,同时,从(q+1)到r也已经按照升序排列,merge()函数将把这两个已经排序好的子序列合并成一个排序序列。结果放到array中。

/**
  * 0 <= p <= q < r, subarray array[p..q] and array[q+1..r] are already sorted.
  * the merge() function merges the two sub-arrays into one sorted array.
  */

static void merge(int array[], int p, int q, int r)
{
        int i,k;
        int begin1,end1,begin2,end2;
        int* temp = (int*)malloc((r-p+1)*sizeof(int));
        begin1= p;     end1 = q;
        begin2 = q+1;  end2 = r;
       
        k = 0;
        while((begin1 <= end1)&&( begin2 <= end2))
        {
                if(array[begin1]<array[begin2])
                {
                        temp[k] = array[begin1];  begin1++; 
                }
                else
                {
                        temp[k] = array[begin2];  begin2++;
                }
                k++;            
        }
        
        while(begin1<=end1) 
        {
                temp[k++] = array[begin1++];
        }
        while(begin2<=end2)
        {
                temp[k++] = array[begin2++];
        }
        for (i = 0; i < (r - p +1); i++) 
                array[p+i] = temp[i];
        free(temp); 
}

[编辑] 归并排序

归并排序具体工作原理如下(假设序列共有n个元素):

  1. 将序列每相邻两个数字进行归并操作(merge),形成floor(n / 2)个序列,排序后每个序列包含两个元素
  2. 将上述序列再次归并,形成floor(n / 4)个序列,每个序列包含四个元素
  3. 重复步骤2,直到所有元素排序完毕

[编辑] 示例代码

示例代码为C语言,输入参数中,需要排序的数组为array[],起始索引为first,终止索引为last。调用完成后,array[]中从first到last处于升序排列。

void merge_sort(int array[], unsigned int first, unsigned int last)
{
        int mid = 0;
        if(first<last)
        {
                mid = (first+last)/2;
                merge_sort(array, first, mid);
                merge_sort(array, mid+1,last);
                merge(array,first,mid,last);
        }
}


[编辑] 算法复杂度

比较操作的次数介于(nlogn) / 2nlognn + 1赋值操作的次数是(2nlogn)。 归并算法的空间复杂度为:Θ (n)

[编辑] 外部連結

Our "Network":

Project Gutenberg
https://gutenberg.classicistranieri.com

Encyclopaedia Britannica 1911
https://encyclopaediabritannica.classicistranieri.com

Librivox Audiobooks
https://librivox.classicistranieri.com

Linux Distributions
https://old.classicistranieri.com

Magnatune (MP3 Music)
https://magnatune.classicistranieri.com

Static Wikipedia (June 2008)
https://wikipedia.classicistranieri.com

Static Wikipedia (March 2008)
https://wikipedia2007.classicistranieri.com/mar2008/

Static Wikipedia (2007)
https://wikipedia2007.classicistranieri.com

Static Wikipedia (2006)
https://wikipedia2006.classicistranieri.com

Liber Liber
https://liberliber.classicistranieri.com

ZIM Files for Kiwix
https://zim.classicistranieri.com


Other Websites:

Bach - Goldberg Variations
https://www.goldbergvariations.org

Lazarillo de Tormes
https://www.lazarillodetormes.org

Madame Bovary
https://www.madamebovary.org

Il Fu Mattia Pascal
https://www.mattiapascal.it

The Voice in the Desert
https://www.thevoiceinthedesert.org

Confessione d'un amore fascista
https://www.amorefascista.it

Malinverno
https://www.malinverno.org

Debito formativo
https://www.debitoformativo.it

Adina Spire
https://www.adinaspire.com