Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #define MAX_ARR_SIZE 3000
- #define MAX_STR_LEN 100
- char a[MAX_ARR_SIZE][MAX_STR_LEN],
- b[MAX_ARR_SIZE][MAX_STR_LEN];
- void qSort(int l, int r)
- {
- if (r - l < 2)
- return;
- int m = rand() % (r - l) + l;
- int ll = l, rr = r - 1, i;
- for (i = l; i < r; i++)
- if (strcmp(a[i], a[m]) < 0)
- strcpy(b[ll++], a[i]);
- else if (strcmp(a[i], a[m]) > 0)
- strcpy(b[rr--], a[i]);
- for (i = ll; i <= rr; i++)
- strcpy(b[i], a[m]);
- for (i = l; i < r; i++)
- strcpy(a[i], b[i]);
- qSort(l, ll);
- qSort(rr + 1, r);
- }
- int main(int argc, char const *argv[])
- {
- srand(time(NULL));
- int i, n;
- printf("Number of lines to sort: ");
- scanf("%d", &n);
- for (i = 0; i < n; i++)
- scanf("%s", a[i]);
- qSort(0, n);
- printf("\n");
- for (i = 0; i < n; i++)
- printf("%s\n", a[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment