0% found this document useful (0 votes)
42 views20 pages

Data Structures and Algorithms: (CS210/ESO207/ESO211)

This document discusses different sorting algorithms like counting sort, radix sort, and hashing. Counting sort can sort integers in a range [0, k] in O(n+k) time by using an auxiliary count array. Radix sort generalizes this idea to sort numbers with multiple digits in O(d(n+b)) time by performing counting sort repeatedly based on each digit. Hashing allows searching a set S of size n for an element in O(1) time using a hash table of size O(n). The next class will cover hashing and recursion.

Uploaded by

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

Data Structures and Algorithms: (CS210/ESO207/ESO211)

This document discusses different sorting algorithms like counting sort, radix sort, and hashing. Counting sort can sort integers in a range [0, k] in O(n+k) time by using an auxiliary count array. Radix sort generalizes this idea to sort numbers with multiple digits in O(d(n+b)) time by performing counting sort repeatedly based on each digit. Hashing allows searching a set S of size n for an element in O(1) time using a hash table of size O(n). The next class will cover hashing and recursion.

Uploaded by

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

Data Structures and Algorithms

(CS210/ESO207/ESO211)
Lecture 37

Integer Sorting (continued from last lecture)
1
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 sorting.






Example: Merge sort.
2
A
0 1 2 3 4 5 6 7
2 5 3 0 6.1 3 7.9 4
A
0 1 2 3 4 5 6 7
0 2 3 3 4 5 6.1 7.9
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


A
0 1 2 3 4 5 6 7
Count
0 1 2 3 4 5
2 5 3 0 2 3 0 3
2
2 2 4 7 7 8
0 2 3 0 1
Place
0 1 2 3 4 5
B
0 1 2 3 4 5 6 7
3 0
We could have used Count
array only to output the
elements of A in sorted
order. Why did we compute
Place and B ?
Why did we scan
elements of A in reverse
order (from index to )
while placing them in the final
sorted array 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
{ B[ ?? ] A[];
Place[A[]] Place[A[]]-1;
}
return B;
Place[A[]]-1
Counting sort: algorithm for sorting integers
Key points of Counting sort:
It performs arithmetic operations involving O(log + log ) bits (O(1) 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.

2 0 1 2
1 3 8 5
4 9 6 1
5 8 1 0
2 3 7 3
6 2 3 9
9 6 2 4
8 2 9 9
3 4 6 5
7 0 9 8
5 5 0 1
9 2 5 8
5 8 1 0
4 9 6 1
5 5 0 1
2 0 1 2
2 3 7 3
9 6 2 4
1 3 8 5
3 4 6 5
7 0 9 8
9 2 5 8
6 2 3 9
8 2 9 9
Demonstration of Radix Sort through example

A
=4
=12
=10
2 0 1 2
1 3 8 5
4 9 6 1
5 8 1 0
2 3 7 3
6 2 3 9
9 6 2 4
8 2 9 9
3 4 6 5
7 0 9 8
5 5 0 1
9 2 5 8
5 8 1 0
4 9 6 1
5 5 0 1
2 0 1 2
2 3 7 3
9 6 2 4
1 3 8 5
3 4 6 5
7 0 9 8
9 2 5 8
6 2 3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you see where we are exploiting 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;
}
Correctness:





Inductive assertion:
At the end of th iteration, array A is sorted according to the last digits.



A number stored in A
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


: {0,1, , } 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).




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

0 0 1 0 0 0 1 0 0 0 1
0 1 2 3 4 -1
A
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