Advertisement
BanyRule

16. qsort

Jul 24th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. /*Задание 16. Быстрая сортировка*/
  2. // переписать!
  3. #include <stdio.h>
  4.  
  5. void qsortx(int *a, size_t low, size_t high) {
  6.     size_t i, j;
  7.     int tmp, pivot;
  8.  
  9.     i = low;
  10.     j = high;
  11.  
  12.     pivot = a[(low + (high-low)/2)];
  13.     do {
  14.         while (a[i] < pivot) {
  15.             i++;
  16.         }
  17.         while (a[j] > pivot) {
  18.             j--;
  19.         }
  20.         if (i <= j) {
  21.             if (a[i] > a[j]) {
  22.                 tmp = a[i];
  23.                 a[i] = a[j];
  24.                 a[j] = tmp;
  25.             }
  26.             i++;
  27.             if (j > 0) {
  28.                 j--;
  29.             }
  30.         }
  31.     } while (i <= j);
  32.  
  33.     if (i < high) {
  34.         qsortx(a, i, high);
  35.     }
  36.     if (j > low) {
  37.         qsortx(a, low, j);
  38.     }
  39. }
  40.  
  41. int main(void) {
  42.     int n, a[64];
  43.    
  44.     scanf("%d\n", &n);
  45.     for(int i = 0; i < n; ++i)
  46.         scanf("%d", &a[i]);
  47.        
  48.     qsortx(a, 0, n - 1);
  49.    
  50.     for(int i = 0; i < n; ++i)
  51.         printf("%d ", a[i]);
  52.        
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement