fork download
  1. #include <stdio.h>
  2.  
  3. void swap(int *xp, int *yp)
  4. {
  5. int temp = *xp;
  6. *xp = *yp;
  7. *yp = temp;
  8. }
  9.  
  10. // A function to implement bubble sort
  11. void bubbleSort(int arr[], int n)
  12. {
  13. int i, j;
  14. for (i = 0; i < n-1; i++)
  15.  
  16. // Last i elements are already in place
  17. for (j = 0; j < n-i-1; j++)
  18. if (arr[j] > arr[j+1])
  19. swap(&arr[j], &arr[j+1]);
  20. }
  21.  
  22. /* Function to print an array */
  23. void printArray(int arr[], int size)
  24. {
  25. for (int i=0; i < size; i++)
  26. printf("%d ", arr[i]);
  27. printf("\n");
  28. }
  29.  
  30. // Driver program to test above functions
  31. int main()
  32. {
  33. int arr[] = {6, 4, 29, 32, 22, 54, 104};
  34. int n = sizeof(arr)/sizeof(arr[0]);
  35. bubbleSort(arr, n);
  36. printf("Sorted array: \n");
  37. printArray(arr, n);
  38. return 0;
  39. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Sorted array: 
4 6 22 29 32 54 104