Two-way Merge Sorting Algorithm Implementation-complete C language program and merge Language Program

Source: Internet
Author: User

Two-way Merge Sorting Algorithm Implementation-complete C language program and merge Language Program

/*************************************** **************************************** * **************** 1. set two pointers. The initial position is the start position of the two sorted sequences. compare the elements pointed to by the two pointers, select a relatively small element into the merging space, and move the pointer to the next position 3. repeat Step 3 until a pointer reaches the end of the sequence 4. copy all the remaining elements of another sequence directly to the end of the merge sequence and merge the sequence. The specific working principle of the merge sequence is as follows (assuming there are n elements in the sequence): 1. merge two adjacent numbers to form a floor (n/2) sequence. After sorting, each sequence contains two elements. merge the above sequence to form the floor (n/4) sequence. Each sequence contains four elements 3. repeat Step 2 until all elements are sorted and merged, which is stable. The worst, average, and best time is O (nlogn ). But it requires additional storage space. He asked hovertree.com Merge Sort (Merge Sort, hereinafter referred to as MS) is a model of the use of the division and control method. The main algorithm operations can be divided into the following steps: Step 1: divide n elements into two subsequences containing n/2 elements Step 2: use MS to recursively sort two subsequences (the entire original sequence can be divided into n subsequences) Step 3: merge two sorted sequences ********************************** **************************************** * *********************/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <climits> # include <cstdlib> # include <time. h >#include <cstdlib> # include <cstdio> using namespace std; void Random (int a [], int n) {int I = 0; srand (unsigned) time (NULL); while (I <n) {a [I ++] = rand () ;}} void merge (int * a, int low, int mid, int high) // merge operations {int k, begin1, begin2, end1, end2; begin1 = low; end1 = mid; begin2 = mid + 1; end2 = high; int * temp = (int *) malloc (high-low + 1) * sizeof (int); for (k = 0; begin1 <= end1 & begin2 <= end2; k ++) // sort from small to large {if (a [begin1] <= a [begin2]) temp [k] = a [begin1 ++]; else temp [k] = a [begin2 ++];} if (begin1 <= end1) // left remaining memcpy (temp + k, a + begin1, (end1-begin1 + 1) * sizeof (int); else // right remaining memcpy (temp + k, a + begin2, (end2-begin2 + 1) * sizeof (int); memcpy (a + low, temp, (high-low + 1) * sizeof (int )); // copy the sorted data to the original array free (temp); // release space} void merge_sort (int * a, unsigned int begin, unsigned int end) {int mid; if (begin <end) {mid = begin + (end-begin)> 1; // mid = (end + begin)/2; prevent data addition overflow merge_sort (, begin, mid); // sub-governance merge_sort (a, mid + 1, end); // sub-governance merge (a, begin, mid, end ); // merge two sorted columns} int main () {int a [20]; Random (a, 20); for (int I = 0; I <20; I ++) {cout <"<a [I] <" ";} merge_sort (a, 0, 20-1); for (int I = 0; I <20; I ++) {cout <"" <a [I] <endl;} return 0 ;}

Recommended: http://www.cnblogs.com/roucheng/p/cppjy.html

Related Article

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.