SharedSchoolSpace/作业/数据结构-金健/C++/第八章作业/第八章作业1.cpp

45 lines
914 B
C++
Raw Normal View History

2024-01-02 11:46:42 +08:00
#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;
}