C语言数据结构合并排序

#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]);
}
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