Guest User

Untitled

a guest
Apr 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include "quicksort.h"
  2.  
  3.  
  4.  
  5. void particao(int esq, int dir, int *i, int *j, int a[])
  6.  
  7. {
  8.  
  9. int pivo, temp;
  10.  
  11.  
  12.  
  13. *i = esq;
  14.  
  15. *j = dir;
  16.  
  17. pivo = a[*i];
  18.  
  19.  
  20.  
  21. do {
  22.  
  23. while (pivo > a[*i])
  24.  
  25. (*i)++;
  26.  
  27.  
  28.  
  29. while (pivo < a[*j])
  30.  
  31. (*j)--;
  32.  
  33.  
  34.  
  35. if (*i <= *j) {
  36.  
  37. temp = a[*i];
  38.  
  39. a[*i] = a[*j];
  40.  
  41. a[*j] = temp;
  42.  
  43.  
  44.  
  45. (*i)++;
  46.  
  47. (*j)--;
  48.  
  49. }
  50.  
  51. } while (*i <= *j);
  52.  
  53. }
  54.  
  55.  
  56.  
  57. void ordena(int esq, int dir, int a[])
  58.  
  59. {
  60.  
  61. int i, j;
  62.  
  63.  
  64.  
  65. particao(esq, dir, &i, &j, a);
  66.  
  67.  
  68.  
  69. if (esq < j)
  70.  
  71. ordena(esq, j, a);
  72.  
  73.  
  74.  
  75. if (i < dir)
  76.  
  77. ordena(i, dir, a);
  78.  
  79. }
  80.  
  81.  
  82.  
  83. void quickSort(int a[], int *n)
  84.  
  85. {
  86.  
  87. ordena(0, (*n)-1, a);
  88.  
  89. }
  90.  
  91.  
  92.  
  93. void particaoLista(int esq, Ponteiro *esquerda, Ponteiro *esquerdaOriginal, int dir, Ponteiro *direita, Ponteiro *direitaOriginal, int *i, int *j, Lista *a)
  94.  
  95. {
  96.  
  97. int pivo;
  98.  
  99. Ponteiro x, w;
  100.  
  101.  
  102.  
  103. *i = esq;
  104.  
  105. *j = dir;
  106.  
  107. pivo = retornaChave(*esquerda);
  108.  
  109.  
  110.  
  111. do {
  112.  
  113. while (pivo > retornaChave(*esquerda)) {
  114.  
  115. *esquerda = retornaProx(*esquerda);
  116.  
  117. (*i)++;
  118.  
  119. }
  120.  
  121.  
  122.  
  123. while (pivo < retornaChave(*direita)) {
  124.  
  125. *direita = retornaAnt(*direita);
  126.  
  127. (*j)--;
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. if (*i <= *j) {
  136.  
  137.  
  138.  
  139. if((*esquerdaOriginal == *esquerda) && (*esquerdaOriginal != *direita)){
  140.  
  141. *esquerdaOriginal = *direita;
  142.  
  143. }
  144.  
  145.  
  146.  
  147. if((*direitaOriginal == *direita) && (*direitaOriginal != *esquerda)){
  148.  
  149. *direitaOriginal = *esquerda;
  150.  
  151. }
  152.  
  153.  
  154.  
  155. if(retornaProx(*esquerda) != *direita){
  156.  
  157. x = retornaProx(*esquerda); w = retornaAnt(*direita);
  158.  
  159. }else{
  160.  
  161. x = (*esquerda); w = (*direita);
  162.  
  163. }
  164.  
  165.  
  166.  
  167. trocaPosicao(*esquerda, *direita, a);
  168.  
  169. /*
  170.  
  171. Ponteiro aux = *esquerda;
  172.  
  173. *esquerda = *direita;
  174.  
  175. *direita = aux;
  176.  
  177. */
  178.  
  179. *esquerda = x; (*i)++;
  180.  
  181. *direita = w; (*j)--;
  182.  
  183. }
  184.  
  185. } while (*i <= *j);
  186.  
  187. }
  188.  
  189.  
  190.  
  191. void ordenaLista(int esq, Ponteiro esquerda, int dir, Ponteiro direita, Lista *a)
  192.  
  193. {
  194.  
  195. int i, j;
  196.  
  197. Ponteiro iCel = esquerda;
  198.  
  199. Ponteiro jCel = direita;
  200.  
  201.  
  202.  
  203. particaoLista(esq, &iCel, &esquerda, dir, &jCel, &direita, &i, &j, a);
  204.  
  205.  
  206.  
  207. if (esq < j)
  208.  
  209. ordenaLista(esq, esquerda, j, jCel, a);
  210.  
  211.  
  212.  
  213. if (i < dir)
  214.  
  215. ordenaLista(i, iCel, dir, direita, a);
  216.  
  217. }
  218.  
  219.  
  220.  
  221. void quickSortLista(Lista *a)
  222.  
  223. {
  224.  
  225. Ponteiro esquerda = a->primeiro->prox;
  226.  
  227. Ponteiro direita = a->ultimo;
  228.  
  229.  
  230.  
  231. ordenaLista(0, esquerda, a->numElementos-1, direita, a);
  232.  
  233. }
  234.  
  235.  
  236.  
  237. void particaoRegistro(int esq, int dir, int *i, int *j, Registro r[])
  238.  
  239. {
  240.  
  241. int pivo;
  242.  
  243. Registro temp;
  244.  
  245.  
  246.  
  247. *i = esq;
  248.  
  249. *j = dir;
  250.  
  251. pivo = retornaChaveReg(&r[*i]);
  252.  
  253.  
  254.  
  255. do {
  256.  
  257. while (pivo > retornaChaveReg(&r[*i]))
  258.  
  259. (*i)++;
  260.  
  261.  
  262.  
  263. while (pivo < retornaChaveReg(&r[*j]))
  264.  
  265. (*j)--;
  266.  
  267.  
  268.  
  269. if (*i <= *j) {
  270.  
  271. temp = r[*i];
  272.  
  273. r[*i] = r[*j];
  274.  
  275. r[*j] = temp;
  276.  
  277.  
  278.  
  279. (*i)++;
  280.  
  281. (*j)--;
  282.  
  283. }
  284.  
  285. } while (*i <= *j);
  286.  
  287. }
  288.  
  289.  
  290.  
  291. void ordenaRegistro(int esq, int dir, Registro r[])
  292.  
  293. {
  294.  
  295. int i, j;
  296.  
  297.  
  298.  
  299. particaoRegistro(esq, dir, &i, &j, r);
  300.  
  301.  
  302.  
  303. if (esq < j)
  304.  
  305. ordenaRegistro(esq, j, r);
  306.  
  307.  
  308.  
  309. if (i < dir)
  310.  
  311. ordenaRegistro(i, dir, r);
  312.  
  313. }
  314.  
  315.  
  316.  
  317. void quickSortRegistro(Registro r[], int *n)
  318.  
  319. {
  320.  
  321. ordenaRegistro(0, (*n)-1, r);
  322.  
  323. }
Add Comment
Please, Sign In to add comment