fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. void swap(int *xp, int *yp)
  6. {
  7.  
  8. int temp = *xp;
  9.  
  10. *xp = *yp;
  11.  
  12. *yp = temp;
  13. }
  14.  
  15.  
  16. // A function to implement bubble sort
  17.  
  18. void bubbleSort(int arr[], int n)
  19. {
  20.  
  21. int i, j;
  22.  
  23. for (i = 0; i < n-1; i++)
  24.  
  25.  
  26.  
  27. // Last i elements are already in place
  28.  
  29. for (j = 0; j < n-i-1; j++)
  30.  
  31. if (arr[j] > arr[j+1])
  32.  
  33. swap(&arr[j], &arr[j+1]);
  34. }
  35.  
  36.  
  37. /* Function to print an array */
  38.  
  39. void printArray(int arr[], int size)
  40. {
  41.  
  42. int i;
  43.  
  44. for (i = 0; i < size; i++)
  45.  
  46. cout << arr[i] << " ";
  47.  
  48. cout << endl;
  49. }
  50.  
  51. int main() {
  52. // 1 9 2 3 4 1 6 6 5 3 8
  53. int arr[] = {1,9,2,3,4,1,6,6,5,3,8};
  54.  
  55. int n = sizeof(arr)/sizeof(arr[0]);
  56.  
  57. bubbleSort(arr, n);
  58.  
  59. cout<<"Sorted array: \n";
  60.  
  61. printArray(arr, n);
  62.  
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 4368KB
stdin
Standard input is empty
stdout
Sorted array: 
1 1 2 3 3 4 5 6 6 8 9