最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言 實現(xiàn)歸并排序算法

 更新時間:2016年11月10日 15:31:26   作者:wuyudong  
這篇文章主要介紹了C語言 實現(xiàn)歸并排序算法的相關(guān)資料,需要的朋友可以參考下

C語言 實現(xiàn)歸并排序算法

歸并排序(Merge sort)是創(chuàng)建在歸并操作上的一種有效的排序算法。該算法是采用分治法(Divide and Conquer)的一個非常典型的應用。

一個歸并排序的例子:對一個隨機點的鏈表進行排序

算法描述

歸并操作的過程如下:

  1. 申請空間,使其大小為兩個已經(jīng)排序序列之和,該空間用來存放合并后的序列
  2. 設定兩個指針,最初位置分別為兩個已經(jīng)排序序列的起始位置
  3. 比較兩個指針所指向的元素,選擇相對小的元素放入到合并空間,并移動指針到下一位置
  4. 重復步驟3直到某一指針到達序列尾
  5. 將另一序列剩下的所有元素直接復制到合并序列尾

特點:歸并排序是穩(wěn)定的排序.即相等的元素的順序不會改變,  速度僅次于快速排序,但較穩(wěn)定。

歸并操作

歸并操作(merge),也叫歸并算法,指的是將兩個順序序列合并成一個順序序列的方法。

如:設有數(shù)列 [6,202,100,301,38,8,1]

初始狀態(tài):6, 202, 100, 301, 38, 8, 1

第一次歸并后:[6, 202], [100, 301], [8, 38], [1],比較次數(shù):3;

第二次歸并后:[6, 100, 202, 301],[1, 8, 38],比較次數(shù):4;

第三次歸并后:[1, 6, 8, 38, 100, 202, 301],比較次數(shù):4;

總的比較次數(shù)為:3+4+4=11,;

逆序數(shù)為14;

算法實現(xiàn)

// Completed on 2014.10.11 17:20
// Language: C99
//
// 版權(quán)所有(C)codingwu  (mail: oskernel@126.com) 
// 博客地址:http://www.cnblogs.com/archimedes/
#include<stdio.h>
#include<stdlib.h>void merge_sort(int *list, const int first, const int last)
{
  int len= last-first+1; 
  int left_min,left_max;  //左半?yún)^(qū)域邊界 
  int right_min,right_max; //右半?yún)^(qū)域邊界 
  int index;
  int i;
  int *tmp;
  tmp = (int *)malloc(sizeof(int)*len);
  if( tmp == NULL || len <= 0 )
    return;
  
  for( i = 1; i < len; i *= 2 )
  {
    for( left_min = 0; left_min < len - i; left_min = right_max)
    {
      int j;
      right_min = left_max = left_min + i;
      right_max = left_max + i;
      j = left_min;
      if ( right_max > len )
        right_max = len;
      index = 0;
      while( left_min < left_max && right_min < right_max )
      {
        tmp[index++] = (list[left_min] > list[right_min] ? list[right_min++] : list[left_min++]);
      }
      while( left_min < left_max )
      {
        list[--right_min] = list[--left_max];
      }
      while( index > 0 )
      {
        list[--right_min] = tmp[--index];
      }
    }
  }
  free(tmp);
}
int main()
{
  int a[] = {288, 52, 123, 30, 212, 23, 10, 233};
  int n, mid;
  n = sizeof(a) / sizeof(a[0]);
  mid = n / 2;
  merge_sort(a, 0, n - 1);
  for(int k = 0; k < n; k++)
    printf("%d ", a[k]);
  printf("\n");
  return 0;
}

使用遞歸實現(xiàn):

// Completed on 2014.10.11 18:20
// Language: C99
//
// 版權(quán)所有(C)codingwu  (mail: oskernel@126.com) 
// 博客地址:http://www.cnblogs.com/archimedes/
#include<stdio.h>
#include<stdlib.h>
void merge(int *array,const int first, const int mid, const int last)
{
  int i,index;
  int first1,last1;
  int first2,last2;
  int *tmp;
  tmp = (int *)malloc((last-first+1)*sizeof(int));
  if( tmp == NULL )
    return;
  first1 = first;
  last1 = mid;
  first2 = mid+1;
  last2 = last;
  index = 0;
  while( (first1 <= last1) && (first2 <= last2) )
  {
    if( array[first1] < array[first2] )
    {
      tmp[index++] = array[first1];
      first1++;
    }
    else{
      tmp[index++] = array[first2];
      first2++;
    }
  }
  while( first1 <= last1 )
  {
    tmp[index++] = array[first1++];
  }
  while( first2 <= last2 )
  {
    tmp[index++] = array[first2++];
  }
  for( i=0; i<(last-first+1); i++)
  {
    array[first+i] = tmp[i];
  }
  free(tmp);
}
void merge_sort(int *array, const int first, const 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);
  }
}
int main()
{
  int a[] = {288, 52, 123, 30, 212, 23, 10, 233};
  int n, mid;
  n = sizeof(a) / sizeof(a[0]);
  mid = n / 2;
  merge_sort(a, 0, n - 1);
  for(int k = 0; k < n; k++)
    printf("%d ", a[k]);
  printf("\n");
  return 0;
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論

齐齐哈尔市| 巴青县| 金阳县| 龙海市| 卢氏县| 平远县| 渑池县| 岳阳市| 翁源县| 庐江县| 固阳县| 广德县| 辉南县| 新民市| 乌审旗| 黄冈市| 水城县| 龙海市| 喜德县| 拉孜县| 濮阳市| 达拉特旗| 唐海县| 开鲁县| 拜泉县| 共和县| 临颍县| 遵义县| 西昌市| 万宁市| 手游| 仁化县| 河曲县| 兴山县| 乌什县| 黎城县| 筠连县| 德令哈市| 宁波市| 桂平市| 中宁县|