homer512 icon

trivial sort

homer512 | PRO | 05/27/15 06:46:49 PM UTC | 0 ⭐ | 888 👁️ | Never ⏰ | []
C++ |

820 B

|

None

|

0 👍

/

0 👎

#include<iostream>
 
 
int main(){
  std::cout << "Enter 3 numbers:" << std::endl;
  int array[3];
  const int n = 3;
  for(int i = 0; i < n; i++) {
    std::cin >> array[i];
  }
  /* basic bubble sort for educational purposes
   * Replace with std::sort for production code
   */
  for(int i = n; i > 0; i--) {
    for(int j = 0; j < i - 1; j++) {
      if(array[j] > array[j+1]) {
    /* Replace with std::swap for production code */
    int hold = array[j];
    array[j] = array[j+1];
    array[j+1] = hold;
      }
    }
  }
  std::cout << "Ascending Array is:\n";
  for(int i = 0; i < 3; i++) {
    std::cout << array[i] << "\n";
  }
  std::cout << "Descending Array is:\n";
  for(int i = n - 1; i >= 0; i--) {
    std::cout << array[i] << "\n";
  }
  /* just for good measure */
  std::cout.flush();
  return 0;
}

Comments