Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. void insertionSort(int *vetor, int lenght)
  2. {
  3. int i,j,atual;
  4. for(i=0; i<lenght; i++)
  5. {
  6. atual = vetor[i];
  7. j = i-1;
  8.  
  9. while((j>=0)&&(atual<vetor[j]))
  10. {
  11. vetor[j + 1] = vetor[j];
  12. j = j-1;
  13. }
  14. vetor[j+1] = atual;
  15. }
  16. }
  17. void merge(int vetor[], int comeco, int meio, int fim)
  18. {
  19. int com1 = comeco, com2 = meio+1, comAux = 0;
  20. int vetAux[fim-comeco+1];
  21. while(com1<=meio && com2<=fim)
  22. {
  23. if(vetor[com1] <= vetor[com2])
  24. {
  25. vetAux[comAux] = vetor[com1];
  26. com1++;
  27. }
  28. else
  29. {
  30. vetAux[comAux] = vetor[com2];
  31. com2++;
  32. }
  33. comAux++;
  34. }
  35. while(com1<=meio) //Caso ainda haja elementos na primeira metade
  36. {
  37. vetAux[comAux] = vetor[com1];
  38. comAux++;
  39. com1++;
  40. }
  41. while(com2<=fim) //Caso ainda haja elementos na segunda metade
  42. {
  43. vetAux[comAux] = vetor[com2];
  44. comAux++;
  45. com2++;
  46. }
  47. for(comAux=comeco; comAux<=fim; comAux++) //Move os elementos de volta para o vetor original
  48. {
  49. vetor[comAux] = vetAux[comAux-comeco];
  50. }
  51. }
  52. void mergeSort(int vetor[], int comeco, int fim)
  53. {
  54. if (comeco < fim)
  55. {
  56. int meio = (comeco+fim)/2;
  57. mergeSort(vetor, comeco, meio);
  58. mergeSort(vetor, meio+1, fim);
  59. merge(vetor, comeco, meio, fim);
  60. }
  61. }
  62. void maxHeapfy(int vetor[],int lenght,int x){
  63. int maior = x;
  64. int aux;
  65. if(x*2 <= lenght && vetor[x*2]>vetor[maior])maior = x*2;
  66. if(x*2+1 <= lenght && vetor[x*2+1]>vetor[maior])maior = x*2+1;
  67. if(maior != x){
  68. aux = vetor[x];
  69. vetor[x] = vetor[maior];
  70. vetor[maior] = aux;
  71. maxHeapfy(vetor,lenght,maior);
  72. }
  73. }
  74. void constroiHeap(int vetor[],int lenght){
  75. int i;
  76. for(i=lenght/2;i>=0;i--) maxHeapfy(vetor,lenght,i);
  77. }
  78. void heapSort(int vetor[], int lenght){
  79. int i,aux;
  80. constroiHeap(vetor,lenght);
  81. for(i=lenght;i>0;i--){
  82. aux = vetor[0];
  83. vetor[0] = vetor[i];
  84. vetor[i] = aux;
  85. maxHeapfy(vetor,i-1,0);
  86. }
  87. }
  88. void quickSort(int vetor[], int inicio, int fim)
  89. {
  90.  
  91. int pivo, aux, i, j, meio;
  92.  
  93. i = inicio;
  94. j = fim;
  95.  
  96. meio = (int) ((i + j) / 2);
  97. pivo = vetor[meio];
  98.  
  99. do
  100. {
  101. while (vetor[i] < pivo) i = i + 1;
  102. while (vetor[j] > pivo) j = j - 1;
  103.  
  104. if(i <= j)
  105. {
  106. aux = vetor[i];
  107. vetor[i] = vetor[j];
  108. vetor[j] = aux;
  109. i = i + 1;
  110. j = j - 1;
  111. }
  112. }
  113. while(j > i);
  114.  
  115. if(inicio < j) quickSort(vetor, inicio, j);
  116. if(i < fim) quickSort(vetor, i, fim);
  117.  
  118. }
  119. countSort(int vetor[],int length,int range)
  120. {
  121. int *count,i;
  122. int *aux;
  123. aux = (int*)malloc(length*sizeof(int));
  124. count = (int*)malloc(range*sizeof(int));
  125. for(i=0; i<range; i++)count[i]=0;
  126. for(i=0; i<length; i++)count[vetor[i]]++;
  127. for(i=1; i<range; i++)count[i] +=count[i-1];
  128. for(i=0; i<length; i++)
  129. {
  130. aux[count[vetor[i]]-1] = vetor[i];
  131. count[vetor[i]]--;
  132. }
  133. for(i=0; i<length; i++)
  134. {
  135. vetor[i] = aux[i];
  136. }
  137. free(count);
  138. free(aux);
  139. }
  140. void radixSort(int vetor[], int tamanho)
  141. {
  142. int i;
  143. int *VetOrdenado;
  144. int maior = vetor[0];
  145. int exp = 1;
  146.  
  147. VetOrdenado = (int *)calloc(tamanho, sizeof(int));
  148.  
  149. for (i = 0; i < tamanho; i++)
  150. {
  151. if (vetor[i] > maior)
  152. maior = vetor[i];
  153. }
  154.  
  155. while (maior/exp > 0)
  156. {
  157. int aux[10] = { 0 };
  158. for (i = 0; i < tamanho; i++)
  159. aux[(vetor[i] / exp) % 10]++;
  160. for (i = 1; i < 10; i++)
  161. aux[i] += aux[i - 1];
  162. for (i = tamanho - 1; i >= 0; i--)
  163. VetOrdenado[--aux[(vetor[i] / exp) % 10]] = vetor[i];
  164. for (i = 0; i < tamanho; i++)
  165. vetor[i] = VetOrdenado[i];
  166. exp *= 10;
  167. }
  168.  
  169. free(VetOrdenado);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement