1. //Trabalho 1
  2.  
  3. //Autor: Pedro Henrique da Costa Braga
  4.  
  5. //DRE: 109062092
  6.  
  7.  
  8.  
  9. #include <stdio.h>
  10.  
  11. #include <string.h>
  12.  
  13. #include <stdlib.h>
  14.  
  15.  
  16.  
  17. int* vetor;
  18.  
  19. int tam;
  20.  
  21. //======================quickSort==========================================================
  22.  
  23.  
  24. void troca(int *x, int *y)
  25.  
  26. {
  27.  
  28. int temp;
  29.  
  30. temp = *x;
  31.  
  32. *x = *y;
  33.  
  34. *y = temp;
  35.  
  36. }
  37.  
  38. int pivoteia(int v[],int antes,int depois)
  39.  
  40. {
  41.  
  42. int i,j;
  43.  
  44. i = antes;
  45.  
  46. for(j = antes + 1; j <= depois; j++)
  47.  
  48. {
  49.  
  50. if(v[j] < v[depois])
  51.  
  52. {
  53.  
  54. i++;
  55.  
  56. troca(&v[i], &v[j]);
  57.  
  58. }
  59.  
  60. }
  61.  
  62. troca(&v[antes], &v[i]);
  63.  
  64. return i;
  65.  
  66. }
  67.  
  68.  
  69. void quickSort(int v[], int antes, int depois)
  70.  
  71. {
  72.  
  73. int pivo;
  74.  
  75. if(antes > depois)
  76.  
  77. {
  78.  
  79. pivo = pivoteia(v,antes,depois);
  80.  
  81. quickSort(v, antes, pivo - 1);
  82.  
  83. quickSort(v, pivo + 1, depois);
  84.  
  85. }
  86.  
  87. }
  88.  
  89.  
  90. //=================================Função de Entrada================================================
  91.  
  92.  
  93. void entrada()
  94.  
  95. {
  96.  
  97. int temp;
  98.  
  99. tam = 0;
  100.  
  101. int max = 10;
  102.  
  103. vetor = (int *)malloc(max*sizeof(int));
  104.  
  105. while (scanf ("%d", &temp) != EOF)
  106.  
  107. {
  108.  
  109. tam++;
  110.  
  111. if(tam >= max - 1)
  112.  
  113. {
  114.  
  115. max = max + 10;
  116.  
  117. vetor = (int *)realloc(vetor,max*sizeof(int));
  118.  
  119. }
  120.  
  121. vetor[tam-1] = temp;
  122.  
  123. }
  124.  
  125. }
  126.  
  127.  
  128. //==================================Função main=====================================================
  129.  
  130.  
  131. int main (int argc, char *argv[])
  132.  
  133. {
  134.  
  135. if (argc < 3)
  136.  
  137. {
  138.  
  139. if(strcmp(argv[1],"-h") == 0) //Ajuda
  140.  
  141. {
  142.  
  143. printf("\nAutor: Pedro Henrique da Costa Braga\n");
  144.  
  145. printf("DRE: 109062092\n\n");
  146.  
  147. printf("Aqui se encontram implementados os algoritmos de quickSort e quickSelect. O programa utiliza os parametros da linha de comando para decidir o que fazer:\n");
  148.  
  149. printf("\n1) quick -h -> Imprime texto de ajuda;\n");
  150.  
  151. printf("\n2) quick -o k -> roda o quickSort, e depois imprime o valor na k-esima posicao;\n");
  152.  
  153. printf("\n3) quick -s k -> roda o quickSelect, e depois imprime o valor k-esima posicao;\n");
  154.  
  155. return 0;
  156.  
  157. }
  158.  
  159. else
  160.  
  161. {
  162.  
  163. printf("Parametro invalido\nDigite 'quick -h' para ver os comandos aceitados");
  164.  
  165. return 0;
  166.  
  167. }
  168.  
  169. }
  170.  
  171. else if(argc > 3)
  172.  
  173. {
  174.  
  175. printf("Parametro invalido\nDigite 'quick -h' para ver os comandos aceitados");
  176.  
  177. return 0;
  178.  
  179. }
  180.  
  181. else
  182.  
  183. {
  184.  
  185.  
  186.  
  187. if(strcmp(argv[1],"-o") == 0) //quickSort
  188.  
  189. {
  190.  
  191. int i;
  192.  
  193. entrada();
  194.  
  195. // quickSort(vetor[],0,tam);
  196.  
  197. i = atoi(argv[2]);
  198.  
  199. printf("%d\n", vetor[i]);
  200.  
  201. }
  202.  
  203. else if(strcmp(argv[1],"-s") == 0) //quickSelect
  204.  
  205. {
  206.  
  207. printf("Quickselect\n");
  208.  
  209. }
  210.  
  211. else
  212.  
  213. {
  214.  
  215. printf("Parametro invalido\nDigite 'quick -h' para ver os comandos aceitados");
  216.  
  217. }
  218.  
  219. return 0;
  220.  
  221. }
  222.  
  223. }
  224.