You are on page 1of 20

Data Structures and Algorithms

(CS210/ESO207/ESO211)

Lecture 37
Integer Sorting (continued from last lecture)

Types of sorting algorithms


In Place Sorting algorithm:
A sorting algorithm which uses only O(1) extra space to sort.
Example: Heap sort, Quick sort.
Stable Sorting algorithm:
A sorting algorithm which preserves the order of equal keys while
0
1
2
3
4
5
6
7
sorting.
2A 5
3
0 6.1 3
7.9
4
0

0A

6.1

7.9

Example: Merge sort.


2

Integer Sorting algorithms


Continued from last class

Counting sort: algorithm for


sorting integers

Input: An array A storing integers in the range [0].
Output: Sorted array A.
Running time: O() in word RAM model of computation.
Extra space: O()

Counting sort: a visual description


0

2A

3
could
used
Why
did have
we scan
We

Count 2
1

Place
2

Count of
array
to
elements
A inonly
reverse
output
the elements
order (from
index to of
)
A
in sorted
order.
Why
while
placing
them
in
did
compute
Place
thewe
final
sorted array
and
B ?B ?

Counting sort: algorithm for sorting


integers

CountSort(A[...], )
For =0 to do Count[] 0;
For =0 to do Count[A[]] Count[A[]] +1;
For =0 to do Place[] Count[];
For =1 to do Place[] Place[] + Count[];
For = to do
{
??
] A[];
B[
Place[A[]]-1
Place[A[]] Place[A[]]-1;
}
return B;

Counting sort: algorithm for sorting


integers
It performs arithmetic operations involving O(log
Key points of Counting sort:

+ log ) bits (O() time

in word RAM).
It is a stable sorting algorithm.

Theorem: An array storing integers in the range [..]can be sorted in O(+)


time and using total O(+) space in word RAM model.
For , we get an optimal algorithm for sorting.
For , time and space complexity is O().
(too bad for . )
Question:
How to sort integers in the range [..] in O() time and using O() space?

Radix Sort

Radix Sort

Input: An array A storing integers, where


(i) each integer has exactly digits.
(ii) each digit has value
(iii) .

Output: Sorted array A.


Running time: O() in word RAM model of computation.
Extra space: O()
Important points:
makes use of a count sort.
Heavily relies on the fact that count sort is a stable sort
algorithm.

2012
1385
4961
5810
2373
6239
9624
8299
3465
7098
5501
9258

Demonstration of Radix Sort


through example
5810
4961
5501
2012
2373
9624
1385
3465
7098
9258
6239
8299

=4
=12
=10

2012
1385
4961
5810
2373
6239
9624
8299
3465
7098
5501
9258

Demonstration of Radix Sort


through example
5810
4961
5501
2012
2373
9624
1385
3465
7098
9258
6239
8299

5501
5810
2012
9624
6239
9258
4961
3465
2373
1385
7098
8299

2012
1385
4961
5810
2373
6239
9624
8299
3465
7098
5501
9258

Demonstration of Radix Sort


through example
5810
4961
5501
2012
2373
9624
1385
3465
7098
9258
6239
8299

5501
5810
2012
9624
6239
9258
4961
3465
2373
1385
7098
8299

2012
7098
6239
92588
299
2373
1385
3465
5501
9624
5810
4961

2012
1385
4961
5810
2373
6239
9624
8299
3465
7098
5501
9258

Demonstration of Radix Sort


through example
5810
4961
5501
2012
2373
9624
1385
3465
7098
9258
6239
8299

5501
5810
2012
9624
6239
9258
4961
3465
2373
1385
7098
8299

2012
7098
6239
92588
299
2373
1385
3465
5501
9624
5810
4961

Can you see where we are exploiting the


fact that Countsort is a stable sorting
algorithm ?

1385
2012
2373
3465
4961
5501
5810
6239
7098
8299
92589
624

Radix Sort

RadixSort(A[...], )
{ For =1 to do
Execute CountSort(A,) with th digit as the key;
return A;
}
Correctness:

A number stored in A

Inductive assertion:
At the end of th iteration, array A is sorted according to the last digits.
During the induction step, you will have to use
the fact that Countsort is a stable sorting
algorithm.

Radix Sort

RadixSort(A[...], )
{ For =1 to do
Execute CountSort(A,) with th digit as the key;
return A;
}
Time complexity:
A single execution of CountSort(A,) runs in O() time and O() space.
Since , a single execution of CountSort(A,) runs in O() time.
Time complexity of radix sort = O().
Extra space used = O().
Question: How to sort A storing integers in range [..] in O() time and
O() space ?
Answer:
RadixSort(A[...], )

Power of the word RAM model


Very fast algorithms for sorting integers:


Example: integers in range [..] in O() time and O()
space ?

Do not always go after Merge sort and Quick sort when


input is integers.

Interesting programming exercise:


Compare quick sort with Radix sort for
sorting long integers.

Data structures for


searching
in O(1) time

Problem Description

: {} called universe
,
,

Aim: A data structure for a given set that can facilitate


efficient searching.
A search query: Does ?
Note: can be any element from .

A trivial data structure for O(1)


search time
Build a 0-1 array A of size such that
A[] = 1 if .
A[] = 0 if .
Time complexity for searching an element in set : O(1).

0 1 2

0 0 1 A0 0

0 1 0

0 0 1

This is a totally Impractical data structure because


!
Example: = few thousands, = few trillions.
Question:
Can we have a data structure of O() size that can answer a search query in
O(1) time ?
Answer: Hashing

Next class
Hashing (15 minutes will be devoted to this topic)

a widely used heuristic in real life applications.

Recursion (the remaining time of the lecture)


This discussion on recursion will change the way most of you looked
at recursion. So do come to this last class of the course.

You might also like