Introduction to algorithms-Chapter 7-quick sorting

Source: Internet
Author: User

Chapter 7 quick sorting

 

Summary: This chapter describes the fast sorting algorithm, analyzes the complexity of the algorithm, and describes the random version of quick sorting.

 

1.Quick sorting

Partition (A, P, R) converts a [p... R] performs local shuffling and returns Q, where a [p... The elements in q-1] are less than a [Q], a [q + 1 ,... The elements in R] are greater than a [Q].

Recursive sorting of quicksort (A, P, R)

Analysis: Worst Case O (N ^ 2)

Best case o (nlgn)

Average O (N ^ 2)

 

Pseudocode

Partition (A, P, R)

X <-A [R]

I <-P-1

For j <-P to R-1

Do if a [J] <= x

Then I <-I + 1

Exchange a [I] <-> A [J]

Exchange a [I + 1] <-> A [R]

Return I + 1

 

Quicksort (A, P, R)

If P <r

Then q <-partion (A, P, R)

Quicksort (A, P, q-1)

Quicksort (A, q + 1, R)

 

 

# Include <iostream> <br/> using namespace STD; <br/> int partition (int A [], int P, int R) // A [p... r] <br/>{< br/> int I = p-1; <br/> Int J = P; <br/> int temp; <br/> int key = A [R]; <br/> for (j = P; j <r; j ++) <br/>{< br/> if (a [J] <= Key) <br/>{< br/> I ++; <br/> temp = A [I]; <br/> A [I] = A [J]; <br/> A [J] = temp; <br/>}< br/> temp = A [I + 1]; <br/> A [I + 1] = A [R]; <br/> A [R] = temp; <br/> return I + 1; <br/>}< br/> void quicksort (int A [], int P, int R) <br/>{< br/> If (P> = r) <br/> return; <br/> int q = partition (A, P, r); <br/> quicksort (A, P, q-1); <br/> quicksort (A, q + 1, R ); <br/>}< br/> int main () <br/> {<br/> int A [10] = }; <br/> quicksort (A, 0, 9); <br/> for (INT I = 0; I <10; I ++) <br/> cout <A [I] <Endl; <br/>}< br/>

2.Quick Sort random Edition

A random algorithm is introduced. In partition, a [R] is not used as the primary element. Instead, the sub-array a [p... R] randomly selects an element as the principal component.

Analysis: Worst Case O (N ^ 2)

Best case o (nlgn)

Average O (N ^ 2)

 

Pseudocode

Randomized-partition (A, P, R)

I <-random (P, R)

Exchange a [I] <-> A [R]

Return partition (A, P, R)

 

# Include <iostream> <br/> # include <ctime> <br/> using namespace STD; <br/> void Exchange (Int & A, Int & B) <br/>{< br/> int temp = A; <br/> A = B; <br/> B = temp; <br/>}</P> <p> int randomizedpartition (int A [], int P, int R) <br/>{< br/> int I = p-1; <br/> Int J = P; <br/> srand (Time (null); <br/> int K = P + rand () % (r-p + 1); <br/> Exchange (A [K], a [R]); <br/> for (j = P; j <R; j ++) <br/>{< br/> if (a [J] <= A [R]) <br/>{< br/> I ++; <br/> Exchange (A [I], a [J]); <br/>}< br/> Exchange (A [I + 1], A [R]); <br/> return I + 1; <br/>}</P> <p> void randomizedquicksort (int A [], int P, int R) <br/>{< br/> If (P> = r) <br/> return; <br/> int q = randomizedpartition (A, P, r); <br/> randomizedquicksort (A, P, q-1); <br/> randomizedquicksort (A, q + 1, R ); <br/>}< br/> int main () <br/> {<br/> int A [10] = }; <br/> randomizedquicksort (A, 0, 9); <br/> for (INT I = 0; I <10; I ++) <br/> cout <A [I] <Endl; <br/>}</P> <p>

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.