C language bubble sort implementation source code and demo animation

C language bubble sort implementation source code and demo animation

#include <stdio.h>

void bubble_sort(int arr[], int len) {
    int i, j, tmp;
    for (i = 0; i < len - 1; i++) {
        for (j = 0; j < len - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
            }
        }
    }
}
int main() {
    int i;
    int arr[] = { 2, 4, 3, 32, 8, 5, 9, 50, 7, 5, 64, 35, 9, 70 };
    int len = (int) sizeof(arr) / sizeof(*arr);
    bubble_sort(arr, len);

    for (i = 0; i < len; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Animation of bubble sort demonstration



Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example