Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Sorting algorithms

Name: Anonymous 2007-08-01 12:56 ID:otd2+Ec4

Lets talk about them! Which ones do you like/use/have implemented/have invented/etc.?

Code related:

    // A spectacular variant of bogo-sort which has the interesting property
    // that, if the Many Worlds interpretation of quantum mechanics is true, it
    // can sort an arbitrarily large array in linear time.
    static BogoSort(arrayToSort : array [int]) : void
    {
        // permute the array randomly using a quantum process
        def rnd = Random(DateTime.Now.Millisecond);
        def sort(left = 0)
        {
            when (left < arrayToSort.Length - 1)
            {
                def right = rnd.Next(left, arrayToSort.Length);
                def tmp = arrayToSort[left];
                arrayToSort[left] = arrayToSort[right];
                arrayToSort[right] = tmp;
                sort(left + 1);
            }
        }
        sort();

        // check if array is sorted
        def check(left = 0)
        {
            def right = left + 1;
            when (right < arrayToSort.Length)
            {
                if (arrayToSort[left] < arrayToSort[right])
                    check(right);
                else
                    throw Exception("Sort failed! Please destroy the universe. :(");
            }
        }
        check();
    }

Name: Anonymous 2007-08-01 12:58 ID:4IDyUFcY

quicksort.

Name: Anonymous 2007-08-01 13:05 ID:Heaven

What the fuck is this language?

Name: Anonymous 2007-08-01 13:26 ID:tlfQbkUK


(defun merge-sort (list)
  (if (endp (rest list)) list
      (insert (first list) (merge-sort (rest list)))))

(defun insert (item list)
  (if (or (endp list) (< item (car list))) (cons item list)
      (cons (first list) (insert item (rest list)))))

Name: Anonymous 2007-08-01 13:36 ID:iogNOH4H

>>3

It's something.NET, but I don't know what

Name: Anonymous 2007-08-01 13:38 ID:8xJbQiwv

Google thinks ASP.NET

Name: Anonymous 2007-08-01 13:38 ID:ekb73pv7

quickSort [] = []
quickSort (x:xs) = quickSort (filter (< x) xs) ++ x ++ quickSort (filter (>= x) xs)

Name: Anonymous 2007-08-01 13:44 ID:iogNOH4H

Got it, it's J#

Name: Anonymous 2007-08-01 13:46 ID:iogNOH4H

No wait

Name: Anonymous 2007-08-01 13:46 ID:iogNOH4H

I think it's this http://www.erights.org

Name: Anonymous 2007-08-01 13:47 ID:iogNOH4H

No no I'm wrong again, I don't know what it is

Name: Anonymous 2007-08-01 13:50 ID:iogNOH4H

Python.NET?

Name: Anonymous 2007-08-01 14:31 ID:CjleYNTL

C# like syntax, object-oriented and functional programming, optional type annotation, .NET framework.. Sounds good!

Name: Anonymous 2007-08-01 15:42 ID:1j+4HILw

No reason to rewrite sorting algorithms, I like NIH so I don't have to take the blame.

Name: Anonymous 2007-08-01 18:25 ID:CjleYNTL

How is this algorithm called? I invented it!


def srt arr, i
    if i < arr.length - 1
        if not arr[i] <= arr[i+1]
            tmp = arr[i]
            arr[i] = arr[i+1]
            arr[i+1] = tmp
            srt arr, i == 0 ? i : i-1
        else
            srt arr, i+1
        end
    else
        return arr
    end
end

Name: Anonymous 2007-08-01 18:32 ID:8xJbQiwv

>>15

Shit

Name: Anonymous 2007-08-01 18:50 ID:CjleYNTL

>>16
Can't be.. it has to have some cool name like Smoothsort, Gnome sort, LSD Radix sort or Bucket sort. You know..

Name: Anonymous 2007-08-01 19:28 ID:CjleYNTL

>>17
OK, I can't find it anywhere, I guess I really am the inventor! Though, it looks a bit like Bubble sort.. but should be faster!

Sweet. I think I'll name it Ghey Nigger Bubble sort.

Name: Anonymous 2007-08-01 20:14 ID:aT9U4L7e

My current favourite is the radix sort variant where you count the number of items that go in each bucket in the preceding radix pass, and therefore can produce your intermediate outputs right off the bat without linked lists or other such structures that require dynamic allocation. Destructive radixsort ends up using N temporary space for N sorted keys (plus a small constant amount depending on how big your radix is), which isn't that big a deal given that just storing the data you're about to sort requires about as much too.

Not exactly the most exciting algorithm around, and rather shitty from a theoretical standpoint. However, in today's computers optimization is about hitting the cache as often as possible and avoiding impredictable branching; this kind of radix sorting accomplishes the former by allocating temporary memory at the start of the routine and by not doing dynamic memory management, and the latter by using bitwise operations and indexing rather than comparisons. The "N storage to sort N items" issue's cache effect can be mitigated with prefetch instructions, as every pass of radix sort fetches keys in order. (Plus the temp space can be allocated on the stack if sufficiently small, which is nearly guaranteed to be "hot" already.)

Radixsort also has the worst-case advantage over qsort, which is nice if you're sorting inputs from an untrusted source, i.e. no chance of exploitable worst-case performance.

Name: Anonymous 2007-08-01 20:17 ID:aT9U4L7e

I'll poast code to >>19 if some expect BBCode programmer will tip me off on how I'd coax this board software to not mangle my indentation.

Name: Anonymous 2007-08-01 20:43 ID:iogNOH4H

>>20

Use [code] \[/code] tags

Name: Anonymous 2007-08-01 20:56 ID:Heaven

>>21
The only useful BBCode I've seen.
Thanks.

Name: Anonymous 2007-08-01 21:02 ID:tlfQbkUK

>>22
you don't understand sage

Name: Anonymous 2007-08-01 21:11 ID:QkXE14Uy

I have a more efficient version of it, which simply does rnd() over all the bytes in the array

Name: Anonymous 2007-08-01 21:13 ID:QkXE14Uy

btw the language of op, I'm pretty sure it's nemerle

Name: Anonymous 2007-08-01 22:05 ID:Heaven

>>23
No YOU don't understand sage.
Lurk more.
Or read a damn FAQ once in a while.

Name: Anonymous 2007-08-01 22:14 ID:iogNOH4H

>>25
Looks like you're right, thanks

Name: Anonymous 2007-08-04 7:48 ID:1wJo5U5o

>>20
Do it.

Name: Anonymous 2007-08-04 8:56 ID:dXV5lm8S

Yeah, hang on. I've got a hangover to manage here.

Name: Anonymous 2009-01-14 15:06

Turing

Name: Anonymous 2011-01-31 19:57

<-- check em dubz

Don't change these.
Name: Email:
Entire Thread Thread List