Sorting algorithm (Advanced chapter, finishing self-study hall online Deng Junhui teacher "data Structure" course)

Source: Internet
Author: User

Advanced article algorithms, including quick sort and hill sort. The quick sort is introduced first.

1. Quicksort,c.a.r.hoare (1934~) Turing award,1980

2. Divide and conquer strategies

Quicksort VS. MergeSort

(1) Divide the sequence into two sub-sequences: S = S1 + S2 scaled down, independent of each other (max (S1) <= min (S2))

(2) After the sub-sequence is "recursively" sorted, the original sequence is naturally ordered sorted (S) = sorted (S1) + sorted (S2)

(3) Recursive base: Only a single element is left, which is itself the solution

(4) MergeSort's calculation difficulty lies in "close", the difficulty of quicksort lies in "cent"

Okay, let's see quicksort. Specific implementations:

(1) Pivot point

The element on the left is no larger than it, and the right element is no smaller than it.

(2) "Divide" [lo, hi-1] = [lo, mi-1] + [mi] + [mi + 1, hi-1];

The algorithm architecture is as follows

1Template <typename t>2 voidVector<t>:: quicksort (rank lo, rank hi)3 {4     if(Hi-lo <2)//Only one element5     {6         return;7     }8Rank mi = partition (lo, hi-1)//construct Pivot9 quicksort (lo, MI);TenQuicksort (mi+1, HI); One}

Visible, the most important thing is partition this step

Pivot point: (1) may not exist;

(2) The pivot point itself must be in place. In position, the element on the left is no bigger than it, and the element on its right is no smaller than it.

(3) in an ordered sequence, all elements are pivot points, and vice versa

Therefore, a quick sort is the process of converting each element in an array to an axis point one by one

(4) Convert any element to pivot point by proper exchange

Above

Sorting algorithm (Advanced chapter, finishing self-study hall online Deng Junhui teacher "data Structure" course)

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.