avr39-ripe

sortBuble

May 27th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #define OPTIMAL
  4.  
  5. using namespace std;
  6. int main()
  7. {
  8.     const int arrSize = 10;
  9.     int arr[arrSize] = { 6,1,4,2,8,9,11,3,2,1 };
  10.     //int arr[arrSize] = { 1,1,1,2,2,9,11,1,2,1 };
  11.     //int arr[arrSize] = { 1,2,3,4,5,6,7,8,9,10 };
  12.     //int arr[arrSize] = { 1,2,3,4,5,6,7,9,8,7 };
  13.     //int arr[arrSize] = { 10,9,8,7,6,5,4,3,2,1 };
  14.     //int arr[arrSize] = {0};
  15.     int minEl = arr[0];
  16.     int minIdx = -1;
  17.  
  18.     for (int i = 0; i < arrSize; i++) { cout << arr[i] << " "; }    cout << endl;
  19.  
  20.     for (int head = 0; head < arrSize; head++)
  21.     {
  22.         for (int tail = arrSize - 1; tail > head; tail--)
  23.         {
  24. #ifndef OPTIMAL
  25.             if (arr[tail]<arr[tail-1])
  26. #else
  27.             if (arr[tail] < arr[head])
  28. #endif
  29.             {
  30.                 int tmp = arr[tail];
  31. #ifndef OPTIMAL
  32.                 arr[tail]=arr[tail-1];
  33.                 arr[tail - 1] = tmp;
  34. #else
  35.                 arr[tail] = arr[head];
  36.                 arr[head] = tmp;
  37. #endif
  38.  
  39.                 for (int i = 0; i < arrSize; i++) { cout << arr[i] << " "; } cout<< " head:" << head << endl;
  40.             }
  41.         }
  42.  
  43.     }
  44.  
  45.     for (int i = 0; i < arrSize; i++) { cout << arr[i] << " "; }    cout << endl;
  46. }
Add Comment
Please, Sign In to add comment