Chapter 7 "Quick Sort Hoare version HOARE-QUICKSORT)

Source: Internet
Author: User

This is an earlier version. Hoare is the name of the person. The partition () function in this version is different from the current one. I think this old version is not as easy to understand as the current version. It may seem easy to understand on the big aspect, but it is very troublesome to consider pointer movement when writing code.

# Include <string. h> # include <time. h> # define buffer_size 10int hoarepartition (int * a, int P, int R) {int TMP = 0; int I = 0; Int J = 0; int x = 0; I = P; j = r; X = A [p]; while (1) {While (A [J]> X) {J --;} while (A [I] <X) {I ++;} if (I <j) {TMP = A [I]; A [I] = A [J]; A [J] = TMP;} else {return I ;}} void hoarequicksort (int * a, int P, int R) {int q = 0; if (P <r) {q = hoarepartition (A, P, R); hoarequicksort (A, P, q-1); hoarequicksort (A, q + 1, R );}} int main () {int I = 0; Int J = 0; int A [buffer_size]; // randomly generated array srand (unsigned) Time (null )); for (j = 0; j <buffer_size; j ++) {A [J] = rand () % 100;} printf ("randomly generated array: \ n "); for (I = 0; I <buffer_size; I ++) {printf ("% d", a [I]);} printf ("\ n "); hoarequicksort (A, 0, BUFFER_SIZE-1); printf ("Sort arrays quickly: \ n"); for (I = 0; I <buffer_size; I ++) {printf ("% d", a [I]);} system ("pause"); Return 0 ;}

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.