问题 C: 快速排序 qsort [2*]

389 篇文章 1 订阅
144 篇文章 2 订阅

问题 C: 快速排序 qsort [2*]
时间限制: 1 Sec 内存限制: 128 MB
献花: 43 解决: 34
[献花][花圈][TK题库]
题目描述
输入n个整数,用快速排序的方法进行排序
Input
第一行数字n 代表接下来有n个整数
接下来n行,每行一个整数
**Output
Output**

升序输出排序结果
每行一个数据
Sample Input
5
12
18
14
13
16
Sample Output
12
13
14
16
18
Hint
n<=5000
每个数据<=5000

方法一:直接用库函数sort

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;

const int MaxN = 5010;
int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
    //fstream cin("data.txt");
#endif // _DEBUG

    int n, data[MaxN];
    while (~scanf("%d", &n))
    {
        for (int i = 0; i < n; ++i)
            scanf("%d", &data[i]);

        sort(data, data + n);

        for (int i = 0; i < n; ++i)
            printf("%d\n", data[i]);
    }



#ifdef _DEBUG
    //cin.close();
#ifndef _CODEBLOCKS
    system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG

    return 0;
}

/**************************************************************
    Problem: 2843
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:2 ms
    Memory:1708 kb
****************************************************************/

方法二:自己实现基础版快排函数

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <ctime>

using namespace std;

const int MaxN = 5010;

int partition(int a[], int L, int R)
{
    int tmp = a[L];

    while (L < R)
    {
        while (L < R && tmp <= a[R]) R--;
        a[L] = a[R];
        while (L<R && tmp >= a[L])L++;
        a[R] = a[L];
    }
    a[L] = tmp;
    return L;
}

void quickSort(int a[], int L, int R)
{
    if (L >= R)return;
    int pos = partition(a, L, R);
    quickSort(a, L, pos - 1);
    quickSort(a, pos + 1, R);
}

int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
    //fstream cin("data.txt");
#endif // _DEBUG

    int n, data[MaxN];
    srand((unsigned int)time(NULL));
    while (~scanf("%d", &n))
    {
        for (int i = 0; i < n; ++i)
            scanf("%d", &data[i]);

        quickSort(data, 0, n - 1);

        for (int i = 0; i < n; ++i)
            printf("%d\n", data[i]);
    }



#ifdef _DEBUG
    //cin.close();
#ifndef _CODEBLOCKS
    system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG

    return 0;
}

/**************************************************************
    Problem: 2843
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:1 ms
    Memory:1708 kb
****************************************************************/

方法三:基于基础版改进的快排

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <ctime>

using namespace std;

const int MaxN = 5010;

int partition(int a[], int L, int R)
{
    int pos = (rand() % (R - L)) + L, tmp;
    tmp = a[pos]; a[pos] = a[L];

    while (L < R)
    {
        while (L < R && tmp <= a[R]) R--;
        a[L] = a[R];
        while (L<R && tmp >= a[L])L++;
        a[R] = a[L];
    }
    a[L] = tmp;
    return L;
}

void quickSort(int a[], int L, int R)
{
    if (L >= R)return;
    int pos = partition(a, L, R);
    quickSort(a, L, pos - 1);
    quickSort(a, pos + 1, R);
}

int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
    //fstream cin("data.txt");
#endif // _DEBUG

    int n, data[MaxN];
    srand((unsigned int)time(NULL));
    while (~scanf("%d", &n))
    {
        for (int i = 0; i < n; ++i)
            scanf("%d", &data[i]);

        quickSort(data, 0, n - 1);

        for (int i = 0; i < n; ++i)
            printf("%d\n", data[i]);
    }



#ifdef _DEBUG
    //cin.close();
#ifndef _CODEBLOCKS
    system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG

    return 0;
}

/**************************************************************
    Problem: 2843
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:1 ms
    Memory:1708 kb
****************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值