Fast data structure (recursive Version)

Source: Internet
Author: User

After graduating from college, I used to write all the algorithms in the data structure book, but it was not enough ..
After I learned to use sort in STL, I never wrote too fast. In the future, I want to write these words slowly .. Step by step.
# Include <cstdio>

/* Fast sorting adopts the divide and conquer idea, with two pointers left and right,
* One scan from left to right and one scan from right to left;
* For the left pointer, if the value of the element referred to by the Left pointer is less than or equal to the reference value,
* The pointer is shifted to the right. if the pointer is greater than the reference value, it is exchanged with the reference value;
* Similarly, for a right pointer, if the value of the element referred to by the right pointer is greater than or equal to the reference value,
* Move the pointer to the left. if the pointer is smaller than the reference value, it is exchanged with the reference value. */

// Partition the sequence
Int Part (int array [], int left, int right)
{
Int flag = array [left]; // reference
While (right> left)
{
While (right> left & array [right]> = flag)
{
Right --;
}
Array [left] = array [right];
While (right> left & array [left] <= flag)
{
Left ++;
}
Array [right] = array [left];
}
Array [left] = flag;
Return left;
}

// Recursive sorting
Int quicksort (int array [], int low, int high)
{
Int flag;
If (low {
Flag = Part (array, low, high );
Quicksort (array, low, flag-1 );
Quicksort (array, flag + 1, high );
}
}


Author: sunrise

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.