News for hackers. Stuff that matters.
Re:Is this the simplest (and most surprising) sorting algori... by svat on Tuesday October 05, 2021 @11:15AM (#28760569)
[Edit: I took a quick glance at the paper and got carried away; the rest of this comment is about a different algorithm apparently known as "exchange sort", and an interesting property of it. The algorithm in the paper is actually different, and indeed more surprising.]

Hey! That's my sorting algorithm! (Well, I'm sure it has been independently discovered many times, but one of those times was by me before I had learned of O(n log n) sorting; I used it many times including in the INOI 2004 programming contest, about which I asked the following question ten years ago in 2011 on math.SE about this algorithm and Catalan numbers, a truly surprising property that is not mentioned in this paper: https://math.stackexchange.com/questions/36022/how-do-the-ca...

> For situations where a quadratic-time sorting algorithm is fast enough, I usually use the following:

    //Given array a[1], ... a[n]
    for i = 1 to n:
        for j = i+1 to n:
            if a[i] > a[j]:
                swap(a[i],a[j])
> It looks like bubble sort, but is closer to selection sort. It is easy to see why it works: in each iteration of the outer loop, `a[i]` is set to be the smallest element of `a[i…n]`.

----

Also: A few years ago I emailed Prof. Richard Stanley of Enumerative Combinatorics fame asking whether it was related to any of the hundreds of problems in his Catalan Numbers list, and he said he didn't know a direct connection at the time (so maybe it's an addition to his list?):

> > permutations equal to the product of all their inversions (in lexicographic order) [multiplying on the left]

> Example: The permutation 1423 has two inversions (24) and (34), and the product (34)(24) is the same as 1423.

> Non-example: The permutation 3412 has four inversions (13), (14), (23) and (24), and the product (24)(23)(14)(13) is 4321 which is not the same as 3412.

> Among the permutations of {1, 2, 3, 4}, there are 14 examples (and 10 non-examples).

And 14 is C_4, the fourth Catalan number.

