Sequencing and sequencing statistics (2)--Quick sort

Source: Internet
Author: User

Quick row I contact also more, from before Noip time algorithm teacher speak of version, to before the data structure class to learn the version, to now "Introduction to the algorithm" in the version, I personally can not distinguish their good or bad, right vote are written, later to distinguish. There are three ways to achieve this:

Noip

void Qsort1 (int *a,int l,int R)
{
int i,j,mid,temp;
i=l;j=r;mid=a[(I+J)/2];
while (I<=J)
{
while (A[i]<mid) i++;
while (A[j]>mid) j--;
if (I<=J)
{
Temp=a[i];a[i]=a[j];a[j]=temp;
i++;j--;
}
}
if (l<j) Qsort1 (A,L,J);
if (i<r) Qsort1 (a,i,r);
}

The idea is to select the median as the reference value, I,j from both ends to the other end of the enumeration, and then from the left to find a smaller than it, from the right to find a larger number, swap position, until the i,j position intermodulation, and then the next round of recursion.

Data:

int partition (int *a,int l,int R)
{
int temp;
while (L<R)
{
while (L<r&&a[l]>=flag) l++;
Temp=a[l];a[l]=a[r];a[r]=temp;
while (L<r&&a[r]<=flag) l++;
Temp=a[l];a[l]=a[r];a[r]=temp;
}
return l;
}
void Qsort2 (int *a,int l,int R)
{
if (l<r)
{
int flag = partition (A,L,R);
Qsort2 (a,l,flag-1);
Qsort2 (A,FLAG+1,R);
}
}

This realization is to encounter the big move back, encounter small on the move forward.

Introduction to Algorithms:

int partition (int *a,int l,int R)
{
int i,j,x,temp;
X=A[R];
I=L-1;
for (j=l;j<r;j++)
if (a[j]<x)
{
i++;
Temp=a[i];a[i]=a[j];a[j]=temp;
}
Temp=a[i+1];a[i+1]=a[r];a[r]=temp;
return i+1;
}
void qsort3 (int *a,int l,int R)
{

if (l<r)
{
int flag = partition (A,L,R);
Qsort3 (a,l,flag-1);
Qsort3 (A,FLAG+1,R);
}
}

Algorithmic format, the version of the introduction of the algorithm is similar to the data structure, but the idea is somewhat different: the introduction of the algorithm is to maintain an adjacent small queue, when the number of the queue to find the right side of the flag tag hours, it and the queue of a number of exchange, it is easy to sort the array into four segments: left l~ I are less than flag,i+1~j are more than flag,j+1~r-1 are not sorted, and R is the flag label.

Po finished three versions of the code, and then to talk about the quick sort, the fast-track as the name implies is very fast sort, it is a sort of in-place algorithm (do not need auxiliary array), unstable, and its expectations are more ideal O (NLGN), but the worst case will be reached O (n^2).

The following are some of the optimization methods for quick-row.

1th, how to become stable. This should not be difficult, is to add a label array, the record is not sorted by the elements of the subscript (b[i]=i), the ordering of the addition of double keywords (first according to the size of the row, the same size according to the label row), this method for the rapid sequencing of the time complexity of the impact is not very large, Therefore, if necessary, it can be modified to suit different situations.

2nd, the worst case for a fast sort is O (n^2), where each partition is divided into a set of (1,n-1), which we do not want to see anyway. So we can introduce randomized fast rows.

For the first version, just a small operation on mid (mid=a[(L+R)/2] to Mid=a[random (L,R)])//Here is doubtful, because after looking at the introduction of the algorithm, it is clear that the worst case of the queue, the algorithm each time the set is divided into equal length two parts, It is said that depth is LGN, but before a lot of circumstances can not get over the topic, and then the use of randomization did go away.

The second version I don't know how to use randomization-. -

The third version is the choice of X on the foot (X=a[r] to X=random (l,r), swap (a[x],a[r]); x=a[r];) So you randomly select a number from L,r as x instead of the original a[r].

The 3rd, similar to the optimization of merge sort, is to use the insertion sort when dividing into a small enough set, which has been said in another article, which should be used when dividing the collection into LGN size.

Sequencing and sequencing statistics (2)--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.