Data Structure and Algorithms (4330704) Chap1
Data Structure and Algorithms (4330704) Chap1
and Algorithm
4330704
Rutvi S. Sheth
Unit-1 Basic Concepts of
Data Structures
CO1: Perform basic operations on arrays and
strings.
1.1 Data Structure Basic Concepts
4. Logical/Boolean:
• A logical data item is a primitive data structure that
• can assume the values of either “true” or “false”.
• Most commonly used logical operators Are AND, OR
and NOT.
5. Pointer:
• Pointer is a variable which store the address of
another variable.
• It provides faster insertion and deletion of elements.
1.2 TYPES OF DATA STRUCTURES
• Array:
• Set of data item using same data type.
• Set of fixed number of item. Insert and delete operation
are time consuming, searching and sorting are fast.
1.2 TYPES OF DATA STRUCTURES
• Stack:
• A stack is a linear list in which insertion and deletion
operations are performed at only one end of the list.
• It is LIFO (Last In First Out).
1.2 TYPES OF DATA STRUCTURES
• Linked list:
• A linked list is a collection of nodes.
• Each node has two fields:
i. Data/ Information
ii. Address, which contains the address of the next
node.
1.2 TYPES OF DATA STRUCTURES
5. Repeat Statement
• Repeat while(condition)
• Repeat thru step No for variable name=1,2,…N
6. Case Statement
Select case
Case value 1:
Case value 2:
Case value N:
Default:
1.5 KEY FEATURES OF ALGORITHM
• Step 3: [FINISHED]
Exit
1.6 Analysis Terms
• Complexity: Complexity of an algorithm is a function describing the efficiency of
the algorithm in terms of the amount of data.
• Time complexity: Time complexity is defined as the amount of time taken by an
algorithm to run, as a function of the length of the input.
• Space Complexity: It is a function describing the amount of memory an
algorithm takes in terms of the amount of input to the algorithm.
• Asymptotic Notation: Is a way of expressing the cost of an algorithm.
• Worst Case Complexity: Slowest time to complete with chosen input.
When the algorithm takes maximum no of steps on any instance of size n.
Example: In linear search, If searching element is at last position Or In
ascending order sorting, if all the elements are in descending order then
time taken by the algorithm is more.
1.6 Analysis Terms
• Best Case Complexity: Fastest time to complete with chosen input. When the
algorithm takes minimum no of steps on any instance of size n. Example: In
linear search, If searching element is at First position Or In ascending order
sorting, if all the elements are in ascending order then time taken by the
algorithm is less.
• Average case: In average case analysis, we take all possible inputs and
calculate the computing time for all of the inputs by calculating the total running
time and divide by the no of trials.
• Big oh notation (O): It is define as upper bound and upper bound on an
algorithm is the most amount of time required ( the worst case performance).
Big oh notation is used to describe asymptotic upper bound. In computer
science, big O notation is used to classify algorithms according to how their run
time or space requirements grow as the input size grows.
1.6 Analysis Terms
• Big Omega Notation (Ω): It is defined as lower bound and
lower bound on an algorithm is the least amount of time
required (the most efficient way possible, in other words best
case).Ω notation provides asymptotic lower bound.
• Big Theta Notation (Θ):It is define as tightest bound and
tightest bound is the best of all the worst case times that the
algorithm can take. In simple language, Big Theta(Θ) notation
specifies asymptotic bounds (both upper and lower) for a
function and provides the average time complexity of an
algorithm.
1.7 Array
• One dimensional array can be declared as int a[20].
• Storage Representation of One Dimensional Array:
Representation of an array in memory location is
shown below:
1.7 Array
•Location(Address) of A[i] in the array is calculated as:
• L0 is the starting(Base) address or address of first element
in the array.
• I is the array index.
• L is the starting index of an array.
• S is the size.
• If we want to find the address of A [2] then it is calculated
as:
• Loc (A [9]) = Base + (I – L0) * s
• = 2000 + (9-0) * 2
• = 2000 + 18
• = 2018
1.7 Array
• Two Dimensional Array (Storage Representation)
• Row-major Arrays: If two dimensional array element
store sequentially row by row then it is called Row
major array.
Row-major Arrays
• The address of element A[i, j] can be obtained by
evaluating expression:
• Loc (A [i, j]) = Base + ((i – L1) * n + (j – L2))* s
• i is the row index.
• j is the column index.
• m is the no. of rows .
• n is the no of column.
• s is size.
• L1 is the lower bound for row.
• L2 is the lower bound for column.
Row-major Arrays
• A[1,2] in a[2][3] is calculated as:
• A [1, 2] = Base + ((i - L1) * n + (j - L2))* s
• Here, n=3, m=2, i=1, j=2,
• Base = 3000
=3000 + ((1 - 0) * 3 + (2 - 0))*2
=3000 + (3+2)*2
=3000 + 10
=3010
Column-major Arrays
• Column-major Arrays: If two dimensional array
elements store sequentially column by column then it is
called Column major array.
• Example: A two dimensional array consist of two rows
and three columns is stored sequentially in row major
order as:
Column-major Arrays
• The address of element A[i, j] can be obtained by evaluating
expression:
• Loc (A [i, j]) = Base + ((j – L2) * m + (i – L1) )* s
• i is the row index.
• j is the column index.
• m is the no. of rows .
• n is the no of column.
• s is size.
• L1 is the lower bound for row.
• L2 is the lower bound for column.
Column-major Arrays
• A[1,2] in a[2][3] is calculated as:
• A [1, 2] = Base + ((j – L2) * m + (i – L1))* s
• Here, n=3, m=2, i=1, j=2,
• Base = 3000
=3000 + ((2 - 0) * 2 + (1 - 0))*2
=3000 + (4+1)*2
=3000 + 10
=3010
1.8 Overview of different array operations
• Insertion: Add a New data element into the list.
• Deletion: Remove an element from the list.
• Searching: Finding the specific location for specific
data.
• Sorting: Arrange the list in Ascending or Descending
order.
• Merging: Combine the two lists into single list.
• Traversal: Accessing each data element once in such a
way that all the data elements are processed.
1.9 Searching an element into an array
• Searching: Search is used to find whether the desired
data is present in set of data or not. If it is present,
the search operation provides us its position. There
are two types of searching technique.
1. Linear search / Sequential Search
2. Binary Search
Linear Search
• This method is used to find out element in an unordered
list.
• Suppose the list L consist of N elements and we want to
find the element from the list.
• It sequentially compares the given value with each value
in list of values one by one from start to end until value is
found or list is ended.
• If the value is found, the position of the value is returned.
• https://ds1-iiith.vlabs.ac.in/exp/unsorted-arrays/linear-sea
rch/linear_search_demo.html
(For Visual Learning)
1.9 Searching an element into an array
• SEQENTIAL_SEARCH (L, N, 3. [Comparison]
X): If (L[i]= X ) Then
• These function searches the i. Flag ← 0
list L consist of N elements
for the value of X.
ii. Write(“ Search is
Successful”)
• L: Represents the list of
element. iii. Write (“ Value found at
• N: Represents the no of location: i +1”)
elements in the list 4. If (flag=1) then
• X: Represent the value to be
search in the list Write (“Search is
1. [Initialization] unsuccessful”)
• Binary Search