Example code for C ++ to implement merging and sorting

Source: Internet
Author: User

Writing this algorithm is very simple, and the principle is very simple, but the trap is used in this algorithm for arrays, access and control of the underlying object. Generally, merge sorting uses 1 as the subscript, but today we want to use 0 as the subscript. En ~ I never turned my mind around, but it was finally implemented.

The source code is as follows:

# Include <iostream> # include <ctime> # include <cstdlib> using namespace std; const int N = 204800; void Merge (int arr [], int p, int q, int r) {int n1 = q-p + 1; int n2 = r-q + 1; int left [n1 + 1], right [n2]; for (int I = 0; I! = N1; ++ I) {left [I] = arr [p + I];} left [n1] = N; for (int j = 0; j! = N2-1; ++ j) {right [j] = arr [q + j + 1];} right [n2-1] = N; int I = 0, j = 0; for (int k = p; k! = R + 1; ++ k) {if (left [I]> right [j]) {arr [k] = right [j]; ++ j ;} else {arr [k] = left [I]; ++ I ;}} void MergeSort (int arr [], int p, int r) {if (p <r) {int q = (p + r)/2; MergeSort (arr, p, q); MergeSort (arr, q + 1, r); Merge (arr, p, q, r) ;}} int main () {int arr [10] = {300, 60, 22, 16, 85, 89, 30, 99,103, 55}; MergeSort (arr, 0, 9); for (int I = 0; I <10; ++ I) {cout <arr [I] <endl;} return 0 ;}


The starting ctime was originally intended to use time as the seed random number. However, this random number is slightly in line with the normal distribution and is discarded. The random number code is as follows:

Srand (time (NULL); int arr [rand () % 100]; for (int I = 0; I! = Sizeof (arr)/sizeof (int) ;++ I) {arr [I] = rand () % 500 ;}for (int I = 0; I! = Sizeof (arr)/sizeof (int); ++ I) {cout <arr [I] <""; if (I + 1) % 10 = 0) cout <endl ;}cout <"end" <endl; MergeSort (arr, 0, sizeof (arr)/sizeof (int )); for (int I = 0; I! = Sizeof (arr)/sizeof (int); ++ I) {cout <arr [I] <""; if (I + 1) % 10 = 0) cout <endl ;}

C ++ merge sorting implementation (Introduction to Algorithms)


Basic idea: set two ordered subsequences (equivalent to the input sequence) to the adjacent positions in the same sequence: array [low .. m], array [m + 1 .. high], first merge them into a local temporary storage sequence temp (equivalent to the output sequence), and then copy temp back to array [low .. to complete the sorting.

# Include <iostream> using namespace std; void merge (int * data, int p, int q, int r) {int n1, n2, I, j, k; int * left = NULL, * right = NULL; n1 = q-p + 1; n2 = r-q; left = (int *) malloc (sizeof (int) * (n1); right = (int *) malloc (sizeof (int) * (n2); for (I = 0; I <n1; I ++) // assign left [I] = data [p + I] to the left array; for (j = 0; j <n2; j ++) // assign right [j] = data [q + 1 + j] to the right array; I = j = 0; k = p; while (I <n1 & j <n2) // compares the array element values and merges them into the data array {if (left [I] <= right [j]) data [k ++] = left [I ++]; else data [k ++] = right [j ++] ;}for (; I <n1; I ++) // if the left array has element surplus, the remaining elements are merged into the data array data [k ++] = left [I]; for (; j <n2; j ++) // if the right array has element surplus, the remaining element is merged into the data array data [k ++] = right [j];} void mergeSort (int * data, int p, int r) {int q; if (p <r) // no sorting is required if there is only one or no record. {q = (int) (p + r)/2); // divide the data array into two halves: mergeSort (data, p, q); // recursively split the left array mergeSort (data, q + 1, r); // recursively splits the right array merge (data, p, q, r); // merges the array} int main () {int n; int * input = NULL; // cout of input data <"Enter the length of the array:"; cin> n; input = (int *) malloc (sizeof (int) * (n); cout <"assign a value to the array:"; for (int I = 0; I <n; ++ I) {cin> input [I] ;}// process data in mergeSort (input, 0, n-1); // output result for (int I = 0; I <n; ++ I) cout <input [I] <""; cout <endl; system ("pause"); return 0 ;}


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.