Slowsort
   HOME

TheInfoList



OR:

Slowsort is a
sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending ...
. It is of humorous nature and not useful. It is a ''reluctant algorithm'' based on the principle of ''multiply and surrender'' (a
parody A parody is a creative work designed to imitate, comment on, and/or mock its subject by means of satire, satirical or irony, ironic imitation. Often its subject is an Originality, original work or some aspect of it (theme/content, author, style, e ...
formed by taking the opposites of ''
divide and conquer The term divide and conquer in politics refers to an entity gaining and maintaining political power by using divisive measures. This includes the exploitation of existing divisions within a political group by its political opponents, and also ...
''). It was published in 1984 by Andrei Broder and Jorge Stolfi in their paper "Pessimal Algorithms and Simplexity Analysis" (a parody of '' optimal algorithms'' and '' complexity analysis'').


Algorithm

Slowsort is a
recursive algorithm In computer science, recursion is a method of solving a computational problem where the solution depends on solutions to smaller instances of the same problem. Recursion solves such recursive problems by using functions that call themselves ...
. * It sorts
in-place In computer science, an in-place algorithm is an algorithm that operates directly on the input data structure without requiring extra space proportional to the input size. In other words, it modifies the input in place, without creating a separa ...
. * It is an
unstable sort In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is importa ...
. (It might change the order of equal-valued keys.) This is an implementation in pseudocode: procedure slowsort(A[], start_idx, end_idx) // Sort array range A[start ... end] in-place. if start_idx ≥ end_idx then return middle_idx := floor( (start_idx + end_idx)/2 ) slowsort(A, start_idx, middle_idx) // (1.1) slowsort(A, middle_idx + 1, end_idx) // (1.2) if A nd_idx< A iddle_idxthen swap (A, end_idx, middle_idx) // (1.3) slowsort(A, start_idx, end_idx - 1) // (2) * Sort the first half, recursively. (1.1) * Sort the second half, recursively. (1.2) * Find the maximum of the whole array by comparing the results of 1.1 and 1.2, and place it at the end of the list. (1.3) * Sort the entire list (except for the maximum now at the end), recursively. (2) An unoptimized implementation in
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
(purely functional) may look as follows: slowsort :: (Ord a) => -> slowsort xs , length xs <= 1 = xs , otherwise = slowsort xs' ++ ax llast rlast -- (2) where m = length xs `div` 2 l = slowsort $ take m xs -- (1.1) r = slowsort $ drop m xs -- (1.2) llast = last l rlast = last r xs' = init l ++ min llast rlast : init r


Complexity

The runtime T(n) for Slowsort is T(n) = 2 T(n/2) + T(n-1) + 1. A lower asymptotic bound for T(n) in
Landau notation Big ''O'' notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. Big O is a member of a family of notations invented by German mathematicians Paul ...
is \Omega\left(n^\right) for any \epsilon > 0. Slowsort is therefore not in
polynomial time In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations p ...
. Even the best case is worse than
bubble sort Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, Swap (computer science), swapping their values ...
.


References

{{Sorting Sorting algorithms