Advertisement
kyoo0000

Lexicography Quicksort C

Nov 27th, 2019
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4.  
  5. int partition(int p, int r, char *v[]) {
  6.     int i, j = p;
  7.     char * pivo = v[r], * temp;
  8.     for(i = p; i < r; i++) {
  9.         if(strcmp(v[i], pivo) < 0) {
  10.             temp = v[j];
  11.             v[j] = v[i];
  12.             v[i] = temp;
  13.             j++;
  14.         }
  15.     }
  16.     v[r] = v[j];
  17.     v[j] = pivo;
  18.     return j;
  19. }
  20.  
  21. void quicksort(int p, int r, char *v[]) {
  22.     int q;
  23.     if(p < r) {
  24.         q = partition(p,r,v);
  25.         quicksort(p,q-1,v);
  26.         quicksort(q+1,r,v);
  27.     }
  28. }
  29.  
  30. //usage
  31. int main(void) {
  32.     char *nomes[] = {"Samuel","Aline","Abner","Luiz","Absolut"};
  33.     quicksort(0,4,nomes);
  34.     int i=0;
  35.     for(i; i < 5; i++)
  36.         printf("%s\n", nomes[i]);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement