Advertisement
homer512

trivial sort

May 27th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2.  
  3.  
  4. int main(){
  5.   std::cout << "Enter 3 numbers:" << std::endl;
  6.   int array[3];
  7.   const int n = 3;
  8.   for(int i = 0; i < n; i++) {
  9.     std::cin >> array[i];
  10.   }
  11.   /* basic bubble sort for educational purposes
  12.    * Replace with std::sort for production code
  13.    */
  14.   for(int i = n; i > 0; i--) {
  15.     for(int j = 0; j < i - 1; j++) {
  16.       if(array[j] > array[j+1]) {
  17.     /* Replace with std::swap for production code */
  18.     int hold = array[j];
  19.     array[j] = array[j+1];
  20.     array[j+1] = hold;
  21.       }
  22.     }
  23.   }
  24.   std::cout << "Ascending Array is:\n";
  25.   for(int i = 0; i < 3; i++) {
  26.     std::cout << array[i] << "\n";
  27.   }
  28.   std::cout << "Descending Array is:\n";
  29.   for(int i = n - 1; i >= 0; i--) {
  30.     std::cout << array[i] << "\n";
  31.   }
  32.   /* just for good measure */
  33.   std::cout.flush();
  34.   return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement