Advertisement
iuliaa

quick

Apr 29th, 2020
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void quicksort(int a[100], int s, int d) {
  5.        int i = s, j = d;
  6.        int x = a[(s + d) / 2];
  7.        do {
  8.               while (a[i]<x) i++;
  9.               while (a[j]>x) j--;
  10.               if (i <= j) {
  11.                      int temp = a[i];
  12.                      a[i] = a[j];
  13.                      a[j] = temp;
  14.                      i++;
  15.                      j--;
  16.               }
  17.        } while (i <= j);
  18.  
  19.        if (s<j) quicksort(a,s, j);
  20.        if (d>i) quicksort(a,i, d);
  21. }
  22. int a[100], i, n;
  23. int main()
  24. {
  25.     printf("Nr de elemente: ");
  26.     scanf("%d", &n);
  27.     for(i=1;i<=n;i++)
  28.         scanf("%d", &a[i]);
  29.     quicksort(a,1,n);
  30.     for(i=1;i<=n;i++)
  31.         printf("%d ", a[i]);
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement