Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

BITS F232

aft
Foundations of
Data Structures and Algorithms
Lect 8
Sameera Muhamed Salam
Dept of CSIS, BITS Pilani, Hyderabad Campus

Dr
Linear Time Sorting Algorithms

BITS F232 Sameera M S 2


Inplace and Stable sorting Algorithms

In-place Algorithm
Algorithm that does not need an extra space and produces an output in the same
memory that contains the data by transforming the input ‘in-place’. However, a small
constant extra space used for variables is allowed.

Stable Sorting Algorithm


A sorting algorithm is said to be stable if two objects with equal keys appear in the
same order in sorted output as they appear in the input data set.

Exercise: Name some stable sorting algorithms.

BITS F232 Sameera M S 3


Linear Time Sorting Algorithms: Introduction

▶ Sorting algorithms which we see so far are comparison sorting algorithms:


▶ the sorted order they determine is based only on comparisons between the input
elements.
▶ No comparison sort exists that is faster than ω(n log n).
▶ All linear time algorithms use operations other than comparisons to determine the
sorted order.

BITS F232 Sameera M S 4


Counting Sort
▶ Assumes that each of the n input elements is an integer in the range 0 to k, for
some integer k
▶ When k = O(n), the sort runs in O(n) time.
▶ Basic idea is to determine, for each input element x, the number of elements less
than x.
▶ This information can be used to place element x directly into its position in the
output array.

BITS F232 Sameera M S 5


Counting Sort Pseudocode
▶ INPUT:An array A of length n ≥ 1, k is the largest eelement in the array, An
empty array B
▶ OUTPUT: Sorted array B

▶ Araay C is used for counting.


▶ Running time is Θ(n).
BITS F232 Sameera M S 6
Bucket Sort

▶ Runs in linear time when the input is drawn from a uniform distribution.
▶ Counting sort assumes that the input consists of integers in a small range, where
as, bucket sort assumes that the input is generated by a random process that
distributes elements uniformly over the interval [0, 1).
▶ The idea of bucket sort is to divide the interval [0, 1) into n equal-sized subin-
tervals, or buckets
▶ Distribute the n input numbers into the buckets.
▶ To produce the output,
▶ Sort the numbers in each bucket
▶ Go through the buckets in order, listing the elements in each.

BITS F232 Sameera M S 7


Bucket Sort Pseudocode

▶ INPUT: An array A of n ≥ 1.
▶ OUTPUT: Sorted array A

▶ Running time is Θ(n)

BITS F232 Sameera M S 8


Insertion sort

Figure: [Ref:GeeksforGeeks]

BITS F232 Sameera M S 9


Radix Sort

Figure: [Ref:Programiz]
▶ Digit by digit sorting
▶ Given n d-digit numbers in which each digit can take on up to k possible values,
RADIX-Sort correctly sorts these numbers in Θ(d(n + k)) time.
BITS F232 Sameera M S 10
Introduction to Linked List

▶ Disadvantages of Arrays?
▶ A linked list is a data structure in which the objects are arranged in a linear order.
▶ The order in a linked list is determined by a pointer in each object.
▶ Provide a simple, flexible representation for dynamic sets

BITS F232 Sameera M S 11


Linked List Operations

▶ Insertion
▶ at beginning
▶ at end
▶ at any position
▶ Deletion
▶ from beginning
▶ from end
▶ from any position
▶ Traversal
▶ Searching

BITS F232 Sameera M S 12


References:

▶ Michael T. Goodrich and Roberto Tamassia, “ Algorithm Design Foundations,


Analysis, and Internet Examples”, Wiley Student Edition.
▶ Jon Kleinberg and Eva Tardos, “ Algorithm Design”, Pearson Publishers.
▶ Thomas H. Chorman, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein, “
Introduction to algorithms”, MIT Press Publishers.
▶ Sanjoy Dasgupta, Umesh Vazirani, Christos Papadimitriou, “ Algorithms”,
McGraw-Hill Education Publishers.

BITS F232 Sameera M S 13


Discussion Time!

BITS F232 Sameera M S 14


BITS F232 Sameera M S 15

You might also like