fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void bubbleSort(int arr[], int n) {
  5. for (int i = 0; i < n - 1; i++) {
  6. for (int j = 0; j < n - i - 1; j++) {
  7. if (arr[j] > arr[j + 1])
  8. swap(arr[j], arr[j + 1]);
  9. }
  10. }
  11. }
  12.  
  13. int main() {
  14. int arr[] = {5, 1, 4, 2, 8};
  15. int n = sizeof(arr) / sizeof(arr[0]);
  16.  
  17. bubbleSort(arr, n);
  18.  
  19. for (int i = 0; i < n; i++)
  20. cout << arr[i] << " ";
  21. cout << endl;
  22.  
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
1 2 4 5 8