<p>I wrote an O(n!) sort for my amusement that can't be trivially optimized to run faster without replacing it entirely.  [And no, I didn't just randomize the items until they were sorted].  </p>

<p>How might I write an even worse Big-O sort, without just adding extraneous junk that could be pulled out to reduce the time complexity?</p>

<p><a href="http://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Big_O_notation</a> has various time complexities sorted in growing order.</p>

<p>Edit: I found the code, here is my O(n!) deterministic sort with amusing hack to generate list of all combinations of a list.  I have a slightly longer version of get_all_combinations that returns an iterable of combinations, but unfortunately I couldn't make it a single statement. [Hopefully I haven't introduced bugs by fixing typos and removing underscores in the below code]</p>

<pre><code>def mysort(somelist):
    for permutation in get_all_permutations(somelist):
        if is_sorted(permutation):
            return permutation

def is_sorted(somelist):
    # note: this could be merged into return... something like return len(foo) &lt;= 1 or reduce(barf)
    if (len(somelist) &lt;= 1): return True
    return 1 &gt; reduce(lambda x,y: max(x,y),map(cmp, somelist[:-1], somelist[1:]))

def get_all_permutations(lst):
    return [[itm] + cbo for idx, itm in enumerate(lst) for cbo in get_all_permutations(lst[:idx] + lst[idx+1:])] or [lst]
</code></pre>

[{'answer_id': 25572, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/users/13/chris-jester-young">Chris</a> and I mentioned <a href="https://stackoverflow.com/questions/19/fastest-way-to-get-value-of-pi#998">Bozosort</a> and <a href="https://stackoverflow.com/questions/19/fastest-way-to-get-value-of-pi#997">Bogosort</a> in a <a href="https://stackoverflow.com/questions/19/fastest-way-to-get-value-of-pi">different question.</a></p>\n'}, {'answer_id': 25574, 'author': 'Dana the Sane', 'author_id': 2567, 'author_profile': 'https://Stackoverflow.com/users/2567', 'pm_score': 0, 'selected': False, 'text': "<p>One way that I can think of would be to calculated the post position of each element through a function that vary gradually moved the large elements to the end and the small ones to the beginning. If you used a trig based function, you could make the elements osculate through the list instead of going directly toward their final position. After you've processed each element in the set, then do a full traversal to determine if the array is sorted or not.</p>\n\n<p>I'm not positive that this will give you O(n!) but it should still be pretty slow.</p>\n"}, {'answer_id': 25605, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': '<p>There\'s always NeverSort, which is O(∞):</p>\n\n<pre><code>def never_sort(array)\n  while(true)\n  end\n  return quicksort(array)\nend\n</code></pre>\n\n<p>PS: I really want to see your deterministic O(n!) sort; I can\'t think of any that are O(n!), but have a finite upper bound in classical computation (aka are deterministic).</p>\n\n<p>PPS: If you\'re worried about the compiler wiping out that empty while block, you can force it not to by using a variable both in- and outside the block:</p>\n\n<pre><code>def never_sort(array)\n  i=0\n  while(true) { i += 1 }\n  puts "done with loop after #{i} iterations!"\n  return quicksort(array)\nend\n</code></pre>\n'}, {'answer_id': 25618, 'author': 'BCS', 'author_id': 1343, 'author_profile': 'https://Stackoverflow.com/users/1343', 'pm_score': 0, 'selected': False, 'text': '<p>I think that if you do lots of copying then you can get a "reasonable" brute force search (N!) to take N^2 time per case giving N!*N^2</p>\n'}, {'answer_id': 25856, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<p>There\'s a (proven!) worst sorting algorithm called <a href="http://c2.com/cgi/wiki?SlowSort" rel="noreferrer">slow sort</a> that uses the “multiply and surrender” paradigm and runs in exponential time.</p>\n\n<p>While your algorithm is slower, it doesn\'t progress steadily but instead performs random jumps. Additionally, slow sort\'s best case is still exponential while yours is constant.</p>\n'}, {'answer_id': 28039, 'author': 'OysterD', 'author_id': 2638, 'author_profile': 'https://Stackoverflow.com/users/2638', 'pm_score': 0, 'selected': False, 'text': "<p>How about looping over all arrays t of n integers (n-tuples of integers are countable, so this is doable though it's an infinite loop of course), and for each of these:</p>\n\n<ul>\n<li>if its elements are exactly those of the input array (see algo below!) and the array is sorted (linear algo for example, but I'm sure we can do worse), then return t;</li>\n<li>otherwise continue looping.</li>\n</ul>\n\n<p>To check that two arrays a and b of length n contain the same elements, how about the following recursive algorithm: loop over all couples (i,j) of indices between 0 and n-1, and for each such couple</p>\n\n<ul>\n<li>test if a[i]==b[j]:</li>\n<li>if so, return TRUE if and only if a recursive call on the lists obtained by removing a[i] from a and b[j] from b returns TRUE;</li>\n<li>continue looping over couples, and if all couples are done, return FALSE.</li>\n</ul>\n\n<p>The time will depend a lot on the distribution of integers in the input array.</p>\n\n<p>Seriously, though, is there a point to such a question?</p>\n\n<p>Edit:</p>\n\n<p>@Jon, your random sort would be in O(n!) on average (since there are n! permutations, you have probability 1/n! of finding the right one). This holds for arrays of distinct integers, might be slightly different if some elements have multiple occurences in the input array, and would then depend on the distribution of the elements of the input arrays (in the integers).</p>\n"}, {'answer_id': 28100, 'author': 'Jon Dewees', 'author_id': 1365, 'author_profile': 'https://Stackoverflow.com/users/1365', 'pm_score': 1, 'selected': False, 'text': "<p>You could always do a Random sort. It works by rearranging all the elements randomly, then checking to see if it's sorted. If not, it randomly resorts them. I don't know how it would fit in big-O notation, but it will definitely be slow!</p>\n"}, {'answer_id': 178380, 'author': 'pookleblinky', 'author_id': 1582786, 'author_profile': 'https://Stackoverflow.com/users/1582786', 'pm_score': 1, 'selected': False, 'text': "<p>Here is the slowest, finite sort you can get:</p>\n\n<p>Link each operation of Quicksort to the Busy Beaver function.</p>\n\n<p>By the time you get >4 operations, you'll need up-arrow notation :)</p>\n"}]
['https://Stackoverflow.com/questions/25566', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2775/']