Quick sorting based on Introduction to Algorithms

Source: Internet
Author: User

In the introduction to algorithms, the reference element is selected as the final one, and the data structure textbook I used is the first;
However, the principle is the same. The elements in array a are divided into three parts by using the Qartion (int a [], int l, int r) function, which is smaller than a [r, larger than a [r], a [r]; www.2cto.com
Finally, the location of the base element is returned;
[Cpp]
# Include <stdio. h>
# Include <iostream>
# Include <string. h>
# Include <algorithm>
# Include <math. h>
Using namespace std;
Const int N = 99;
Int Qartion (int a [], int l, int r)
{
Int tmp = a [r];
Int I = L-1;
For (int j = l; j <= R-1; j ++)
{
If (a [j] <= tmp)
{
I ++;
Swap (a [I], a [j]);
}
}
Swap (a [I + 1], a [r]);
Return I + 1;
}
Void Qsort (int a [], int l, int r)
{
If (l <r)
{
Int p = Qartion (a, l, r );
Qsort (a, l, P-1 );
Qsort (a, p + 1, r );
}
}
Int main ()
{
Int n;
While (~ Scanf ("% d", & n ))
{
Int a [100];
For (int I = 0; I <n; I ++)
{
Cin> a [I];
}
Qsort (a, 0, n-1 );
For (int I = 0; I <n; I ++)
{
Cout <a [I] <"";
}
Cout <endl;
}
Return 0;
}
Author: Java_beginer1

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.