The Folklore of Sorting Algorithms



IJCSI International Journal of Computer Science Issues, Vol. 4, No. 2, 2009

29


The above sequence a = 5, 7, 4, 9, 7 has the inversion
sequence
v = 0, 0, 2, 0, 1.

We now count the inversions (i, j) of a sequence a
separately for each position j. The resulting value of vj
gives the number of elements ai to the left of aj which are
greater than
α7∙. Obviously, we have vi _ i for all
i = 0, ..., n-1. If all vi are 0, then and only then the
sequence
a is sorted. If the sequence a is a permutation,
then it is uniquely determined by its inversion sequence
v.
The permutation
n-1, ..., 0 has inversion sequence 0, ..., n-
1.

Let a = a0, ..., an-1 be a sequence and v = v0, ..., vn-1 be its
inversion sequence. Then the number of steps
T (a)
required by insertion sort to sort sequence
a is

T (a) = Ï i = o, ..., n-1 vi                            (2)

Obviously, insertion sort takes vi steps in each iteration i.
Thus, the overall number of steps is equal to the sum of all
vi. This can be proved by an example: The following table
shows the sequence
a of the first example and its
corresponding inversion sequence. For instance,
v5 = 4
since 4 elements to the left of
a5 = 2 are greater than 2
(namely 5, 7, 3 and 4). The total number of inversions is
17.

Table 1: Inversion Table

i

_0___

J__

_2___

_3___

_4___

_5___

_6___

7

ai

J_

j____

_o___

J__

_4___

_2___

_6___

1

Vi

0___

о___

2___

2___

2___

4___

1_____

6

It is seen that insertion sort requires 17 steps to sort this
sequence.Thus the folklore 3 can be converged in to a
theorem as,
Theorem 3 : The sorting algorithm
insertion sort has a worst case time complexity of
T(n) = n∙(n-1)/2 comparison-exchange steps to sort a
sequence of
n data elements.

4.4 Folklore 4: Any comparison sort type of
algorithm requires lower bound comparisons in the
worst case.

Proof 4: From the folklore 2, it suffices to determine the
height of a decision tree in which each permutation
appears as a leaf. Consider a decision three of height h and
l leaves corresponding to a comparison sort on n elements.
Because each of the n! permutations of the input appear as
some leaf, we have n! ≤ l.

Since a binary tree of height h has no more than 2h leaves,
we have to conclude that n! ≤ l ≤ 2 , which by taking
logarithms , implies h ≥ log(n!) (Since the log function is
monotonically increasing).

Therefore,

h ≥ log(n!)

= log (n.n1.n2....2.1)

= log n + log (n1) + log (n2) + ... + log2 + log1

> log n + log (n1) + log(n2) + ... + log(n/2)

> log (n/2) + log (n/2) + ... + log (n/2)

= n/2 log (n/2)

= n/2logn - n/2

= Ω (n log n)                                       (3)

This folklore can be converged in to new theorem as,
Theorem 4: any comparison sort algorithm requires
Ω (
n log n) comparisons in the worst case.

5. Some Convergence of Theorems into
Folklore

Now we take a theorem and show that it is a piece of
folklore. We know that time needed for these algorithms
like selection sort varies like N2. [12]. Let’s deduce
folklore form this.

5.1 Deduction 1:

Algorithms like selection sort, takes each unsorted list and
goes through each item in order to identify the
smallest. The first unsorted list consists of all N items in
the original, and each successive unsorted list is one
smaller, as the smallest item each time is placed in its
correct position. So the first search for the smallest will
take N operations, the next search for the smallest will
take N-1 operations, the next N-2 operations, and so on
until the last search for the smallest take 2 operations (we
don't need to search when the list has only one
item). Thus the time needed will be a constant times the
following sum:

N + (N-1) + (N-2) + ... + 3 + 2 + 1          (4)

(We include the 1 to create a sum for which a standard
formula applies; it adds only a small constant which does
not affect the overall result.)

This sum, the sum of the first N integers, is known to be N
(N+1) / 2, which is approximately N2/2. Since we know
that this is multiplied by some constant representing the
time the operations take on a specific machine, we can see
that the time needed for this algorithm varies like N2, the
square of the amount of data. Thus the time needed for
the selection sort algorithm behaves like; each time the

IJCSI



More intriguing information

1. The name is absent
2. Secondary stress in Brazilian Portuguese: the interplay between production and perception studies
3. THE WELFARE EFFECTS OF CONSUMING A CANCER PREVENTION DIET
4. Migration and Technological Change in Rural Households: Complements or Substitutes?
5. Chebyshev polynomial approximation to approximate partial differential equations
6. Evidence on the Determinants of Foreign Direct Investment: The Case of Three European Regions
7. Income Mobility of Owners of Small Businesses when Boundaries between Occupations are Vague
8. Individual tradable permit market and traffic congestion: An experimental study
9. The name is absent
10. The name is absent
11. The name is absent
12. What should educational research do, and how should it do it? A response to “Will a clinical approach make educational research more relevant to practice” by Jacquelien Bulterman-Bos
13. Dynamiques des Entreprises Agroalimentaires (EAA) du Languedoc-Roussillon : évolutions 1998-2003. Programme de recherche PSDR 2001-2006 financé par l'Inra et la Région Languedoc-Roussillon
14. Job quality and labour market performance
15. The Functions of Postpartum Depression
16. WP RR 17 - Industrial relations in the transport sector in the Netherlands
17. National urban policy responses in the European Union: Towards a European urban policy?
18. For Whom is MAI? A theoretical Perspective on Multilateral Agreements on Investments
19. The name is absent
20. The name is absent