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
****************************************************************/
#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
本文介绍了一个使用C++实现的快速排序算法。该算法通过递归地将数组分为较小和较大的两个子集,并对这两个子集进行排序来实现整个数组的排序。输入为一组整数,输出为升序排列的整数序列。
561

被折叠的 条评论
为什么被折叠?



