Basic Sort (V): Summary of basic sorting algorithm

Source: Internet
Author: User

Basics of basic sorting
  • It is not necessary to look for a faster algorithm if a major sorting algorithm is not slower than data processing, such as reading and reading.
  • Assume that the sorted file fits in memory. The sort method is called " internal ordering ." Sorting files from disk is called " external sorting ." The difference is that the internal sort can be very easy to access regardless of the element, but the external sort must be sequential to the element. At least in large chunks of data.
  • the way the sequencing program visits elements: through keyword, direct access to the entire element movement;
  • The sort function has wrapped 3 parameters: arrays, left and right borders with sorted word groupsvoid(Item a[], int l, int r);
  • execution time of the sorting algorithm: When ordering n elements
    • Select Sort, insert sort, bubble sort, and < Span class= "Mrow" id= "mathjax-span-2" > n 2 proportional;
    • Hill sort (improvement of insertion sort) is proportional to N (3/2);
    • keyword-index ordering is proportional to N;
  • how to reduce overhead and speed up sorting : (important)
    • Switch to a more efficient algorithm
    • Shorten the internal cycle
    • Check if there is a comparison or swap operation that can be skipped
  • stability of sorting
    When the keyword of the records to be sorted are different. The sorting result is unique, otherwise the sort result is not unique.
    In the file to be sorted, if there are multiple keyword same records, the relative order of these records with the same keyword remains constant, and the sorting method is stable, and if the relative order between records with the same keyword changes, It is said that this sort method is not stable.
    Attention:
    The stability of the sorting algorithm is for all input instances.

    That is, in all possible input instances, only one instance is required to make the algorithm not meet the stability requirements. Then the sorting algorithm is unstable.

Basic sorting algorithm type 1. Select sort

Select the smallest element in the array, swap it with the first element in the array, and then find the minor element. and swap it with the second element in the array ... Until the entire array is sorted. That is, by constantly selecting the smallest element in the remaining array to implement.

2. Insert sort and Hill sort

By constructing an ordered sequence, a backward-forward scan is made in the sorted sequence for unordered data. Locate the corresponding position insert.

Therefore, in the process of backward forward scanning, it is necessary to re-shift the ordered elements gradually. Provides the insertion space for the newest element.

3. Bubble sort

Traversing the file, assuming that the next 2 elements are not in the correct order of size, swap the two, repeat until sorted

4. Index and pointer sorting, list sorting, keyword sorting

Index and pointer ordering, list ordering: Indirect ordering, flexibility, and no changes to the original data and keyword information.

Basic Sort (V): Summary of basic sorting algorithm

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.