歸併排序演算法

2021-06-20 08:08:21 字數 1633 閱讀 2712

// mergesort.c

#include #include #include typedef int datatype;

typedef unsigned char numtype; // 有效個數:2至255

// ( ( 2 ^ ( sizeof ( numtype ) * 8 ) ) - 1 )

#define limit ( numtype ) ( ( 1 << ( sizeof ( numtype ) << 3 ) ) - 1 )

typedef short type; // type 至少比排序個數大乙個級別的資料型別單位

void merge(datatype * const, datatype *, const numtype, const type, const type);

void mergepass(datatype * const, datatype *, const numtype, const type);

void mergesort(datatype * const, const numtype);

int main(void)

; srand((unsigned int)time(0));

puts("生成隨機數:\n");

for (i = 0; i < limit; ++i)

sum = sizeof (data) / sizeof (data[0]);

if ((sizeof (sum) <= sizeof (numtype)) && (sum <= limit) && (sum > 1))

mergesort(data, sum);

puts("\n\n歸併排序後:\n");

for (i = 0; i < limit; ++i)

putchar('\n');

getch();

return 0;

}/******************************以上**僅供測試******************************/

// 歸併排序

void merge(datatype * const data, datatype * result, const numtype low, const type cutoffpoint, const type high)

}void mergepass(datatype * const data, datatype * result, const numtype sum, const type len)

if (i + len < sum) // 乙個長len,另乙個不足len

merge(data, result, i, i + len - 1, sum - 1);

else // 只剩乙個長度不超過len

for (j = i; j < sum; ++j)

result[j] = data[j];

}void mergesort(datatype * const data, const numtype sum)

// 此處可作除錯使用

// printf("\n\n歸併後,有序段(len)長:%4hu\n\n", len);

free(temp);

}



歸併演算法 歸併排序

歸併演算法 歸併排序 這周需要用到歸併演算法,於是找了找相關的資料,整理如下 歸併排序 merge sort 是利用 歸併 技術來進行排序。歸併是指將若干個已排序的子檔案合併成乙個有序的檔案。兩路歸併演算法 1 演算法基本思路 設兩個有序的子檔案 相當於輸入堆 放在同一向量中相鄰的位置上 r low...

歸併排序演算法

include stdafx.h include include include 合併兩段已經排好序的列表 void merge int list int mergelist int left int mid int right else if i mid else 將列表list按照seglen分...

歸併排序演算法

這個演算法感覺比插入難理解一些,下面說說我的理解 歸併排序的步驟 1.把長度為n的序列分為兩個長度為n 2的子串行 2.對這兩個子串行分別採用歸併排序 3.將兩個子串行合併成乙個最終的排序序列 通過步驟2可看到 在歸併排序中又用了歸併排序,可見這是乙個遞迴的過程。例如乙個陣列 a 8 下面採用遞迴排...