20241108 ◎

My housemate is highly sensitive, and I'm considering moving out. I wish the U.S. housing is more affordable…

I was too hungry. I'm thinking about increasing my calorie intake a bit for a few days.

Counting sort

It's embarassing, but I don't remember much about the counting sort, so I took some time to recall it.

def counting_sort(arr: list[int]):
    mi = min(arr)
    ma = max(arr)
    count: list[int] = [0] * (ma - mi + 1)  # value range
    for x in arr:
        count[x - mi] += 1

    result: list[int] = []
    for i in range(mi, ma + 1):
        for _ in range(count[i - mi]):
            result.append(i + mi)

    return result


print(f"{counting_sort([2, 1, 4, 3, 5])=}")  # [2, 3, 4, 5, 6]
print(f"{counting_sort([-1, -3, -2, 5, 6])=}")  # [-6, -5, -4, 2, 3]

Now, the memory is coming back.

What is the intuitive explanation for counting sort? - Quora

Intuition:

Counting sort works because we limit the range of possible input values. By interating over the range of possible values in sorted order, we ensure that the values we add to our output array are always added in sorted order.

TODO: Study Radix Sort


Protein bars 450 Protein shake 150 Sushi bowl 800 Sausage & pancake 250 Sandwich 150 Boba 400

Total 2200 kcal


MUST:

TODO:


index 20241107 20241109