Live data from Hacker News

Bogo-bogosort

dangermouse.net

31–33 of 33 posts

Re: Bogo-bogosort

#31

I have a soft spot in my head for turdsort , which attempts to optimize over bogosort , but ends up being no better. #!/usr/bin/python from random import shuffle def turdsort(a): def turds(a): n = 0 for i in xrange(len(a)-1): if a[i] > a[i+1]: n += 1 return n count = 1 n = len(a) t = turds(a) while n > 0: while t >= n: shuffle(a) t = turds(a) count += 1 n = t return count if __name__ == '__main__': a = range(10) prin…

There is also the slightly less pointless ordersort, which works only on permutations. The complexity of ordersort is given by the Landau function. It is more efficient than bogosort, however.

  #!/usr/bin/python

  def gcd(a,b):
    while b:
      a, b = b, a % b
    return a

  def order(a):
    """compute the order of the permutation a"""
    lcm = 1 
    for i in range(len(a)):
      j = a[i]
      while j > i:
        j = a[j]
      if j == i: # i is a cycle leader
        j = a[j]	# get next element of cycle
        cyc = 1;	# the cycle has length at least 1
        while j != i:   # the cycle hasn't closed
          cyc += 1
          j = a[j]
        lcm = (lcm/gcd(lcm,cyc))*cyc
    return lcm

  def ordersort(a):
    ord = order(a)
    print ord
    b = range(len(a))
    while ord > 0:
      b = map((lambda i: a[i]), b)
      ord -= 1
    return b

  if __name__ == '__main__':
    from random import shuffle
    a = range(50);
    shuffle(a)
    print a, ordersort(a)

Re: Bogo-bogosort

#32
post #17
post #3

My addition to the algorithm: Every time your check if arrays is sorted and it isn't, start from the very beginning throwing away all progress made so far. I doubt this way n == 6 would finish in some normal time period.

bogosort does that already. I ran it on an the array [0,1,2,3,4,5,6] and it took between 3 hundred million and 1 billion operations (by my marginally accurate counter) and between 6 and 20 minutes, but mine was in python, not C.

Welcome to HN.

I was referring to OPs algorithm which was bogo-bogosort.

At first I though my change would not increase the complexity, but I think it is dependent on size of input since for every recursion there is a chance to fail and start completely over, where the number of recursion is dependent on input size. Therefore it adds to complexity.

Re: Bogo-bogosort

#33
post #8
post #7

This seems completely pointless. The elegance of bogosort is that it's an extremely simple algorithm, with a simple description of "randomize until it's sorted". Bogobogosort is complicated for no apparent reason. It's trying to be cute and clever, but there's no rationale for why additional complexity is being added. Bogobogosort seems in the end to be no more worthwhile than sleepybogosort, where you must sleep() i…

No, since the complexity of bogosort with sleep is still O(n!), while the algorithm from OP has a higher bound.

Depends on how you do the sleeps.
Post reply on HN