用于存放学校的作业便于复习。
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

  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4. #define N 1000
  5. int a[N];
  6. int *_one_process(int *_begin, int *_end){
  7. int temp = *_begin;
  8. int len = _end - _begin;
  9. int *left = _begin, *right = _end;
  10. for (int *p = _begin + 1; p <= _end; p++) {
  11. if (*p <= temp) {
  12. swap(*left++, *p);
  13. } else {
  14. swap(*right--, *p);
  15. }
  16. }
  17. assert(left == right);
  18. *left = temp;
  19. return left;
  20. }
  21. void _fastsort(int *_begin, int *_end) {
  22. if (_begin >= _end)
  23. return;
  24. int *middle = _one_process(_begin, _end);
  25. _fastsort(_begin, middle - 1);
  26. _fastsort(middle + 1, _end);
  27. }
  28. void fast_sort(int *a, int len) {
  29. _fastsort(a, a + len - 1);
  30. }
  31. int main()
  32. {
  33. int n;
  34. cin >> n;
  35. for (int i = 0 ; i < n; i++) cin >> a[i];
  36. fast_sort(a, n);
  37. for (int i = 0; i < n; i++) cout << a[i] << " ";
  38. cout << endl;
  39. return 0;
  40. }