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

Pages: 1-

/prog/ challenge: sorting algorithm

Name: Anonymous 2011-11-30 4:37

It's been a long time since the last /prog/ challenge. However, since then the board has filled up with stupid assignments and the return of FrozenVoid.

To level the challenge with current times, here's the proposal:

Design and implement a sorting algorithm with complexity O(nn). Lower complexities will not be allowed. Redundand or blatantly no-ops will not be allowed. Poster must provide proof of complexity.

Extra points will be awarded for a O(nnn) or for an INTERCAL implementation.

Deadline is yesterday. Good luck.

Name: Anonymous 2011-11-30 6:27

sort(xs,p) = find(inorderby(p),permute(xs))

Name: Anonymous 2011-11-30 12:37

var uncleSam = new UncleSam();
try
{
    uncleSam.Sort(array, "please");
}
catch (DoNotWantException dnwex)
{
    uncleSam.Sort(array, "you motherfucker");
}

Name: Anonymous 2011-11-30 13:55

touhou~

Name: Anonymous 2011-11-30 16:49

>>4-kun, surely you meant tåhå? Don't worry, I'll let you off with a warning this time!

Name: Anonymous 2011-11-30 17:23

def nnsort(x): n = len(x); time.sleep(n ** n); return sorted(x)

Name: Anonymous 2011-11-30 18:46

This one here's O(n*n!):


import itertools

def issorted(N):
    it = iter(N)
    it.next()
    return all(a <= b for a,b in itertools.izip(N, it))

def sort(N):
    for Ns in itertools.permutations(N,len(N)):
        if issorted(Ns):
            return Ns


print(sort([3,6,2,1,9]))

Name: Anonymous 2011-11-30 18:55

Is there an algorithm that only sorts the output when time goes to infinity, so the sequences generated are only converging to the sorted sequence, or is that impossible given finite input?

Name: Anonymous 2011-11-30 18:59

>>8

I guess the problem with this is that formally a program that runs forever can't be considered a correct, because it never returns anything that can be verified.

So, it couldn't be a valid sorting algorithm.

Name: Anonymous 2011-11-30 19:11

>>9
What about a mathematical function?

Name: Anonymous 2011-11-30 19:49

/prog/ challenge: check my doubles

Name: Anonymous 2011-11-30 19:50

wtf you guys are fucking nerds have any of you even seen a real vagina? o_O

Name: Anonymous 2011-11-30 19:50

>>12
Yes I have seen a vagina.

Name: Anonymous 2011-11-30 19:56

>>12

Well obviously not. Otherwise we would lose all our wizard powers.

Name: Anonymous 2011-11-30 20:26

(defun same (list1 list2)
  (or (and (null list1) (null list2))
      (let ((item (car list1)))
        (and (= (count item list1) (count item list2))
             (same (remove item list1) (remove item list2))))))

(defun n^n-sort (list)
  (let ((length (length list))
        (new-list nil))
    (dotimes (i length)
      (push (elt list (random length)) new-list))
    (if (and (same list new-list) (apply #'<= new-list))
        new-list
        (n^n-sort list))))


It's similar to Bogosort, except that instead of just shuffling the list, it takes random items without making sure that it gets each one and only gets each one once. So, for the same reason that Bogosort is O(n!) (it has a 1/n! chance of getting the list right) mine is O(n^n). Technically it's a little less if there are duplicates in the input list, though.

Name: Anonymous 2011-11-30 21:07

>>12
Only your mother's.

>>15
Nice. I However, what does `(i length)` do? Wouldn't just `length` be enough as an argument of `dotimes`?

Name: Anonymous 2011-11-30 21:11

>>15
Lisp is shit.

Name: Anonymous 2011-11-30 21:19

>>16
That binds i to the number of the current iteration, and no, you need it even if you don't actually use it. For example, (dotimes (i 3) (print i)) prints:
0
1
2

Name: Anonymous 2011-12-01 0:53

>>15
Actually, bogosort is O(infinite), since that's the worst case scenario and the average of it (with 1 as best) is still infinite ((infinite + 1) / 2 = infinite).

Name: Anonymous 2011-12-01 1:08


#include <stdlib.h>

void mysort(void *base, size_t nmemb, size_t size,
            int(*compar)(const void *, const void *)) {

qsort( base, nmemb, size, compar );
}
/*
░░░░▄▄▄▄▀▀▀▀▀▀▀▀▄▄▄▄▄▄            
░░░░█░░░░▒▒▒▒▒▒▒▒▒▒▒▒░░▀▀▄             
░░░█░░░▒▒▒▒▒▒░░░░░░░░▒▒▒░░█      
░░█░░░░░░▄██▀▄▄░░░░░▄▄▄░░░█           
░▀▒▄▄▄▒░█▀▀▀▀▄▄█░░░██▄▄█░░░█
█▒█▒▄░▀▄▄▄▀░░░░░░░░█░░░▒▒▒▒▒█
█▒█░█▀▄▄░░░░░█▀░░░░▀▄░░▄▀▀▀▄▒█
░█▀▄░█▄░█▀▄▄░▀░▀▀░▄▄▀░░░░█░░█                      
░░█░░▀▄▀█▄▄░█▀▀▀▄▄▄▄▀▀█▀██░█                       
░░░█░░██░░▀█▄▄▄█▄▄█▄████░█                
░░░░█░░░▀▀▄░█░░░█░███████░█                
░░░░░▀▄░░░▀▀▄▄▄█▄█▄█▄█▄▀░░█
░░░░░░░▀▄▄░▒▒▒▒░░░░░░░░░░█
░░░░░░░░░░▀▀▄▄░▒▒▒▒▒▒▒▒▒▒░█
░░░░░░░░░░░░░░▀▄▄▄▄▄░░░░░█
*/

}

Name: Anonymous 2011-12-01 8:30

nice one

Name: Anonymous 2011-12-01 9:19

>>22
Nice doubles!

Name: Anonymous 2011-12-01 9:25

>>19
and the average of it (with 1 as best) is still infinite ((infinite + 1) / 2 = infinite).
That's not how it works.

I was wrong about the average case, though. According to Wikipedia it's O(n * n!), because you shuffle the list (which is O(n)) each iteration.

Name: Anonymous 2011-12-01 11:46

>>19
You failed CS 101 didn't you?

Name: Anonymous 2011-12-01 16:43

I wouldn't even know how to come up with a new method. And if I could, I'm sure it would have been done ages ago.

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