bubble sort c

Bubble Sort in C

Here is an example of the bubble sort algorithm implemented in C:

#include <stdio.h>

void bubbleSort(int arr[], int n) {
    int i, j;
    for (i = 0; i < n-1; i++) {
        for (j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("Sorted array: \n");
    for (int i=0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Explanation of the Bubble Sort Algorithm

The bubble sort algorithm is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

Here is a step-by-step explanation of the bubble sort algorithm:

  1. Start with an unsorted array of elements.
  2. Compare the first element with the second element. If the first element is greater than the second element, swap them.
  3. Move to the next pair of elements and repeat step 2. Continue this process until the end of the array.
  4. At this point, the largest element will be at the end of the array.
  5. Repeat steps 2-4 for the remaining elements, excluding the last element that is already in its correct position.
  6. Continue this process until the entire array is sorted.

Let's apply this algorithm to the example array [64, 34, 25, 12, 22, 11, 90]:

  1. Pass 1:
  2. Compare 64 and 34. Swap them. The array becomes [34, 64, 25, 12, 22, 11, 90].
  3. Compare 64 and 25. Swap them. The array becomes [34, 25, 64, 12, 22, 11, 90].
  4. Compare 64 and 12. Swap them. The array becomes [34, 25, 12, 64, 22, 11, 90].
  5. Compare 64 and 22. Swap them. The array becomes [34, 25, 12, 22, 64, 11, 90].
  6. Compare 64 and 11. Swap them. The array becomes [34, 25, 12, 22, 11, 64, 90].
  7. Compare 64 and 90. No swap is needed. The array remains [34, 25, 12, 22, 11, 64, 90].
  8. Pass 2:
  9. Compare 34 and 25. Swap them. The array becomes [25, 34, 12, 22, 11, 64, 90].
  10. Compare 34 and 12. Swap them. The array becomes [25, 12, 34, 22, 11, 64, 90].
  11. Compare 34 and 22. Swap them. The array becomes [25, 12, 22, 34, 11, 64, 90].
  12. Compare 34 and 11. Swap them. The array becomes [25, 12, 22, 11, 34, 64, 90].
  13. Compare 34 and 64. No swap is needed. The array remains [25, 12, 22, 11, 34, 64, 90].
  14. Pass 3:
  15. Compare 25 and 12. Swap them. The array becomes [12, 25, 22, 11, 34, 64, 90].
  16. Compare 25 and 22. Swap them. The array becomes [12, 22, 25, 11, 34, 64, 90].
  17. Compare 25 and 11. Swap them. The array becomes [12, 22, 11, 25, 34, 64, 90].
  18. Compare 25 and 34. No swap is needed. The array remains [12, 22, 11, 25, 34, 64, 90].
  19. Pass 4:
  20. Compare 12 and 22. No swap is needed. The array remains [12, 22, 11, 25, 34, 64, 90].
  21. Compare 22 and 11. Swap them. The array becomes [12, 11, 22, 25, 34, 64, 90].
  22. Compare 22 and 25. No swap is needed. The array remains [12, 11, 22, 25, 34, 64, 90].
  23. Pass 5:
  24. Compare 12 and 11. Swap them. The array becomes [11, 12, 22, 25, 34, 64, 90].
  25. Compare 12 and 22. No swap is needed. The array remains [11, 12, 22, 25, 34, 64, 90].
  26. Pass 6:
  27. Compare 11 and 12. No swap is needed. The array remains [11, 12, 22, 25, 34, 64, 90].
  28. The array is now sorted: [11, 12, 22, 25, 34, 64, 90].

This is how the bubble sort algorithm works. It repeatedly compares adjacent elements and swaps them if they are in the wrong order until the entire array is sorted.