Advertisement
rebsoo

Lista3 // Quicksort

Apr 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. void quicksort(int ordem[], int left, int right);
  9.  
  10. int main() {
  11.     int contador, tempo, tam, um;
  12.     tempo = 0;
  13.     contador = 0;
  14.  
  15.     scanf("%d", &contador);
  16.  
  17.     while (tempo < contador) {
  18.         um = 0;
  19.         bool b = false;
  20.         if ((tempo > 0) && (tempo < contador)) {
  21.             printf("\n");
  22.         }
  23.         scanf("%d", &tam);
  24.         int ordem [tam];
  25.         for (int i = 0; i < tam; i++) {
  26.                  scanf("%d", &ordem[i]);
  27.              }
  28.  
  29.         quicksort(ordem, 0,(tam - 1) );
  30.  
  31.              if (ordem[tam - 1] <= 3) {
  32.                  for (int z = 0; z < tam; z++) {
  33.                      if (z == 0) {
  34.                          printf("%d",ordem[z]);
  35.                      }
  36.                      else {
  37.                          printf(" %d", ordem[z]);
  38.                      }
  39.                  }
  40.              }
  41.              else {
  42.                  for (int z = 0; z < tam; z++) {
  43.                      if (ordem[z] == 1) {
  44.                          printf("%d ", ordem[z]);
  45.                          b = true;
  46.                      }
  47.                      else {
  48.                          um = z;
  49.                          z = tam;
  50.                      }
  51.                  }
  52.                  if (b) {
  53.                      for (int z = tam - 1; z >= um; z--) {
  54.                        if (z == um) {
  55.                          printf("%d", ordem[z]);
  56.                        }
  57.                        else {
  58.                          printf("%d ", ordem[z]);
  59.                        }
  60.                      }
  61.                      
  62.                  }
  63.                  else {
  64.                      for (int z = tam - 1; z >= 0; z--) {
  65.                        if (z == 0) {
  66.                          printf("%d", ordem[z]);
  67.                        }
  68.                        else {
  69.                          printf("%d ", ordem[z]);
  70.                        }
  71.                      }
  72.                  }
  73.                  
  74.              }
  75.              tempo++;
  76.     }
  77.    
  78.     return 0;
  79.  
  80. }
  81.  
  82. void quicksort (int ordem[], int left, int right) {
  83.     int m = left;
  84.     int n = right;
  85.     int pivot = (ordem[(left + right)/2]);
  86.     int temp;
  87.  
  88.     //caso base
  89.     while (m <= n) {
  90.  
  91.             while (ordem[m] < pivot) {
  92.                 m++;
  93.             }
  94.  
  95.             while (ordem[n] > pivot) {
  96.                 n--;
  97.             }
  98.             //particao
  99.             //caso base
  100.             if (m <= n) {
  101.                 temp = ordem[m];
  102.                 ordem[m] = ordem[n];
  103.                 ordem[n] = temp;
  104.                 m++;
  105.                 n--;
  106.             }
  107.         }
  108.  
  109.         //recursao
  110.         if (left < n) {
  111.             quicksort(ordem, left, n);
  112.         }
  113.  
  114.         if (m < right) {
  115.             quicksort(ordem, m, right);
  116.         }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement