Algorithm (c): merge sort

Source: Internet
Author: User

Merge sort is the same as division method, but different from quick sort

Fast sorting is more biased, and the data exchange between the two sides in the recursive process

The merge sort is more in favor of merging, in order in the recursion of two halves merging


Code:

[email protected]:/mnt/shared/appbox/mgsort# cat mgsort.c#include <stdlib.h> #include <stdlib.h># Include <string.h> #include <unistd.h> #include <stdio.h>static int compare_int (const void *int1,        const void *int2) {if (* (const int *) Int1 = = * (const int *) INT2) return 0; return * (const int *) INT1 > * (const int *) Int2? 1:-1;} static int merge (void *data, int esize, int i, int j, int k, int (*compare) (const void *key1, const void *key2)) {C        Har *a = data;        Char *m;        int IPOs, JPOs, MPOs;        IPOs = i;        JPOs = j + 1;        MPOs = 0;        if ((M = (char *) malloc (Esize * ((k-i) +1)) = = NULL) return-1;                while (IPOs <= J | | JPOs <= k) {if (IPOs > J)//left no further elements to merge {while (JPOs <= k)//merge right {memcpy (&m[mpos * Esize], &a[jpos *Esize], esize);                                jpos++;                        mpos++;                } continue; } else if (JPOs > K)//right No more elements to merge {while (IPOs <= j)//merge left {memcpy (&m[mpos * esize], &a[ipos * E                                Size], esize);                                ipos++;                        mpos++;                } continue; //both left and right need merge if (compare (&a[ipos * Esize], &a[jpos * esize]) &L T                                0) {memcpy (&m[mpos * esize], &a[ipos * esize], esize);                                ipos++;                mpos++; } else {memcpy (&m[mpos * esizE], &a[jpos * esize], esize);                                jpos++;                mpos++;        }} memcpy (&a[i * esize], M, Esize * ((k-i) +1));        Free (m); return 0;}  int Mgsort (void *data, int size, int esize, int i, int k, int (*compare) (const void *key1, const void *key2)) {int        J                if (I < k) {j = (int) (((i + k-1))/2);                if (mgsort (data, size, esize, I, J, compare) < 0) return-1;                if (mgsort (data, size, esize, j+1, K, Compare) < 0) return-1;        if (merge (data, Esize, I, J, K, Compare) < 0) return-1; } return 0;}        int main (int argc, char *argv[]) {int ret;        int I, j, K;        i = 11;        j = 12;        int array[10]= {1,4,7,2,5,8,3,6,9,10};        printf ("\n******************compare_init test**********************\n"); printf ("Compare_iNT (&i, &j) =%d\n ", Compare_int (&i, &j));        printf ("Compare_int (&j, &i) =%d\n", Compare_int (&j, &i));        printf ("Compare_int (&i, &i) =%d\n", Compare_int (&i, &i));        printf ("\n******************partition test**********************\n");        Mgsort (array, ten, sizeof (int), 0, 9, compare_int);        for (i=0; i<10; i++) printf ("%d", array[i]);        printf ("\ n"); return 0;}

Operation Result:

[Email protected]:/mnt/shared/appbox/mgsort#./mgsort ******************compare_init test********************** Compare_int (&i, &j) =-1compare_int (&j, &i) =1compare_int (&i, &i) =0****************** Partition    






Algorithm (c): merge sort

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.