Introduction to MIT (4)-Fast sorting and randomization Algorithms

Source: Internet
Author: User

  Baidu Encyclopedia:Quicksort is an improvement of Bubble sorting, which was proposed by C. A. R. Hoare in 1962. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.

  Wikipedia:Quick sorting uses the Divide and conquer policy to Divide a serial (list) into two sub-serial (sub-lists ).

Steps:

  1. Pick out an element from the sequence, called a benchmark ).
  2. Re-sort the series. All elements are placed before the benchmark values smaller than the benchmark values, and all elements are placed behind the benchmark values larger than the benchmark values (the same number can reach either side ). After the partition exits, the benchmark is in the middle of the series. This is calledPartition)Operation.
  3. Recursively (recursive) sorts the subseries smaller than the reference value element and the subseries larger than the reference value element.

The bottom of recursion is that the number of columns is zero or one, that is, they are always sorted. Although this algorithm is always recursive, it always exits, because in each iteration, it will at least place an element at its final position.

  

Quick sorting is in-situ sorting, just like inserting sorting without extra space. The efficiency depends on the sorting of input data. In the worst case, the original data is ordered or backward, Which is O (n2 ).

  Comparison between quick sorting and Merge Sorting:Both of them can achieve O (nlgn) time complexity, and the time complexity of O (nlgn) can be ensured when the Merge Sorting is the worst, the worst case of fast sorting is the time complexity of O (n2). However, in actual use, fast sorting is usually faster (about 3 times) than Merge Sorting ), in addition, quick sorting is performed in local order, which is better in Computer Virtual Memory/buffer.

  Improvement:

(1) drag the molecular array for hours and directly sort it by insert;

(2) Selection of elements in the partition process: 1. Random selection; 2. Three-point median selection.

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.