harrisonmk

Insertion

Mar 14th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. void InsertionSort (int array[], int tam);
  7. void linha ();
  8.  
  9. int main(int argc, char** argv)
  10. {
  11.  
  12.     int n = 5;
  13.     int vetor[] = {20, 35, 15, 12, 25};
  14.  
  15.     cout << "\t Vetor" << endl << endl;
  16.     for(int i = 0; i < n; i++)
  17.     {
  18.         cout << " " << vetor[i] << "  ";
  19.  
  20.     }
  21.     cout << endl << endl;;
  22.  
  23.     InsertionSort (vetor, n);
  24.  
  25.     cout << "Ordenacao Insertion Sort" << endl << endl;
  26.  
  27.     for(int i = 0; i < n; i++)
  28.     {
  29.         cout << vetor[i] << " ";
  30.  
  31.     }
  32.  
  33.     cout << endl;
  34.  
  35.  
  36.     return 0;
  37. }
  38.  
  39.  
  40. void InsertionSort (int array[], int tam)
  41. {
  42.  
  43.     int j, temp;
  44.  
  45.     for(int i = 1; i < tam; i++)
  46.     {
  47.         j = i;
  48.  
  49.         while(j > 0 && array[j - 1] > array[j])
  50.         {
  51.             cout << " testa " << array[j - 1] << " > " << array[j] << " True" << endl;
  52.             cout << " Troca " << array[j - 1] << " e " << array[j] << endl;
  53.  
  54.             temp = array[j];
  55.             array[j] = array[j - 1];
  56.             array[j - 1] = temp;
  57.  
  58.             for(int i = 0; i < tam; i++)
  59.             {
  60.                 cout << " " << array[i];
  61.             }
  62.  
  63.             cout << endl;
  64.             cout << endl;
  65.             linha ();
  66.  
  67.             j--;
  68.  
  69.         }
  70.     }
  71.  
  72. }
  73.  
  74. void linha ()
  75. {
  76.  
  77.     cout << "--------------------------------------------" << endl;
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment