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