fork download
  1. #include <iostream>
  2.  
  3. void bubble_sort(int number_of_elements, int array[]);
  4.  
  5. void bubble_sort(int number_of_elements, int array[]) {
  6. int flag = 1;
  7. for (int i; i < number_of_elements && flag; i++) {
  8. flag = 0;
  9. for (int j = 0; j < (number_of_elements - 1); j++) {
  10. if (array[j + 1] < array[j]) {
  11. int temp = array[j];
  12. array[j] = array[j + 1];
  13. array[j + 1] = temp;
  14. flag = 1;
  15. }
  16. }
  17. }
  18. for (int counter = 0; counter < number_of_elements; counter++) {
  19. std::cout << array[counter] << std::endl;
  20. }
  21. }
  22.  
  23. int main() {
  24. const int number_of_elements = 5;
  25. int array[number_of_elements] = {7, 2, 4, 3, 5};
  26. bubble_sort(number_of_elements, array);
  27. return 0;
  28. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
2
3
4
5
7