fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int a[10], i, j, temp;
  6.  
  7. // Input elements
  8. cout << "Enter the elements of the array: ";
  9. for (i = 0; i < 10; i++) {
  10. cin >> a[i];
  11. }
  12.  
  13. // Print original array
  14. cout << "Original array: ";
  15. for (i = 0; i < 10; i++) {
  16. cout << a[i] << " ";
  17. }
  18. cout << endl;
  19.  
  20. // Bubble Sort Algorithm
  21. for (i = 0; i < 10 - 1; i++) {
  22. for (j = 0; j < 10 - i - 1; j++) {
  23. if (a[j] > a[j + 1]) {
  24. // Correct swap
  25. temp = a[j];
  26. a[j] = a[j + 1];
  27. a[j + 1] = temp;
  28. }
  29. }
  30. }
  31.  
  32. // Print sorted array
  33. cout << "Bubble sort in ascending order: ";
  34. for (i = 0; i < 10; i++) {
  35. cout << a[i] << " ";
  36. }
  37. cout << endl;
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter the elements of the array: Original array: 1 0 2 0 -2086481896 32764 -1294724219 21904 -1610097888 5343 
Bubble sort in ascending order: -2086481896 -1610097888 -1294724219 0 0 1 2 5343 21904 32764