bubble sort c programize code example

Example: bubble sort in c

#include <stdio.h>
#include <stdbool.h>


int main()
{
    int arr[] = { 12, 11, 13, 5, 6 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);
    int i, j;
	bool isSwapped = false;
    
    for (i = 0; i < arr_size; i++) {
        for (j = 0; j < arr_size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swapping elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                
                isSwapped = true;
            }
        }
    }

    printf("Array After Sorted:\n");
    for (i = 0; i < arr_size; i++)
        printf("%d ", arr[i]);
    return 0;
}

Tags:

Misc Example