Advertisement
harrisonmk

selectionSort passo a passo

Mar 13th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void selecao (int vetor[], int n);
  6. void linha ();
  7.  
  8. int main(int argc, char** argv)
  9. {
  10.  
  11.  
  12.     //int j;
  13.     int n = 5;
  14.  
  15.  
  16.     int vetor[5] = {20, 35, 15, 12, 25};
  17.  
  18.     cout << "Vetor Desordenado" << endl;
  19.  
  20.     for(int i = 0; i < n; i++)
  21.     {
  22.         cout << vetor[i] << " ";
  23.     }
  24.     cout << endl << endl;
  25.  
  26.     selecao (vetor, n);
  27.  
  28.     cout << "Ordenacao selection sort" << endl;
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.  
  32.         cout << vetor[i] << " ";
  33.  
  34.  
  35.     }
  36.     cout << endl;
  37.  
  38.  
  39.     return 0;
  40. }
  41.  
  42. void selecao (int vetor[], int n)
  43. {
  44.  
  45.  
  46.     int menor, aux;
  47.  
  48.     for(int i = 0; i < n - 1; i++)
  49.     {
  50.         menor = i;
  51.         for(int j = i + 1; j < n; j++)
  52.         {
  53.             if(vetor[menor] > vetor[j])
  54.             {
  55.                 cout << " testa " << vetor[menor] << " > " << vetor[j] << " True" << endl;
  56.                 cout << " Troca " << vetor[menor] << " e " << vetor[j] << endl;
  57.                
  58.                 for(int i = 0; i < n; i++)
  59.                 {
  60.                     cout << " " << vetor[i];
  61.                 }
  62.  
  63.                 menor = j;
  64.  
  65.                
  66.  
  67.                 cout << endl;
  68.                 cout << endl;
  69.                 linha ();
  70.  
  71.             }
  72.             else if(vetor[menor] < vetor[j])
  73.             {
  74.  
  75.                 cout << " testa " << vetor[menor] << " > " << vetor[j] << " False" << endl;
  76.             }
  77.  
  78.         }
  79.         if (i != menor)
  80.         {
  81.  
  82.  
  83.             aux = vetor[i];
  84.             vetor[i] = vetor[menor];
  85.             vetor[menor] = aux;
  86.  
  87.  
  88.  
  89.         }
  90.  
  91.     }
  92.  
  93.  
  94.  
  95.  
  96. }
  97.  
  98. void linha ()
  99. {
  100.  
  101.     cout << "--------------------------------------------" << endl;
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement