问题 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
****************************************************************/