Introduction to Algorithms--seventh chapter, Quick Sort

Source: Internet
Author: User

1. Quick Sort Description:

It is divided into three parts: decomposition, settlement and merger based on the divide-and-conquer model;

1) Decomposition:

A[p the array: R] divided into two sub-arrays a[p: Q-1] and A[Q+1..R], yes a[p. Q-1] Each element is less than or equal to a (q)

2) Resolve:

Quick ordering by recursive invocation, a[p of the subarray. Q-1] and A[Q+1..R] Sort

3) Merger:

Merge two ordered arrays, a[p. R

1#include <stdio.h>2#include <stdlib.h>3 4 #defineMaxSize 85 6 intPartition (intA[],intPintR) {7     intx = A[r];//the last element in a[] as the principal, the Datum8     inti = P-1;9     intJ;Ten     inttmp; One      A     //through for () {...} Divide the array, because the last element is the principal, so J < (R-1) -      for(j = P; J < R; J + +){ -         if(A[j] <= x) {//The element is smaller than the primary, placed in front, and swapped two elements for position, I <= J thei = i +1; -              -TMP =A[i]; -A[i] =A[j]; +A[J] =tmp; -     //printf ("\nswaped:a[%d]:%d, a[%d]:%d\n", I, J, A[i], a[j]); +         } A     } at     //Place The main element between less than and greater than two parts -TMP = A[i +1]; -A[i +1] =A[r]; -A[R] =tmp; -      -     return(i +1); in } -  to voidQuickSort (intA[],intPintR) { +     if(P <R) { -         intQ; the          *Q =Partition (A, p, R); $QuickSort (A, p, Q-1);Panax NotoginsengQuickSort (A, q +1, R); -     } the } +  A intMain () { the     intA[maxsize] = {2,1,1,1,3,5,6,4}; +     inti; -      $printf"a[]...\n"); $      for(i =0; i < MaxSize; i++){ -printf"%d", A[i]); -     } the      -QuickSort (A,0, (MaxSize-1));Wuyi      theprintf"\nsorted a[]...\n"); -      for(i =0; i < MaxSize; i++){ Wuprintf"%d", A[i]); -     } Aboutprintf"\ n"); $          -System"Pause"); -     return 0; -}

Introduction to Algorithms--seventh chapter, Quick Sort

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.