Download Sorting Sequences - Applied Probability Trust

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Law of large numbers wikipedia , lookup

Georg Cantor's first set theory article wikipedia , lookup

Real number wikipedia , lookup

Large numbers wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Algorithm wikipedia , lookup

Hyperreal number wikipedia , lookup

Collatz conjecture wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Sequence wikipedia , lookup

Transcript
Published by the Applied Probability Trust
© Applied Probability Trust 2004
13
Sorting Sequences
JONATHAN SMITH
Take any sequence of numbers, e.g. 17, 54, 23, 31, 17, 3 . There are various algorithms to
sort these numbers into ascending order (with the lowest number on the left). With a sorting
algorithm there need to be a certain number of comparisons made between numbers and a
certain number of swaps made. Only adjacent numbers can be compared or swapped. The
number of comparisons needed gives some idea of the efficiency of the algorithm, but the
number of swaps is the same regardless of the algorithm.
For readers who are familiar with sorting algorithms, sorting the above sequence into
ascending order using the bubble sort algorithm requires 15 comparisons and 10 swaps. Using
the shuttle sort algorithm requires 14 comparisons and 10 swaps. Notice that the number of
swaps is the same, as expected.
Given A distinct numbers there are A! ways of arranging them in a sequence. If we treat
the left-hand number as the most significant, followed by the next one in and so on, we can
arrange the sequences in an order and then label them from 0 to (A! − 1) in ascending order,
giving us the sequence number. For example, if we take sequences with three numbers, there
are 3! = 6 possible sequences. We will number these 0 to 5 as in table 1.
Table 1 Sequences of three numbers.
Sequence
1
1
2
2
3
3
2
3
1
3
1
2
3
2
3
1
2
1
Sequence number
0
1
2
3
4
5
The aim of this article is to show how to work out the sequence number of a given sequence
and, conversely, how to work out the sequence with a given sequence number.
1. Reducing the problem to standard form
The numbers in a sequence can be in any order, but it is important that they are the numbers
{1, 2, 3, . . . , A} . If they are not, then replace the lowest number with 1, the next lowest with 2
and so on.
If there are any duplicate digits, then these should be assigned numbers in ascending
order, starting from the left-hand side. Thus, the sequence 17, 54, 23, 31, 17, 3 becomes
2, 6, 4, 5, 3, 1 .