typestar

Bubble sort in C

The sort nobody should use and everybody should have written once.

void bubble_sort(int *items, int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            if (items[j] > items[j + 1]) {
                int tmp = items[j];
                items[j] = items[j + 1];
                items[j + 1] = tmp;
            }
        }
    }
}

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
213
Tokens
97
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 61 seconds.

Type this snippet

Step 1 of 5 in Sorting & searching, step 1 of 20 in Data structures & algorithms.

Next →