C Language Implementation Merge sort algorithm _c language

Source: Internet
Author: User

Implementation of merging sort algorithm in C language

Merge sort is an efficient sort algorithm for creating merge operations. The algorithm is a very typical application of the partition method (Divide and Conquer).

An example of a merge sort: Sorting a list of random points

Algorithm description

The process of merging operations is as follows:

    1. Application space so that the sum of the two sorted sequences is used to store the merged sequence
    2. Set two pointers, starting at the beginning of two sorted sequences, respectively
    3. Compares the elements pointed to by two pointers, selects the relatively small elements into the merged space, and moves the pointer to the next position
    4. Repeat step 3 until a pointer reaches the end of the sequence
    5. Copy all remaining elements of another sequence directly to the end of the merge sequence

Features: Merge sort is a stable sort. That is, the order of equal elements will not change, the speed is second only to the fast sort, but more stable.

Merge operation

Merge operations (merge), also known as merging algorithms, refer to the method of merging two sequential sequences into a sequential sequence.

such as: a series [6,202,100,301,38,8,1]

Initial Status: 6, 202, 100, 301, 38, 8, 1

After the first merge: [6, 202], [100, 301], [8, 38], [1], compare times: 3;

After the second merge: [6, 100, 202, 301],[1, 8, 38], compared the number of times: 4;

After the third merge: [1, 6, 8, 38, 100, 202, 301], compared the number of times: 4;

The total number of comparisons are: 3+4+4=11,;

The reverse number is 14;

Algorithm implementation

Completed on 2014.10.11 17:20//LANGUAGE:C99////Copyright (C) Codingwu (mail:oskernel@126.com)//Blog address: HTTP://WWW.CNB logs.com/archimedes/#include <stdio.h> #include <stdlib.h>void merge_sort (int *list, const INT-A, const 
  int last) {int len= last-first+1;  int Left_min,left_max; Left half region boundary int Right_min,right_max;
  Right half region boundary 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) > Li St[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;
 }

Using recursive implementations:

Completed on 2014.10.11 18:20//LANGUAGE:C99////Copyright (C) Codingwu (mail:oskernel@126.com)//Blog address: HTTP://WWW.CNB logs.com/archimedes/#include <stdio.h> #include <stdlib.h> void merge (int *array,const INT-A, 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 = A;
  Last1 = mid;
  First2 = mid+1;
  Last2 = Last;
  index = 0; while ((First1 <= last1) && (first2 <= last2)) {if (Array[first1] < Array[first2]) {T
      mp[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-A, const int last) {int mid = 0;
    if (I < last) {mid = (i + last)/2;
    Merge_sort (Array, I, mid);
    Merge_sort (Array, mid + 1, last);
  Merge (array, I, 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;

 }

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.