用于存放学校的作业便于复习。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

44 lines
914 B

#include <iostream>
#include <cassert>
using namespace std;
#define N 1000
int a[N];
int *_one_process(int *_begin, int *_end){
int temp = *_begin;
int len = _end - _begin;
int *left = _begin, *right = _end;
for (int *p = _begin + 1; p <= _end; p++) {
if (*p <= temp) {
swap(*left++, *p);
} else {
swap(*right--, *p);
}
}
assert(left == right);
*left = temp;
return left;
}
void _fastsort(int *_begin, int *_end) {
if (_begin >= _end)
return;
int *middle = _one_process(_begin, _end);
_fastsort(_begin, middle - 1);
_fastsort(middle + 1, _end);
}
void fast_sort(int *a, int len) {
_fastsort(a, a + len - 1);
}
int main()
{
int n;
cin >> n;
for (int i = 0 ; i < n; i++) cin >> a[i];
fast_sort(a, n);
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl;
return 0;
}