Implementation of common sort algorithms (v)-Quick sort

Source: Internet
Author: User

Fast Sorting algorithm idea: Select a pivot element, treats the sort sequence to divide, after the division the sequence one part is smaller than the pivot element, one part is bigger than the pivot element, then carries on these two good dividing sequences to carry on the above process.

Selects a pivot element for a given range of substrings, and returns the location of the split element after the function is executed.
The element before the split element is less than the pivot element, and the element behind it is greater than this element
int Partition (int array[], int low, int high)
{
Take the first element of the subsequence as the pivot element
int pivot = Array[low];

while (Low < high)
{
Look for the first element less than the pivot element in the second half
while (Low < high && Array[high] >= pivot)
{
--high;
}

Swap this element smaller than the pivot element to the first half
Swap (&array[low], &array[high]);

Look for the first element that is larger than the pivot element from the top part of the trip
while (Low < high && Array[low] <= pivot)
{
++low;
}

Swap this element larger than the pivot element to the second half.
Swap (&array[low], &array[high]);
}

Returns the location of the pivot element
return to Low;
}

Quick Sort
void QuickSort (int array[], int low, int high)
{
if (Low < high)
{
int n = Partition (array, low, high);
QuickSort (array, low, n);
QuickSort (array, n + 1, high);
}
}

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.