Algorithm final solution (3) -- Merge Sorting and algorithm final Merge Sorting

Source: Internet
Author: User

Algorithm final solution (3) -- Merge Sorting and algorithm final Merge Sorting

Merge Sorting 

O (NlogN), so the worst case of Merge Sorting can reach the average level of fast sorting
Requires additional storage space O (n)


1. Constantly split the data until the remaining one
2. When merging data, it is actually two ordered arrays. Therefore
This process is to merge and sort two sorted arrays.

/<Span id = "_ xhe_cursor"> </span>/merge sort // O (NlogN ), therefore, the worst case of Merge Sorting can reach the average level of fast sorting // requires additional storage space O (n) // 1. The data is continuously divided, until the data is merged one by one // 2. When merging, it is actually two ordered arrays. Therefore, the process of // merging and sorting two ordered arrays # include "sort. h "// merge function implements the merging process // I is the minimum index of the array, j is the median value, and k is the maximum index value int merge (void * data, int size, int esize, int I, int k, int j, int (* compare) (const void * key1, const void * key2) {int ipos, jpos, mpos; // I, j, m cursor int * a = (int *) data; int * m; // apply for extra space of m if (m = (int *) malloc (sizeof (int) * size) = NULL) {return-1;} memcpy (m, 0, sizeof (int) * size); ipos = I; jpos = j; // j is the median mpos = 0; while (ipos <= j | jpos <= k) {while (ipos <= j & jpos> = k) // when the remaining I index is not in the middle {memcpy (& m [mpos], & a [ipos], sizeof (int); ipos ++; mpos ++;} while (ipos> = j & jpos <= k) // The j index is not in the middle {memcpy (& m [mpos], & a [jpos], sizeof (int); jpos ++; mpos ++;} if (compare (& a [ipos], & a [jpos]) <= 0) // a small number is first inserted into the m temporary sequence {memcpy (& m [mpos], & a [ipos], sizeof (int); ipos ++; mpos ++;} else {memcpy (& m [mpos], & a [jpos], sizeof (int); jpos ++; mpos ++ ;}} data = m; // return data. At this time, the sorting is completed. free (m); // Finally, the mreturn 0;} // sorting function 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 = (k + I-1)/2; // Let j take the intermediate value if (mgsort (data, size, esize, I, j, compare) <0) // continuously split the left and combine {return-1;} if (mgsort (data, size, esize, j + 1, k, compare) after recursion )) // split the right {return-1;} if (merge (data, size, esize, I, k, j, compare) <0) {return-1 ;}} return 0 ;}



Merge sort code

MERGE (rectypr R [], rectype R1 [], int low, int mid, int
High)
{Int I, j, k;
I = low; j = mid + 1; k = low;
While (I <= mid) & (j <= high ))
If (R [I]. key <= R [j]. key) R1 [k ++] = R [I ++];
Else R1 [k ++] = R [j ++];
While (j <= mid) R1 [k ++] = R [I ++];
While (j <= high) R1 [k ++] = R [j ++];
}

Analysis:
Merge Sorting uses the preceding merge operation to achieve sorting. The basic idea is to regard the R [0] To R [n-1] columns to be sorted as n ordered subsequences with a length of 1, and merge these subsequences into two, then a high (n/2) subsequence is obtained. Then we merge the high (n/2) ordered sub-sequences into two, so that we can get an ordered sequence with a length of n at the end. Each merge operation above is to merge two subsequences into one subsequence. This is "two-way merge". Similarly, there can be "three-way merge" or "Multiple-way merge ".

Merge Algorithms
MERGEPASS (rectype R [], rectype R1 [], int length)
{Int I, j;
I = 0;
While (I + 2 * length-1 <n)
{MERGE (R, R1, I, I + length-1, I + 2 * length-1 );
I = I + 2 * length;
}
If (I + length-1 <n-1)
MERGE (R, R1, I, I + length-1, n-1 );
Else
For (j = I; j <n; j ++) R1 [j] = R [j];
}

Algorithm complexity analysis:
After Merge Sorting, the length of the ordered sub-file is 2i. Therefore, for a sequence with n records, high (log2n) must be merged, the time spent for each merge is O (n ). Therefore, the time complexity of the two-way Merge Sorting Algorithm is O (nlog2n), and the space required for the auxiliary array is O (n ).

The basic idea of two-way merger:
There are two ordered tables A and B. The numbers of objects are al and bl, And the variables I and j are the current pointers of the two tables. Set Table C to the new ordered table after merging, and variable k to its current pointer. I and j put the objects with small keywords into C in sequence for A and B traversal. When A or B traverses, the rest of the other table is copied to the new table.

Implementation and comparison of C merge sorting algorithms

<Data Structure> there are some books in the book. You can simply flip the books, or you can simply collect and sort them. There are a lot of online data. You need to learn to find the answer to this elementary question. It is very helpful to make full use of the network in the learning process.

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.