#include stdio.h#define max 10int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };int b[10];
编程学习网为您整理以下代码实例,主要实现:C语言数据结构合并排序,希望可以帮到各位朋友。
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
voID merging(int low, int mID, int high) {
int l1, l2, i;
for(l1 = low, l2 = mID + 1, i = low; l1 <= mID && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mID)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
voID sort(int low, int high) {
int mID;
if(low < high) {
mID = (low + high) / 2;
sort(low, mID);
sort(mID+1, high);
merging(low, mID, high);
} else {
return;
}
}
int main() {
int i;
printf("List before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
沃梦达教程
本文标题为:C语言数据结构合并排序
基础教程推荐
猜你喜欢
- 初始化变量和赋值运算符 1970-01-01
- 使用scanf()读取字符串 1970-01-01
- C语言访问数组元素 1970-01-01
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01
- C++定义类对象 1970-01-01
- C++按值调用 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- C++输入/输出运算符重载 1970-01-01
- C++ #define 1970-01-01
- end() 能否成为 stl 容器的昂贵操作 2022-10-23