Sort and search at a glance

Source: Internet
Author: User

One. Merge sort

O (NLOGN) in the time complexity of the sorting method, stable only merge sort, heap sorting and fast sorting are not stable.

In cases where the length of the array is short, instead of recursion, choose a different sort scheme, such as insert sort.

/*** Algorithm of MergeSort *@paramNums*/     Public voidMergeSort (int[] nums) {Mergesorthelp (nums,0, Nums.length-1); }         Public voidMergesorthelp (int[] Nums,intLowintHigh ) {        if(Low >=High )return; intMid = (low + high)/2;        Mergesorthelp (Nums, Low, mid); Mergesorthelp (Nums, Mid+1, high);    Merge (Nums, Low, Mid, high); }         Public voidMergeint[] Nums,intLowintMidintHigh ) {        int[] copy =New int[Nums.length]; intS1 = low, S2 = mid+1; intt = low;//The index of copy         while(S1 <= mid && S2 <=High ) {            if(Nums[s1] <=Nums[s2]) copy[t+ +] = nums[s1++]; Elsecopy[t+ +] = nums[s2++]; }         while(S1 <=mid) Copy[t+ +] = nums[s1++];  while(S2 <=High ) Copy[t+ +] = nums[s2++];  for(intI=low; i<=high; i++) Nums[i]=Copy[i]; }

Two. Outside sort (External sorting)

    • Out-of-order refers to the sort algorithm that handles data that exceeds the memory limit. Usually, the intermediate results are placed on the slower-reading external memory (usually the hard disk);
    • The general strategy is to "sort-merge" the strategy:
      • In the sort stage, it reads the amount of data that can be put in memory, sorts the output with temporary files, and organizes the data to be sorted into several ordered temporary files.
      • Merge phase to combine these temporary files into large, ordered files.
      • For example, use 100M of memory to sort 900M of data:
        • Reads 100M of data into memory and sorts it in a regular way (such as heap sorting);
        • Writes the sorted data to the hard disk;
        • Repeat two steps to get 9 100M blocks;
        • The memory of 100M is divided into 10 parts, the first 9 is the input buffer, and the 10th is the output buffer. such as the first 9 points each 8 m, the 10th part 18M;
        • Executes the nine-way merge algorithm, outputting the result to the output buffer:
          • If the output buffer is full, write the data to the target file and empty the buffer;
          • If the input buffer is empty, the next copy of the corresponding file is read in.

---restore content ends---

Sort and search at a glance

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.