Fast sorting algorithm

Source: Internet
Author: User

/* date:2014.12.14
Quick sorting ideas: Similar to bubble sort, based on comparison and exchange to achieve sorting, improved efficiency.
Process: 1). First set a cutoff value, through which the array is divided into left and right parts, the left sequence is less than equal to it, and the sequence is greater than equal to it;
2). For the left and right sequences, perform (1) operations respectively;
3). Repetition (2), equivalent to recursion, until ordered.
Time complexity: Worst O (n^2), average O (Nlogn).
Spatial complexity: O (log2 (n))-O (n).
is an unstable sorting algorithm.
*/

void QuickSort (int *arr,int left,int right)
{
int f,temp;
int ltemp,rtemp;


Ltemp = left;
Rtemp = right;
f=arr[(left + right)/2];
while (Ltemp < rtemp)
{
while (Arr[ltemp] < f)
{
+ + ltemp;
}
while (F < arr[rtemp])
{
--rtemp;
}
if (ltemp <= rtemp)
{
temp = arr[ltemp];
Arr[ltemp] = arr[rtemp];
ARR[RTEMP] = temp;
+ + ltemp;
--rtemp;
}
}

if (ltemp = = rtemp)
{
Ltemp + +;
}
if (left < rtemp)
{
QuickSort (arr,left,ltemp-1);
}
if (Ltemp < right)
{
QuickSort (arr,rtemp + 1,right);
}

}


Fast sorting algorithm

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.