I can’t believe that I can prove that it can sort
blog.adacore.com
I can’t believe that I can prove that it can sort
1–10 of 124 posts
Re: I can’t believe that I can prove that it can sort
#2After the first main loop, the first item will be the biggest item in the list.
The inner loop, as it always starts from 1 again will gradually replace the bigger items with smaller items, and so on.
Re: I can’t believe that I can prove that it can sort
#3EDIT
^W^W^W
Nope, I'm wrong. I did a worked example on paper. I think the devious thing here is the algorithm looks simple at a lazy glance.
To sort: 4-3-6-9-1, next round pivot for the i loop in [].
[4]-3-6-9-1
9-[3]-4-6-1
3-9-[4]-6-1
3-4-9-[6]-1
3-4-6-9-[1]
1-2-3-6-9 & sorted
I can see that it sorts everything to the left of a pivot, then because it does that n times it comes to a sorted list. A reasonable proof will be more complicated than I thought.Re: I can’t believe that I can prove that it can sort
#4If it looks "obviously wrong" the quick proof that it works ... the j loop will move the largest unsorted element in the array into a sorted position. Since the i loop executes the j loop n times, the array must be sorted (since the n largest elements will be in the correct order). EDIT ^W^W^W Nope, I'm wrong. I did a worked example on paper. I think the devious thing here is the algorithm looks simple at a lazy glan…
Re: I can’t believe that I can prove that it can sort
#5If you compare it with both Insertion and Bubble sort. You can see it looks more like insertion sort than bubble sort.
Re: I can’t believe that I can prove that it can sort
#6Re: I can’t believe that I can prove that it can sort
#7Here is how it looks. https://xosh.org/VisualizingSorts/sorting.html#IYZwngdgxgBAZ... If you compare it with both Insertion and Bubble sort. You can see it looks more like insertion sort than bubble sort.
I think it is very possible to write this algorithm by mistake in intro compsci classes when you try to code a bubble sort by heart. I would think TAs may have many such instances in their students' homework.
Re: I can’t believe that I can prove that it can sort
#8https://news.ycombinator.com/item?id=28758106 (318 comments)
Re: I can’t believe that I can prove that it can sort
#9While it's been on my list for a while, I'm more curious to try out Ada now.
Re: I can’t believe that I can prove that it can sort
#10Wow, I wish we had these built-in provers in VHDL (which is basically Ada in sheep's clothing).
Only the syntax is similar.