Re:Is this the simplest (and most surprising) sorting algori... by BoppreH on Tuesday October 05, 2021 @11:32AM (#28760829)
Not quite, your algorithm has j going from i+1 to n, and swapping when a[i] > a[j].

OP's algorithm has both indexes going from 1 to n, and swapping when a[i] < a[j].

Yours is more efficient and easy to see why it's correct, but OP's is slightly simpler and more surprising.

Re:Is this the simplest (and most surprising) sorting algori... by svat on Tuesday October 05, 2021 @11:46AM (#28761049)
Oh yes, good point: I got carried away; there is a difference! Simplicity is subjective (I think mine is simpler of course :P), but the algorithm in the post is indeed more surprising.
Re:Is this the simplest (and most surprising) sorting algori... by pricecomstock on Tuesday October 05, 2021 @12:10PM (#28761378)
Your algorithm does appear in Section 3.1 of the linked paper by the name "Exchange Sort"
Re:Is this the simplest (and most surprising) sorting algori... by jeffwass on Tuesday October 05, 2021 @12:14PM (#28761449)
I actually did the opposite! I was taught this sort in high school (I think the teacher called it shuttle interchange?), and I independently came up with the bubble sort on my own as a more-efficient in-place implementation on disk.

Back in the 90’s for my high-school computer class, we were saving records on disk using TurboPascal and I was trying to sort my file in-place.

The sort was super slow, which I assumed was the disk head having to jump back and forth over larger distances of the array of records when comparing and swapping.

I reasoned if I could keep the compares and swaps between I and I+1, and then sweep those I’s forward, it’s the same number of overall compares but less head jumping with each compare/swap being adjacent records.

My implementation did speed things up quite a bit. I have no idea if my reasoning was right, or if I had some other inadvertent bottleneck. But I was proud of the speedier result.

I never explicitly pointed it out to the teacher, though, and didn’t get full marks on that homework because he said my overall program wasn’t user friendly enough.

Re:Is this the simplest (and most surprising) sorting algori... by otto2 on Tuesday October 05, 2021 @02:27PM (#28763516)
Back in time I was sorting records on disk using Atari ST and (probably) Omikron BASIC. I applied RADIX sort (or a variant) that I saw in one magazine. It worked as a charm on RAM disk, but once when I tried it on a floppy disk, it just took forever.
Re:Is this the simplest (and most surprising) sorting algori... by goodcanadian on Tuesday October 05, 2021 @12:29PM (#28761664)
Your algorithm is in the paper as a comparison, and it is described as a standard exchange sort.
Re:Is this the simplest (and most surprising) sorting algori... by buzzin_ on Tuesday October 05, 2021 @07:49PM (#28767265)
What do you mean, it looks like bubble sort? It is bubble sort.
Re:Is this the simplest (and most surprising) sorting algori... by svat on Tuesday October 05, 2021 @09:30PM (#28768080)
Bubble sort only swaps adjacent elements. The one I used is a lesser-known sort apparently called "exchange sort".
Re:Is this the simplest (and most surprising) sorting algori... by odonnellryan on Tuesday October 05, 2021 @07:38PM (#28767170)
Oh gosh. Now you made me remember my own Catalan number problem with regards to weird solutions to generating balanced parentheses ahahaha.
Re:Is this the simplest (and most surprising) sorting algori... by maxk42 on Tuesday October 05, 2021 @12:31PM (#28761703)
Because I have a need to optimize:

    // Given array a[1], ..., a[n]
    for i = 1 to n - 1:
        swapped = false
        for j = i + 1 to n:
                if a[i] > a[j]:
                    swapped = true
                    swap(a[i], a[j])
                if swapped == false:
                    break
This should reduce time complexity to O( (n - 1)^2 ) and stop sorting after the remainder of the array is sorted and tested one time.
Re:Is this the simplest (and most surprising) sorting algori... by q-big on Tuesday October 05, 2021 @12:57PM (#28762094)
> This should reduce time complexity to O( (n - 1)^2 )

O((n-1)^2) = O(n^2).

Re:Is this the simplest (and most surprising) sorting algori... by labawi on Tuesday October 05, 2021 @01:33PM (#28762657)
I see where you're going, but your implementation broke the sort.

As for time complexity, it changes from ϴ(n²) to O(n²). Alternatively, you can drop the O-notation and calculate precisely so as to include constant factor speedups, though your algorithm may end up slower in the worst case.

Re:Is this the simplest (and most surprising) sorting algori... by y4mi on Tuesday October 05, 2021 @01:33PM (#28762671)
it doesnt work though

        def swap(lst, a, b):
            tmp = lst[a]
            lst[a] = lst[b]
            lst[b] = tmp

        def optimized_sort(numbers):
            i = 0
            while i < len(numbers):
                swapped = False
                j = i + 1
                while  j < len(numbers):
                    if numbers[i] > numbers[j]:
                        swapped = True
                        swap(numbers, i, j)
                    if swapped == False:
                        break
                    j +=1 
                i+=1
            return numbers

        numbers  = [1, 4,7,3,7,12,55,3,11,2,9,88,8]
        optimized_sort([*numbers]) => [1, 4, 2, 7, 7, 12, 3, 3, 8, 9, 55, 11, 88]

removing the break gets you the correct order: [1, 2, 3, 3, 4, 7, 7, 8, 9, 11, 12, 55, 88]
Re:Is this the simplest (and most surprising) sorting algori... by labawi on Tuesday October 05, 2021 @02:42PM (#28763732)
Original code would be correct, if not for a few spaces - probably a typo.
Re:Is this the simplest (and most surprising) sorting algori... by munk-a on Tuesday October 05, 2021 @04:11PM (#28764942)
This is why I like me some curly braces.

(Whitespace dependent languages suck when it comes to passing around code snippets - it's just the honest truth)

Re:Is this the simplest (and most surprising) sorting algori... by maxk42 on Tuesday October 05, 2021 @04:49PM (#28765419)
It was just an indentation typo. Swap check should be one level higher.
Re:Is this the simplest (and most surprising) sorting algori... by raxxorrax on Wednesday October 06, 2021 @02:38AM (#28769917)
That would be decent sort which returns a slighly sorted output.
Re:Is this the simplest (and most surprising) sorting algori... by keskadale on Tuesday October 05, 2021 @01:32PM (#28762650)
INOI 2004. this guy's OG.