acobzew

sem4-problem3-qsort

Apr 25th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #define MAX_ARR_SIZE 3000
  6. #define MAX_STR_LEN 100
  7.  
  8. char a[MAX_ARR_SIZE][MAX_STR_LEN],
  9.         b[MAX_ARR_SIZE][MAX_STR_LEN];
  10.  
  11. void qSort(int l, int r)
  12. {
  13.     if (r - l < 2)
  14.         return;
  15.  
  16.     int m = rand() % (r - l) + l;
  17.     int ll = l, rr = r - 1, i;
  18.  
  19.     for (i = l; i < r; i++)
  20.         if (strcmp(a[i], a[m]) < 0)
  21.             strcpy(b[ll++], a[i]);
  22.         else if (strcmp(a[i], a[m]) > 0)
  23.             strcpy(b[rr--], a[i]);
  24.  
  25.     for (i = ll; i <= rr; i++)
  26.         strcpy(b[i], a[m]);
  27.  
  28.     for (i = l; i < r; i++)
  29.         strcpy(a[i], b[i]);
  30.  
  31.     qSort(l, ll);
  32.     qSort(rr + 1, r);
  33. }
  34.  
  35. int main(int argc, char const *argv[])
  36. {
  37.     srand(time(NULL));
  38.  
  39.     int i, n;
  40.     printf("Number of lines to sort: ");
  41.     scanf("%d", &n);
  42.     for (i = 0; i < n; i++)
  43.         scanf("%s", a[i]);
  44.  
  45.     qSort(0, n);
  46.  
  47.     printf("\n");
  48.     for (i = 0; i < n; i++)
  49.         printf("%s\n", a[i]);
  50.    
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment