Advertisement
Guest User

Untitled

a guest
May 25th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. /*BUBBLE SORT PARA PDIS E LOCAIS (POR ORDEM ALFABETICA E POR N INSCRITOS)*/
  2. void ordena_pdis(lista_locais auxLocais,int flag){
  3.     lista_locais l = auxLocais;
  4.     while(l){
  5.         lista_pdi d = l->lista_pdi_local;
  6.         int n_nos=0;
  7.         n_nos = nNosPdi(d);
  8.         if(flag==0){
  9.             bubbleSortPdi(&d,n_nos,0);
  10.         }else{
  11.             bubbleSortPdi(&d,n_nos,1);
  12.         }
  13.         l=l->next;
  14.  
  15.     }
  16. }
  17.  
  18. lista_locais trocaNos(lista_locais ptr1, lista_locais ptr2)
  19. {
  20.     lista_locais tmp = ptr2->next;
  21.     ptr2->next = ptr1;
  22.     ptr1->next = tmp;
  23.     return ptr2;
  24. }
  25.  
  26. lista_pdi trocaNosPdi(lista_pdi ptr1, lista_pdi ptr2)
  27. {
  28.     lista_pdi tmp = ptr2->next;
  29.     ptr2->next = ptr1;
  30.     ptr1->next = tmp;
  31.     return ptr2;
  32. }
  33.  
  34. int nNos(lista_locais head)
  35. {
  36.     int count = 0;
  37.     lista_locais atual = head;
  38.     while (atual != NULL)
  39.     {
  40.         count++;
  41.         atual = atual->next;
  42.     }
  43.     return count;
  44. }
  45.  
  46. int nNosPdi(lista_pdi head)
  47. {
  48.     int count = 0;
  49.     lista_pdi atual = head;
  50.     while (atual != NULL)
  51.     {
  52.         count++;
  53.         atual = atual->next;
  54.     }
  55.     return count;
  56. }
  57.  
  58.  
  59. void bubbleSort(lista_locais * head, int count,int flag)
  60. {
  61.     lista_locais* h;
  62.     int i, j, troca;
  63.  
  64.     for (i = 0; i <= count; i++) {
  65.  
  66.         h = head;
  67.         troca = 0;
  68.  
  69.         for (j = 0; j < count - i - 1; j++) {
  70.  
  71.             lista_locais p1 = *h;
  72.             lista_locais p2 = p1->next;
  73.             if(flag==0){
  74.                 if (p1->n_inscritos_local > p2->n_inscritos_local) {
  75.  
  76.                     *h = trocaNos(p1, p2);
  77.                     troca = 1;
  78.                 }
  79.             }else{
  80.                 if (strcmp(p1->nome,p2->nome)>0){
  81.                     *h = trocaNos(p1, p2);
  82.                     troca = 1;
  83.                 }
  84.             }
  85.            
  86.  
  87.             h = &(*h)->next;
  88.         }
  89.  
  90.         if (troca == 0)
  91.             break;
  92.     }
  93. }
  94.  
  95. void bubbleSortPdi(lista_pdi * head, int count,int flag)
  96. {
  97.     lista_pdi* h;
  98.     int i, j, troca;
  99.  
  100.     for (i = 0; i <= count; i++) {
  101.  
  102.         h = head;
  103.         troca = 0;
  104.  
  105.         for (j = 0; j < count - i - 1; j++) {
  106.  
  107.             lista_pdi p1 = *h;
  108.             lista_pdi p2 = p1->next;
  109.             if(flag==0){
  110.                 if (p1->n_inscritos_pdi > p2->n_inscritos_pdi) {
  111.  
  112.                     *h = trocaNosPdi(p1, p2);
  113.                     troca = 1;
  114.                 }
  115.             }else{
  116.                 if (strcmp(p1->nome,p2->nome)>0) {
  117.  
  118.                 *h = trocaNosPdi(p1, p2);
  119.                 troca = 1;
  120.                 }
  121.             }  
  122.                
  123.  
  124.             h = &(*h)->next;
  125.         }
  126.  
  127.         if (troca == 0)
  128.             break;
  129.     }
  130. }
  131.  
  132. /*FIM DAS FUNCOES PARA O SORT*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement