Quicksort

Description
#include <iostream>
using namespace std;
#define maxlen 100000
int partition(int a[], int p, int r)
{
    int x = a[r];
    int i = p - 1, j;
    for (j = p; j < r; j++)
    {
        if (a[j] <= x)
        {
            i = i + 1;
            int temp1 = a[i];
            a[i] = a[j];
            a[j] = temp1;
        }
    }
    int temp2 = a[i + 1];
    a[i + 1] = a[r];
    a[r] = temp2;
    return i + 1;
}
 
void quicksort(int a[], int first, int last)
{
    if (first < last)
    {
        int q = partition(a, first, last);
        quicksort(a, first, q - 1);
        quicksort(a, q, last);
    }
}
 
int main()
{
    int n;
    while (cin >> n)
    {
        int a[maxlen];
        int i;
        for (i = 0; i < n; i++)
            cin >> a[i];
        quicksort(a, 0, n - 1);
        for (i = 0; i < n-1; i++)
            cout << a[i]<<" ";
        cout << a[i];
        cout << endl;
    }
    return 0;
}
/**************************************************************
    Problem: ****
    User: Avivadepp
    Language: C++
    Result: Accepted
    Time:4 ms
    Memory:1596 kb
****************************************************************/

快速排序

Input

For each of the input, the first line of the input will be the size of the array (size < 1,000,000), and the second line is the integers in the array, separated by commas.

Output

For each of the input, your program should use quicksort to sort the integers in increasing order and output them.

Sample Input

31 3 2103 4 6 1 2 6 0 -1 2 5

Sample Output

1 2 3-1 0 1 2 2 3 4 5 6 6

HINT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值