for i = 1 to n do
for j = 1 to n do
if A[i] < A[j] then
swap A[i] and A[j] for i = 1 to n do
for j = 1 to n do
if A[i] < A[j] then
swap A[i] and A[j]It behaves more like insertion sort.
First of all, there's a Start button at the bottom of the right menu. Hit that and you will see that big color gradient sort itself.
However, that massive square is useless for seeing what's going on. So instead change the size to its lowest (8) and the delay to something like 400.
Now what you've got is 8 randomly-sorted columns. When you hit Start that algorithm will start sorting them each in parallel. You can just look at a single column and see the colors getting swapped. The darkest color moves to the top very quickly. This is a feature of how this particular algorithm works.
What's the point of the big square? Well, for one thing it's an interesting way to compare algorithms. Go back to the settings for a large number, a very low delay, and check off two other sorting algorithms, say Comb Sort and Heap sort. Hit Start and you'll see three very different approaches to sorting the columns.
for i = 1 to n do
- Inserts A[i] into A[1 to i-1], using the A[i]
- spot as a temp variable.
for j = 1 to i-1 do
if A[i] < A[j] then
swap A[i] and A[j]
- Does nothing.
for j = i do
if A[i] < A[j] then
swap A[i] and A[j]
- Permutes A[i to n], placing the maximum into
- A[i]. Since A[i] already has the max after the
- first iteration, it won't change, and we don't
- care how A[i+1 to n] is permuted since it gets
- sorted anyway.
for j = i+1 to n do
if A[i] < A[j] then
swap A[i] and A[j] - quick sort
- merge sort
- custom sort (this naive algorithm)
- bubble sort
Then click “start”.And then you can clearly see the difference side by side, including the speed of the sort!
Quick sort is quick! Merge sort is better in the worst case but slower on this random data.
So if you visualize two sorts side by side, it's obvious which ones completes faster, and that corresponds to the algorithm's speed, right?
https://xosh.org/VisualizingSorts/sorting.html#IYZwngdgxgBAZ...
You can change the detail level slider while its running and see how it changes the animation. At level 1 (higher level) this and insertion are same.
* though, edflsafoiewq's analysis is nice and tidy
But apparently some people treated pixels as geometrical points without size?
If you don't know, you don't know.