Professional Documents
Culture Documents
Sort Two 5
Sort Two 5
and Data
Structures
Marcin
Sydow
Algorithms
and Data
Structures
Marcin
Sydow
Algorithms
and Data A sorting algorithm is stable if it preserves the original order of
Structures
ties (elements of the same value)
Marcin
Sydow
Most sorting algorithms are easily adapted to be stable, but it is
Introduction not always the case.
QuickSort
Partition
Stability is of high importance in practical applications. E.g.
Limit
when the records of a database are sorted, usually the sorting key is
CountSort
RadixSort one of many attributes of the relation. The equality of the value of
Summary this attribute does not mean equality of the whole records, of course,
Algorithms
and Data
Structures
3 sorting algorithms were discussed up to now:
Marcin
Sydow
Introduction selectionSort
QuickSort insertionSort
Partition
Limit mergeSort
CountSort
RadixSort Two rst algorithms have square complexity but the third one
Summary is faster it has linear-logarithmic complexity.
Algorithms
and Data
Structures Quick sort is based on the divide and conquer approach.
Marcin
Sydow
The idea is as follows (recursive version):
Introduction 1 For the sequence of length 1 nothing has to be done (stop
QuickSort
the recursion)
Partition
Limit 2 longer sequence is reorganised so that some element M
CountSort (called pivot) of the sequence is put on nal position so
RadixSort that there is no larger element to the left of M and no
Summary
smaller element to the right of M.
Algorithms
and Data
Structures
Marcin The algorithm described above can be ecient only when the
Sydow
procedure described in step 2 is ecient.
Introduction
QuickSort This procedure can be implemented so that it has linear time
Partition
complexity and it works in place (constant space complexity) if
Limit
we take comparison as the dominating operation and sequence
CountSort
length as the datasize.
RadixSort
Summary
Due to this, quick sort is ecient.
Algorithms
and Data
Structures partition(S, l, r)
Marcin For a given sequence S (bound by two indexes l and r) the
Sydow
partition procedure selects some element M (called pivot)
QuickSort such a nal position so that there is no larger element to the
Partition
left of M and no smaller element to the right of M.
Limit
The partition procedure returns the nal index of element M.
CountSort
RadixSort
For the following assumptions:
Summary
Dominating operation: comparing 2 elements
Algorithms
and Data
Structures
Marcin
Having dened partition it is now easy to write a recursive
Sydow QuickSort algorithm described before:
Introduction
input: a - array of integers; l,r - leftmost and rightmost indexes
QuickSort
Partition of the array
Limit
(the procedure does not return anything)
CountSort
RadixSort quicksort(a, l, r){
Summary
if(l >= r) return;
k = partition(a, l, r);
quicksort(a, l, k - 1);
quicksort(a, k + 1, r);
}
QuickSort - analysis
Algorithms
and Data
Structures
Marcin
Sydow
Let n denote the lenght of the array - data size.
Introduction
QuickSort Dominating operation: comparing 2 elements of the sequence
Partition
Limit
The above version of quick sort is recursive and its time
CountSort
complexity depends directly on the recursion depth.
RadixSort
Summary
Notice that on each level of the recursion the total number of
comparisons (in partition) is of the rank Θ(n)
QuickSort - analysis, cont.
Algorithms
and Data
Structures
Marcin
Sydow The quick sort algorithm, after each partition call, calls itself
recursively for each of 2 parts of reorganised sequence
Introduction
(assuming the length of subsequence is igher than 1)
QuickSort
Partition
Limit First, for simplicity assume that the pivot element is put
CountSort always in the middle of the array. In such a case the recursion
Algorithms
and Data
Structures
Marcin It can be proved, that if we assume the uniform distribution of
Sydow
all the possible input permutations, the average time complexity
Introduction is also linear-logarithmic:
QuickSort
Partition
Limit
A(n) = Θ(n · log (n))
CountSort
Furthermore, it can be shown that the multiplicative constant is
RadixSort
not high - about 1.44.
Summary
Algorithms
and Data
Structures
Marcin
Sydow The pessimistic case is when the recursion depth is maximum
possible. What input data causes this?
Introduction
QuickSort
Partition
Limit
CountSort
RadixSort
Summary
QuickSort - pessimistic complexity
Algorithms
and Data
Structures
Marcin
Sydow The pessimistic case is when the recursion depth is maximum
possible. What input data causes this?
Introduction
QuickSort
Partition Input data which is already sorted (or invertedly sorted).
Limit
CountSort What is the recursion depth in such case?
RadixSort
Summary
QuickSort - pessimistic complexity
Algorithms
and Data
Structures
Marcin
Sydow The pessimistic case is when the recursion depth is maximum
possible. What input data causes this?
Introduction
QuickSort
Partition Input data which is already sorted (or invertedly sorted).
Limit
CountSort What is the recursion depth in such case? linear (Θ( n))
RadixSort
Summary
QuickSort - pessimistic complexity
Algorithms
and Data
Structures
Marcin
Sydow The pessimistic case is when the recursion depth is maximum
possible. What input data causes this?
Introduction
QuickSort
Partition Input data which is already sorted (or invertedly sorted).
Limit
CountSort What is the recursion depth in such case? linear (Θ( n))
RadixSort
Thus, the pessimistic complexity of the presented version of the
Summary
QuickSort algorithm is, unfortunately square
W (n) = Θ(n2 )
Properties of Quick Sort
Algorithms The algorithm is fast in average case, however its pessimistic time
and Data
Structures complexity is a serious drawback.
Marcin
Sydow To overcome this problem many corrected variants of quick sort
were invented. Those variants have linear-logarithmic pessimistic
Introduction
time complexity (e.g. special, dedicated sub-procedures for sorting
QuickSort very short sequences are applied)
Partition
Limit
Ensuring stability is another issue in quicksort. Adapting partition
CountSort
procedure to be stable is less natural compared to the algorithms
RadixSort
discussed before.
Summary
Algorithms
and Data
Structures
Marcin Among the algorithms discussed up to now, the best average
Sydow
time complexity order is linear-logarithmic
1 (merge sort, quick
Introduction sort).
QuickSort
Partition Is there comparison-based sorting algorithm which has better
Limit
rank of time complexity?
CountSort
RadixSort
Summary
1
Assuming comparison as the dominating operation and sequence
length as the data size
Is it possible to sort faster?
Algorithms
and Data
Structures
Marcin Among the algorithms discussed up to now, the best average
Sydow
time complexity order is linear-logarithmic
1 (merge sort, quick
Introduction sort).
QuickSort
Partition Is there comparison-based sorting algorithm which has better
Limit
rank of time complexity?
CountSort
RadixSort It can be mathematically proven that the answer is negative:
Summary
i.e. linear-logarithmic average time complexity is the best
possible for comparison-based sorting algorithms!
1
Assuming comparison as the dominating operation and sequence
length as the data size
Linear-logarithmic bound - explanation
Algorithms
and Data The problem of sorting n-element sequence by means of
Structures
Marcin comparisons can be viewed as follows. The task is to discover
Sydow the permutation of the original (sorted) sequence by asking
QuickSort
Partition Thus any comparison-based sorting algorithm can be
Limit represented as a binary decision tree, where each node is a
CountSort comparison and each leaf is the discovered permutation.
RadixSort
Notice that the number of leaves is n! (factorial)
Summary
Algorithms
and Data
Structures
To conclude:
Marcin
Sydow
is it possible to sort faster than with linear-logarithmic time
Introduction
complexity?
QuickSort
Partition
Limit
CountSort
RadixSort
Summary
Beyond comparisons...
Algorithms
and Data
Structures
To conclude:
Marcin
Sydow
is it possible to sort faster than with linear-logarithmic time
Introduction
complexity?
QuickSort
Partition
yes
Limit
CountSort
RadixSort
Summary
Beyond comparisons...
Algorithms
and Data
Structures
To conclude:
Marcin
Sydow
is it possible to sort faster than with linear-logarithmic time
Introduction
complexity?
QuickSort
Partition
yes
Limit
how is it possible?
CountSort
RadixSort
Summary
Beyond comparisons...
Algorithms
and Data
Structures
To conclude:
Marcin
Sydow
is it possible to sort faster than with linear-logarithmic time
Introduction
complexity?
QuickSort
Partition
yes
Limit
how is it possible?
CountSort
RadixSort
It is possible to beat the limit if we do not use comparisons.
Summary
Algorithms
and Data
Structures
Marcin
Sydow The idea of the algorithm is based on application of direct
The algorithm has lower time complexity than quick sort, but
the price is very high space complexity (2 helper arrays).
CountSort - code
Algorithms
and Data input: a - array of non-negative integers; l - its length
Structures
Marcin
Sydow
countSort(a, l){
Introduction
max = maxValue(a, l);
QuickSort
l1 = max + 1;
Partition
counts[l1];
Limit result[l];
CountSort for(i = 0; i < l1; i++) counts[i] = 0;
RadixSort
for(i = 0; i < l; i++) counts[a[i]]++;
Summary for(i = 1; i < l1; i++) counts[i] += counts[i - 1];
for(i = l - 1; i >= 0; i--)
result[--counts[a[i]]] = a[i];
}
(in the last line, notice pre-decrementation to avoid shing all the elements by 1
to the right)
CountSort - analysis
Algorithms
and Data
Structures
dominating operation: put value into array
Marcin
Sydow
data size (2 arguments): length of sequence n, maximum value
Introduction in the sequence
QuickSort
Partition
The algorithm needs 2 sequential scans through the arrays
Limit
(n-element one and m-element one). Its time complexity is
CountSort
RadixSort linear (!).
Summary
A(n, m) = W (n, m) = 2n + m = Θ(n, m)
Unfortunately, the space complexity is also linear (very high):
S (n, m) = n + m = Θ(n, m)
RadixSort
Algorithms
and Data
Structures The Radix Sort algorithm is a scheme of sorting rather than a
Marcin
Sydow proper sorting algorithm. It applies another, inernal sorting
algorithm.
Introduction
QuickSort It is ideal for lexicographic sort of object sequences having
Partition
xed length (e.g. strings, multi-digit numbers, etc.)
Limit
CountSort
Radix sort applies any stable sorting algorithm to all
RadixSort
consecutive positions of the sorted objects starting from the
Summary
last position to the rst one.
Algorithms
and Data
Structures
Marcin
Sydow
Stability
Introduction Partition
QuickSort
Partition QuickSort
Limit
Lower bound for sorting by comparisons
CountSort
CountSort
RadixSort
Summary Comparative analysis of (strong and weak) properties of all
sorting algorithms discussed
RadixSort
Algorithms
and Data
Structures
Marcin
Sydow
Introduction
QuickSort
Partition
Limit Thank you for your attention!
CountSort
RadixSort
Summary