Data Structure BASICS (5): Data Structure Basics

Source: Internet
Author: User

Data Structure BASICS (5): Data Structure Basics

The basic idea of Merge Sorting:

"Merge" two or more ordered subsequences into an ordered sequence: assuming that the table to be sorted contains n records, it can be considered as n ordered subtables, the length of each sub-table is 1, and then merged into two to obtain [n/2] ordered tables with a length of 2 or 1. Then, merge the quantities ,...., this is repeated until it is merged into an ordered table with a length of n. This sorting method is called 2-way merge sorting. an example of a 2-path Merge Sorting:


/** Note: The ordered record sequence initList [left: mid] And initList [mid + 1: right] are merged into an ordered record sequence initList [left: right] initList: the original ordered sequence is [divided into two segments] tmpList: the central sequence required during the merge process. left: The subscript mid of the leftmost element of initList: subscript right pointing to the last element of the first ordered sequence: subscript of the rightmost element of initList */template <typename Type> int Merge (Type * initList, Type * tmpList, int left, int mid, int right) {// copy the array to be merged to the tmpList to std: copy (initList + left, initList + right + 1, tmpList + left ); // same as the following: // for (int I = left; I <= right; ++ I) // {// tmpList [I] = initList [I]; //} int s1 = left, s2 = mid + 1; int iResult = left; while (s1 <= mid & s2 <= right) {if (tmpList [s1] <= tmpList [s2]) {initList [iResult ++] = tmpList [s1 ++];} else {initList [iResult ++] = tmpList [s2 ++] ;}} int * end; if (s1 <= mid) end = std: copy (tmpList + s1, tmpList + mid + 1, initList + iResult); if (s2 <= right) end = std: copy (tmpList + s2, tmpList + right + 1, initList + iResult ); return end-(initList + left); // in the same way: in fact, only one of the two loops will execute // while (s1 <= mid) // {// initList [iResult ++] = tmpList [s1 ++]; //} // while (s2 <= right) /// {// initList [iResult ++] = tmpList [s2 ++]; //} // return iResult ;}
// Binary Merge Sorting-recursive algorithm template <typename Type> void mergeSort (Type * initList, Type * tmpList, int left, int right) {if (left> = right) return; int mid = (left + right)/2; mergeSort (initList, tmpList, left, mid); // sort the elements on the left by mergeSort (initList, tmpList, mid + 1, right); // sort the right elements by Merge (initList, tmpList, left, mid, right); // Merge}

It can be seen that the time complexity of merging and sorting n records is merge (nlogn ). That is:

(1) the time complexity of each Merge (merge) is O (n );

(2) A total of [logn] queries are required.

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.