0% found this document useful (0 votes)
1 views14 pages

Linear Ordering

Linear Ordering dbms

Uploaded by

harshit765948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views14 pages

Linear Ordering

Linear Ordering dbms

Uploaded by

harshit765948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Linear Sorting

Curs 2017
Upper and lower bounds on time complexity of a problem.

A problem has a time upper bound TU (n) if there is an algorithm


A such that for any input of size n:
A(e) gives the correct answer in ≤ TU (n) steps.
A problem has a time lower bound TL (n) if there is NO algorithm
which solves the problem if time < TL (n), for any input of size n.
It may be that an algorithm solves the problem faster that TL (n)
for a specific input.

Lower bounds are hard to prove, as we have to consider every


possible algorithm.
Upper and lower bounds on time complexity of a problem.

I Upper bound: ∃A, ∀e time A(e) ≤ TU (|e|),


I Lower bound: ∀A, ∃e time A(e) ≥ TL (|e|),
To prove an upper bound: produce an A which works for any e,
|e| = n.
To prove a lower bound , show that for any possible algorithm, the
time on an input is greater than the lower bound.
Lower bound for comparison based sorting algorithm.
Use a decision tree: A binary tree where,
I each internal node represents a comparison ai : aj , the left
subtree represents the case ai ≤ aj and the right subtree
represents the case ai > aj
I each leaf represents one of the n! possible permutations
(aπ(1) , aπ(2) , . . . , aπ(n) ). Each of the n permutations must
appear as one of the leaves of the tree

9 4 6 1:2
1 2 3 < >=

2:3 1:3
< >= < >=

123 1:3 213 2:3


< >= < >=

132 312 231 312


469
Theorem
Any comparison sort that sorts n elements must performe Ω(n lg n)
comparisons.

Proof.
Equivalent to prove: Any decision tree that sorts n elements must
have height Ω(n lg n).
Let h the height of a decision tree with n! leaves,
n! ≤ 2h ⇒ h ≥ lg(n!) > lg( ne )n = Ω(n lg n).
Linear sorting: Counting sort
Assume the input A[1 . . . n], is an array of integers betwen 0 and k.
Need: an array B[1 . . . n] as the output
and an array C [1 . . . k + 1] as scratch.
Counting (A, k)
for i = 0 to k do
do C [i] := 0
end for
for i = 1 to n do
do C [A[i]] := C [A[i]] + 1
end for
for i = 1 to k do
do C [i] := C [i] + C [i − 1]
end for
for i = n downto 1 do
do B[C [A[i]]] := A[i]
C [A[i]] := C [A[i]] − 1
end for
Counting (A, k)
for i = 0 to k do
do C [i] := 0 { O(k)}
end for
for i = 1 to n do
do C [A[i]] := C [A[i]] + 1 { O(n)}
end for
for i = 1 to k do
do C [i] := C [i] + C [i − 1] { O(k)}
end for
for i = n downto 1 do
do B[C [A[i]]] := A[i] { O(n)}
C [A[i]] := C [A[i]] − 1 { O(k)}
end for
Time complexity: T (n) = O(n + k) if k = O(n), then
T (n) = O(n).
Linear sorting: Radix sort

An important property of counting sort is that it is stable, numbers


with the same value, appear in the output in the same order as
they do in the input.
For instance Heap sort is not stable.
Given an array A with n keys, each one with d digits, the Radix
(Least Significant Digit),
Radix LSD (A, d)
for i = 1 to d do
Use stable sorting to sort the i-th digit of A.
end for
Example

329 720 720 329


475 475 329 355
657 355 436 436
839 ⇒ 436 ⇒ 839 ⇒ 457
436 657 355 657
720 329 657 720
355 839 475 839
Theorem (Correctness of Radix)
The previous algorithm sort correctly n keys.

Induction on d.
If d = 1 the stable sorting works. Assume it is true for d − 1,
to sort the d-th digit,
if ad < bd then a will be placed before b,
if bd < ad then b will be placed before a,
if bd = ad then as we are using a stable sorting a and b will remain
in the same order, which by hypothesis was already the correct
one.
Complexity

Given n integers each with d digits, each digit in the range 0 to 9,


if we use counting sorting: T (n, d) = Θ(d(n + 9)).
Faster?
Given n words of b bits each. View each word as having d = b/r
digits of r bits
11001010 0011010011101001 11001000
In this case b = 38, r = 8 and d = 4
Notice r bits can take value between 0 and 2r , so each pass of
counting sort will take:
Θ(n + 2r ) ⇒ T (n, b) = Θ(d(n + 2r )) = Θ((b/r )(n + 2r )).
Given b and n, how to choose r ?
I If we take r >> log n, then T (n, b) is exponential,
I If r ∼ lg n ⇒ T (n, b) = Θ( lgbn (2n)).
Comparing radix and counting:

For n integers, each integer with at most d digits, where each digit
is in the range [0, 9]:
I Counting sort is Θ(9dn),
I Radix with the choice of r = log9 n-digits can sort n d-digit
numbers in logd n Θ(9n).
9
Comparing radix and quicksort:

Consider 2000 integers of 32 bits each:


I Quicksort needs to do lg 2000 = 11 passes over the data,
I Radix sort with digits of 11-bits, takes 3 passes (at each one
counting sort makes 2 passes).

Empirically, when dealing with natural numbers,radix is better than


other sorting methods for values of n > 2000.
A bit of history.

Radix and all counting sort are due to


Herman Hollerith.
In 1890 he invented the card sorter that
allowed to shorten the US census to 5
weeks, using punching cards.

You might also like