fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, k, arr[200005];
  6. void quicksort(int lo, int hi, int sign) {
  7.  
  8. int i,j,m;
  9. i = lo;
  10. j = hi;
  11. m = arr[(lo+hi)>>1];
  12.  
  13. while(i<=j) {
  14. while(sign * arr[i] < m * sign) i++;
  15. while(sign * arr[j] > m * sign) j--;
  16. if(i<=j) {
  17. int temp = arr[i];
  18. arr[i] = arr[j];
  19. arr[j] = temp;
  20. i++;
  21. j--;
  22. }
  23. }
  24.  
  25. if(lo<j) quicksort(lo,j, sign);
  26. if(i<hi) quicksort(i,hi, sign);
  27.  
  28. }
  29.  
  30. int main() {
  31.  
  32. cin>>n>>k;
  33. for(int i = 0; i < n; ++i) {
  34. cin>>arr[i];
  35. }
  36.  
  37. quicksort(0, k-1, 1);
  38. quicksort(k, n, -1);
  39.  
  40. for (int i = 0 ; i < n ; i ++) cout << arr[i] << " ";
  41. }
Success #stdin #stdout 0.01s 5412KB
stdin
7 3
13 1 10 15 3 7 11
stdout
1 10 13 15 11 7 3