C語言 實現歸併排序演算法_C 語言

來源:互聯網
上載者:User

C語言 實現歸併排序演算法

歸併排序(Merge sort)是建立在歸併操作上的一種有效排序演算法。該演算法是採用分治法(Divide and Conquer)的一個非常典型的應用。

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

演算法描述

歸併操作的過程如下:

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

特點:歸併排序是穩定的排序.即相等的元素的順序不會改變,  速度僅次於快速排序,但較穩定。

歸併操作

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

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

初始狀態:6, 202, 100, 301, 38, 8, 1

第一次歸併後:[6, 202], [100, 301], [8, 38], [1],比較次數:3;

第二次歸併後:[6, 100, 202, 301],[1, 8, 38],比較次數:4;

第三次歸併後:[1, 6, 8, 38, 100, 202, 301],比較次數:4;

總的比較次數為:3+4+4=11,;

逆序數為14;

演算法實現

// Completed on 2014.10.11 17:20// Language: C99//// 著作權(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;  //左半地區邊界   int right_min,right_max; //右半地區邊界   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;}

使用遞迴實現:

// Completed on 2014.10.11 18:20// Language: C99//// 著作權(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;}

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.