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 - 1; j++)
  7. if (arr[j] > arr[j + 1])
  8. swap(arr[j], arr[j + 1]);
  9. }
  10.  
  11. void printArray(int arr[], int size)
  12. {
  13. for (int i = 0; i < size; i++)
  14. cout << arr[i] << " ";
  15. cout << endl;
  16. }
  17.  
  18. int main()
  19. {
  20. int arr[] = { 5, 2, 4, 6, 1, 3};
  21. int size = sizeof(arr) / sizeof(arr[0]);
  22. bubbleSort(arr, size);
  23. cout << "Sorted array: ";
  24. printArray(arr, size);
  25. return 0;
  26. }
Success #stdin #stdout 0s 5288KB
stdin
5,4,1,2,8
stdout
Sorted array: 1 2 3 4 5 6