DSA Unit 1
DSA Unit 1
INTRODUCTION
Mathematics Review, Introduction to recursion,
Asymptotic notations, sequential search,
Binary Search - sorting algorithms: Insertion
sort, shell sort, merge sort, quick sort.
Unit II
LINEAR DATA STRUCTURES
List ADT - Stack ADT - Queue ADT - Array and
Linked Implementations - Applications.
Unit III
NON LINEAR DATA STRUCTURES
Binary Trees - Binary Search Tree - Adelson
Velski Landis(AVL) Trees - Tree Traversals - B-
Trees.
Unit IV
PRIORITY QUEUE AND HASHING
Priority Queue - Binary Heap - Heapsort - Hash
functions - separate chaining, open addressing
- rehasing - Extendible hashing.
Unit V
DISJOINT SET ADT AND GRAPH ALGORITHMS
Basic data structure - smart union algorithms -
path compression - Topological sort - Shortest
path algorithms - Minimum spanning tree.
References
1. Mark Allen Weiss, Data Structures and Algorithm
Analysis in C, Second Edition, Pearson Education,
2015.
2. Thomas H Cormen, Charles E Leiserson, Ronald L
Rivest, Clifford Stein, Introduction to Algorithms,
Third Edition, MIT Press, 2014.
3. Ellis Horowitz, Sartaj Sahni, Susan Anderson Freed,
Fundamentals of Data Structures in C, Second
Edition, Universities Press, 2008.
4. Gilberg, Data Structures: A Pseudocode Approach
with C, Second Edition, Cengage Learning, 2007.
NEED FOR DATA
STRUCTURES AND
MATHEMATICS REVIEW
INTRODUCTION
• Typically a computer is a data processing
machine
*
Contd..
• The field of data structures mainly deals with
organization of data in the main
memory.
• What is data structure?
– Defines how the data is
organized / structured
Terminology
• Abstract Data Type (ADT)
– Mathematical description of an object with set of
operations on the object. Useful building block.
• Algorithm
– A high level, language independent, description of a step-
by-step process
• Data structure
– A specific family of algorithms for implementing an abstract
data type.
• Implementation of data structure
– A specific implementation in a specific language
*
Terminology examples
• A stack is an abstract data type supporting push,
pop and isEmpty operations
• A stack data structure could use an array, a linked
list, or anything that can hold data
*
Concepts vs. Mechanisms
• Abstract • Concrete
• Pseudocode • Specific programming language
• Algorithm • Program
– A sequence of high-level, – A sequence of operations in a specific
language independent programming language, which may
operations, which may act act upon real data in the form of
upon an abstracted view of numbers, images, sound, etc.
data. • Data structure
• Abstract Data Type (ADT) – A specific way in which a program’s
– A mathematical description of data is represented, which reflects
an object and the set of the programmer’s design
operations on the object. choices/goals.
*
Need for Data Structure
• To have efficiency in computation
General idea:
When something is organized properly, it
is very easy to have processing /
computation.
Definition
• Data structures is a way of organizing data
elements in a computer memory in a
manner that is convenient to perform
operations on it.
• When elements of data are organized
together in terms of some relationship
among the elements, the organization is
called DS.
Classification of data Structures
• Primitive data type
– Int
– Float
– Character
*
Data Structure
• A data structure is the physical implementation of
an ADT.
– Each operation associated with the ADT is
implemented by one or more subroutines in the
implementation.
Theorem:
logAB = logc B/logcA, C>0
log AB = log A+log B
log A/B = log A-log B
log(AB) = B log A
Log 1 = 0, log 2 =1, log 1,024=10
Series
Modular arithmetic
For a positive integer n, two integers a and b are
said to be congruent modulo n, written:
a is congruent to b modulo n
A Ξ B(mod N)
if their difference a − b is an integer multiple of n (or n divides a − b). The number
n is called the modulus of the congruence. 38=14 (mod12)
Recursion
• A function that is defined in terms of itself is called
recursion.
• a recursive call will keep on being made until a base
case is reached.
• Value for which the function is directly known is called
base case
• Remember:
Recursion is meaningless without a base case.
int factorial(int n)
{
if n==1 or n==0 return 1;// base case
Else
return n * factorial(n-1); }
FAQ -1
1. Which Data Structure is used to perform Recursion?
a. Queue b. Stack c. Linked List d.
Tree
– May be specified
• In English
• As a computer program
• As a pseudo-code
SO2 - Why Analysis of
Algorithms?
• If we want to go from city A to city B, there can be many ways
of doing this: by flight, by bus, by train and also by cycle.
• Depending on the availability and convenience we choose the
one which suits us.
• Similarly, in computer science there can be multiple algorithms
exist for solving the same problem .
• Example: To sort the given numbers; there are so many sorting
algorithms available (Insertion sort, merge sort, selection sort,
etc)
How to analyze the algorithms?
– Size of an array
– Polynomial degree
– Defines the input for which the algorithm takes huge time.
– Input is the one for which the algorithm runs the slower.
• Best case
– Defines the input for which the algorithm takes lowest time.
– Input is the one for which the algorithm runs the fastest.
• Average case
m=m+1; // constant c
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
Values of some important functions as n
Assessment
a. big-O notation
b. big-Ω notation
c. big-C notation
d. small-O notation
Searching
• Searching through a lot of records for a specific record or set
of records
• Placing records in order, which we call sorting
• Searching algorithms
Sequential (Linear) Search
Ordered
Unordered
Binary Search
Linear Search
• Searching is the process of determining whether or not a
given value exists in a data structure or a storage media.
• Two searching methods on one-dimensional arrays:
linear search and binary search.
• The linear (or sequential) search algorithm on an array is:
– Sequentially scan the array, comparing each array item with the searched value.
– If a match is found; return the index of the matched element; otherwise return –
1.
*
Sequential Search on an Ordered File
Basic algorithm:
Procedure ls(l,n,k)
{
i=0;
while ((i<n) and (k>l[i]))
{
i=i+1;
}
if (k==l[i])
printf(“key is found”);
else
printf(“key is not found”);
}
Example: 16,18,56,78,89,90,100
search element:78
Sequential Search on an Unordered File
Basic algorithm:
Procedure ls(l,n,k)
{
i=0;
while ((i<n) and (l[i]!= k))
{
i=i+1;
}
if (k==l[i])
printf(“key is found”);
else
printf(“key is not found”);
}
Example: 23,14,98,45,67,53
search element: 53
Linear Search
#include <stdio.h> scanf("%d", &k);
int main() for (i = 0; i < n; i++)
{ int a[10], k, i, n; { if (a[i] == k) /* if required
printf("Enter the number of element found */
elements in array\n"); { printf("%d is present at
scanf("%d",&n); location %d.\n", k, i+1);
printf("Enter the numbers:”); break; } }
for (i = 0; i < n; i++) if (i == n)
scanf("%d", &a[i]); printf("%d is not present in
printf("Enter the number to array.\n", k);
search\n"); return 0; }
Search Algorithms
How a Binary Search Works
a)pass by value
b)pass by reference
c)declare it in a global array variable.
CONTD..
a)O(log n) base 2
b)O(log n) base 10
c)O(n)
d)O(n x n)
Classification of sorting
Internal sorting
•The data resides in the MM of the computer.
Disadvantages
•Amount of MM available is smaller than amount of
data
•The MM is a volatile device
External sorting
•The data is stored on the secondary storage device
Various sorting techniques
• Internal sorting
– Bubble sort
– Insertion sort
– Shell sort
– Selection sort
– Merge sort
– Radix sort
– Heap sort
– Quick sort
• External sorting
– Multiway merge
– Polyphase merge
– Replacement selection
INSERTION SORTING
• Real life example:
– An example of an insertion sort occurs in
everyday life while playing cards.
– To sort the cards in your hand you extract a card,
shift the remaining cards, and then insert the
extracted card in the correct place.
– This process is repeated until all the cards are in
the correct sequence.
Insertion Sort: Idea
1. We have two group of items:
– sorted group, and
– unsorted group
2. Initially, all items in the unsorted group and the sorted group
is empty.
– We assume that items in the unsorted group unsorted.
– We have to keep items in the sorted group sorted.
3. Pick any item from, then insert the item at the right position
in the sorted group to maintain sorted property.
4. Repeat the process until the unsorted group becomes empty.
Insertion Sort: Example
40 2 1 43 3 65 0 -1 58 3 42 4
2 40 1 43 3 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
Insertion Sort: Example
1 2 40 43 3 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 -1 58 3 42 4
Insertion Sort: Example
1 2 3 40 43 65 0 -1 58 3 42 4
0 1 2 3 40 43 65 -1 58 3 42 4
-1
0 1
0 2
1 3
2 40
3 40
43 43
65 65 58 3 42 4
Insertion Sort: Example
-1
0 1
0 2
1 3
2 40
3 40
43 43
65 58 65 3 42 4
-1
0 1
0 2
1 3
2 40
3 43
3 40
65 43
43 58 58
65 65 42 4
-1
0 1
0 2
1 3
2 40
3 43
3 40
65 42 43
43 65 58 65 4
-1
0 1
0 2
1 3
2 40
3 43
3 43
65
4 40
42 42
65 43
43 58 58
65 65
Algorithm
for( i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while( j>=0 && a[j]>temp)
{
a[j+1] = a[j];
j=j-1;
}
a[j+1]=temp;
}
Example: 70, 30, 20, 50, 60, 10
Insertion Sort: Analysis
• Running time analysis:
– Worst case: O(N2)
– Best case: O(N)
FA Questions
1. ____________an operation that segregates items into groups
according to specified criterion.
a. Sorting b. Searching c. Inserting d. Deleting
2. Sorting can be done in _________ is called internal sorting.
a. main memory b. secondary memory c. auxiliary memory
d. disks
3. __________ is an internal sorting algorithm
a. poly-phase merge sort b. replacement selection
c. insertion sort d. 2-way merge sort
Shell Sort -General Description
– Divides an array into several smaller non-contiguous
segments
– The distance between successive elements in one
segment is called a gap.
– Each segment is sorted within itself using insertion sort.
– Then resegment into larger segments (smaller gaps)
and repeat sort.
– Continue until only one segment (gap = 1) - final sort
finishes array sorting.
Algorithm
for( int k = n/2; k > 0; k /= 2 )
for( i = k; i<n; i++ )
{
t = a[i];
for( j = i; j >= k; j -= k)
if( t < a[j-k] )
a[j] = a[j-k];
else
break;
a[j] = t;
}
Example: 25, 57, 48, 37, 12, 92, 89, 33
Assessment
1.Diminishing increment sort is a ____________
sort.
a) Bubble b) insertion c) selection d) shell
Ans:d
2.Shell sort is a _____ sorting.
a) Internal b) external c) searching d) merging
Ans:a
Divide and Conquer
• Recursive in structure
– Divide the problem into sub-problems that are
similar to the original but smaller in size
– Conquer the sub-problems by solving them
recursively. If they are small enough, just solve
them in a straightforward manner.
– Combine the solutions to create a solution to
the original problem
An Example: Merge Sort
Sorting Problem: Sort a sequence of n elements into
non-decreasing order.
• Divide: Divide the n-element sequence to be
sorted into two subsequences of n/2 elements
each
• Conquer: Sort the two subsequences recursively
using merge sort.
• Combine: Merge the two sorted subsequences to
produce the sorted answer.
Mergesort
•Mergesort (divide-and-conquer)
– Divide array into two halves.
A L G O R I T H M S
A L G O R I T H M S divide
Mergesort
•Mergesort (divide-and-conquer)
– Divide array into two halves.
– Recursively sort each half.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
Mergesort
•Mergesort (divide-and-conquer)
– Divide array into two halves.
– Recursively sort each half.
– Merge two halves to make sorted whole.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
A G H I L M O R S T merge
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
smallest smallest
A G L O R H I M S T
A auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
smallest smallest
A G L O R H I M S T
A G auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
smallest smallest
A G L O R H I M S T
A G H auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O R auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S T auxiliary array
Algorithm
i=i+1; k=k+1; }
if (l<h)
{ else // smaller element is in right
m=l+h/2; {
sort(a,l,m); t[k]=a[j];j=j+1; k=k+1; }
sort(a,m+1,h); }
combine(a,l,m,h); //copy remaining elements of left to temp
} while(i<=m)
combine(a[n],l,m,h) {
{ t[k]=a[i]; i=i+1; k=k+1;
k=l; //index for temp }
i=l;// index for left sublist // copy remaining elements of right to temp
j=m+1; //index for right sublist while (j<=h)
while(i<=m && j<= h) {
{ t[k]=a[j];
if)a[i]<=a[j]) // smaller element is in left j=j+1;
{ k=k+1;
t[k]=a[i]; }
Analysis of Merge Sort
• Running time T(n) of Merge Sort:
• Divide: computing the middle takes (1)
• Conquer: solving 2 sub problems takes 2T(n/2)
• Combine: merging n elements takes (n)
• Total:
T(n) = (1) if n = 1
T(n) = 2T(n/2) + (n) if n > 1
T(n) = (n lg n)
Comp 122
Do by yourself
• 10,3,54,23,25,5,75,1,453,36,68,51
• 10,5,7,6,1,4,8,3,2,9
Assessment