Slowsort is a
sorting algorithm
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 important ...
. 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, also known as a spoof, a satire, a send-up, a take-off, a lampoon, a play on (something), or a caricature, is a creative work designed to imitate, comment on, and/or mock its subject by means of satiric or ironic imitation. Often its sub ...
formed by taking the
opposites
Opposite or Opposites may refer to:
* Opposite (semantics), a word that means the reverse of a word
* Opposite (leaf), an arrangement of leaves on a stem
* Opposite (mathematics), the negative of a number; numbers that, when added, yield zero
*" ...
of ''
divide and conquer''). It was published in 1986 by Andrei Broder and Jorge Stolfi in their paper ''Pessimal Algorithms and Simplexity Analysis''
(a parody of ''
optimal algorithms'' and ''
complexity analysis
In computer science, the analysis of algorithms is the process of finding the computational complexity of algorithms—the amount of time, storage, or other resources needed to execute them. Usually, this involves determining a function that re ...
'').
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 which transforms input using no auxiliary data structure. However, a small amount of extra storage space is allowed for auxiliary variables. The input is usually overwritten by the output ...
.
* It is a
stable 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 important ...
. (It does not 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 nd_idx, A iddle_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 has pioneered a number of programming lan ...
(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
for Slowsort is
.
A lower
asymptotic bound for
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 Paul Bachmann, Edmund Lan ...
is
for any
.
Slowsort is therefore not in
polynomial time. Even the best case is worse than
Bubble sort.
References
{{Sorting
Computer humor
Sorting algorithms