Insertion vs. Selection Sort In 30 Seconds

preview_player
Показать описание
#shorts Insertion and Selection sort are both used to sort 400 bars of random lengths. These are both N^2 sorts, so in theory they should take about the same time. In this case, the insertion sort takes slightly fewer operations.

Green shows bars in the correct spot. And red shows bars that are being moved.

Insertion sort works by grabbing the next item, and putting it in the right spot out of what has already been sorted. That requires moving everything else up a space, which is why there is a big block of moving red bars.

Selection sort works by finding the smallest value out of the bars that haven't already been placed That is why it has a growing list of green bars.

This was made with Python & Pygame

Thumbnail image by Gerd Altmann from Pixabay
Рекомендации по теме
Комментарии
Автор

Thanks! I was looking for exactly that.

ricopin