0% found this document useful (0 votes)
26 views

Unit-1 - Introduction To DS

Data structure

Uploaded by

vanshoad0
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Unit-1 - Introduction To DS

Data structure

Uploaded by

vanshoad0
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Data Structures (DS)

GTU # 3130702

Unit-1
Introduction to
Data Structure

Prepared By:

Tushar Jakhaniya
 Outline
Looping
 Data Management concepts
 Data types
• Primitive
• Non-primitive
 Types of Data Structures
• Linear Data Structures
• Non Linear Data Structures
 Performance Analysis and Measurement
 Time analysis of algorithms
 Space analysis of algorithms
What is Data?
 Data is the basic fact or entity that is utilized in calculation or manipulation.
 There are two different types of data Numeric data and Alphanumeric data.
 When a programmer collects such type of data for processing, he would require to store
them in computer’s main memory.
 The process of storing data items in computer’s main memory is called representation.
 Data to be processed must be organized in a particular fashion, these organization leads to
structuring of data, and hence the mission to study the Data Structures.
What is Data Structure?
 Data Structure is a representation of the logical relationship existing between individual
elements of data.
 In other words, a data structure is a way of organizing all data items that considers not only
the elements stored but also their relationship to each other.
 We can also define data structure as a mathematical or logical model of a particular
organization of data items.
 Data Structure mainly specifies the following four things
 Organization of Data
 Accessing Methods
 Degree of Associativity
 Processing alternatives for information
What is Data Structure? Cont..
 The representation of a particular data structure in the memory of a computer is called
Storage Structure.
 The storage structure representation in auxiliary memory is called as File Structure.

Algorithm Data Structure Program


Classification of Data Structure
Data Structure

Primitive Data Structure Non-Primitive Data Structure

Integer Characters Arrays Lists Files

Floating Pointers
Point Linear Non-linear
List List

Stack Queue Graphs Trees


Primitive / Non-primitive data structures
 Primitive data structures
 Primitive data structures are basic structures and are directly operated upon by machine instructions.
 Integers, floats, character and pointers are examples of primitive data structures.
 Non primitive data structure
 These are derived from primitive data structures.
 The non-primitive data structures emphasize on structuring of a group of homogeneous or heterogeneous
data items.
 Examples of Non-primitive data type are Array, Stack, and Queue.
Non primitive Data Structure
 Array: An array is a fixed-size sequenced collection of elements of the same data type.
 List: An ordered set containing variable number of elements is called as Lists.
 Example : Array, Stack, Queue, Linked List.

Array

List
Linear / Non-Linear data structure
 Linear data structures
 A data structure is said to be Linear, if its elements are connected in linear fashion by means of logically or
in sequence memory locations.
 Examples of Linear Data Structure are Stack, Queue, Array, Linked-list.
 Nonlinear data structures
 Nonlinear data structures are those data structure in which data items are not arranged in a sequence.
 Examples of Non-linear Data Structure are Tree and Graph.

Stack Queue Tree Graph


Algorithm
 The word Algorithm means ” A set of finite rules or instructions to be followed in
calculations or other problem-solving operations ”
Characteristic of Algorithm
 Finiteness:
 It should terminate after a finite number of steps.
 Definiteness:
 Each instruction should be clear and unambiguous.
 Input:
 Each algorithm is supplied with zero or more external quantities.
 Output:
 It produces at least one output.
 Effectiveness:
 It must be effective enough to solve in finite time with given resources.
Operations of Data Structure
 Create: It results in reserving memory for program elements.
 Destroy: It destroys memory space allocated for specified data structure.
 Selection: It deals with accessing a particular data within a data structure.
 Updation: It updates or modifies the data in the data structure.
 Searching: It finds the presence of desired data item in the list of data items.
 Sorting: It is a process of arranging all data items in a data structure in a particular order.
 Merging: It is a process of combining the data items of two different sorted list into a single
sorted list.
 Splitting: It is a process of partitioning single list to multiple list.
 Traversal: It is a process of visiting each and every node of a list in systematic manner.
Time and space complexity
 The valid algorithm takes a finite amount of time for execution. The time required by the
algorithm to solve given problem is called time complexity of the algorithm.

 Problem-solving using computer requires memory to hold temporary data or final result
while the program is in execution. The amount of memory required by the algorithm to solve
given problem is called space complexity of the algorithm.
 It is the amount of memory needed for the completion of an algorithm.
 To estimate the memory requirement we need to focus on two parts:
 (1) A fixed part: It is independent of the input size. It includes memory for instructions
(code), constants, variables, etc.
 (2) A variable part: It is dependent on the input size. It includes memory for recursion stack,
referenced variables, etc.
Time and space analysis of algorithms
 Sometimes, there are more than one way to solve a problem.
 We need to learn how to compare the performance of different algorithms and choose the best
one to solve a particular problem.
 While analyzing an algorithm, we mostly consider time complexity and space complexity.
 Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run
as a function of the length of the input.
 Space complexity of an algorithm quantifies the amount of space or memory taken by an
algorithm to run as a function of the length of the input.
 Time & space complexity depends on lots of things like hardware, operating system,
processors, etc.
 However, we don't consider any of these factors while analyzing the algorithm. We will only
consider the execution time of an algorithm.
Calculate Time Complexity of Algorithm
 Time Complexity is most commonly estimated by counting the number of elementary
functions performed by the algorithm .
 It is called as frequency count.
 For (i=0;i<=n;i++)
 For (j=0;J<=n;j++)
 Statement Frequency Count
X=x+1;
i=0 1
i<=n n+1
i++ n
J=0 n
J<=n n(n+1)
J++ N*N
X++ N*N
total 3N2+4N+2
Calculating Time Complexity
 Calculate Time Complexity of Sum of elements of List (One dimensional Array)

A is array, n is no of elements in
SumOfList(A,n) array
{ Line No of Times
Line 1 total = 0 1 1
Line 2 for i = 0 to n-1 2 n+1
Line 3 total = total + A[i] 3 n
Line 4 return total 4 1
}
TSumOfList = 1 + 2 (n+1) + 2n +
1 = 4n + 4 We can neglate constant 4
=n

Time complexity of given algorithm is n unit time


Asymptotic Notations
 Asymptotic Notations are programming languages that allow you to analyze an algorithm’s
running time by identifying its behavior as its input size grows.
 This is also referred to as an algorithm’s growth rate.
 There are mainly three asymptotic notations:
 Big-O Notation (O-notation)
 Omega Notation (Ω-notation)
 Theta Notation (Θ-notation)
Measurement of Complexity of an Algorithm

 Based on the above three notations of Time Complexity there are three cases to analyze an
algorithm:
 1. Worst Case Analysis
 the worst-case analysis, we calculate the upper bound on the running time of an algorithm.
We must know the case that causes a maximum number of operations to be executed.
 2. Best Case Analysis
 In the best-case analysis, we calculate the lower bound on the running time of an algorithm.
We must know the case that causes a minimum number of operations to be executed.
 3. Average Case Analysis
 n average case analysis, we take all possible inputs and calculate the computing time for all
of the inputs.
Big-O notation (O-notation):
 Big-O notation represents the upper bound of the running time of an algorithm. Therefore, it
gives the worst-case complexity of an algorithm.
 It is the most widely used notation for Asymptotic analysis. It specifies the upper bound of a
function. The maximum time required by an algorithm or the worst-case time complexity.
 If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a positive constant C and n0 such
that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0
Omega Notation (Ω-Notation):

 Omega notation represents the lower bound of the running time of an algorithm. Thus, it
provides the best case complexity of an algorithm.
 The execution time serves as a lower bound on the algorithm’s time complexity.
 It is defined as the condition that allows an algorithm to complete statement execution in the
shortest amount of time.
 Let g and f be the function from the set of natural numbers to itself. The function f is said to be Ω(g), if there is
a constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0
Theta Notation (Θ-Notation):

 Theta notation encloses the function from above and below. Since it represents the upper and
the lower bound of the running time of an algorithm, it is used for analyzing the average-
case complexity of an algorithm.
 Theta (Average Case) You add the running times for each possible input combination and
take the average in the average case.
 Let g and f be the function from the set of natural numbers to itself. The function f is said to be Θ(g), if there are
constants c1, c2 > 0 and a natural number n0 such that c1* g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0
Array
 Array is group of Elements having same datatype.
 Array is an ordered set which consist of fixed number of elements of
same data type.
 Array is a linear data structure.
 ฀Syntax:
 Datatype arrayname [arrraysize];
Conti.
 All the element of array shares the common name and they are distinguished with the help of
element number.
 ฀ Array elements are always stored sequentially or in linear fashion.
 ฀ In array, Insertion and deletion operation is slower and time consuming but searching and
sorting operation is faster.
 ฀ When array is declared and not initialized, it contains garbage values. If array is declared as
static, all elements are initialized to zero.

Array Name : Array Size : 5


arr
Row Major and Column Major Arrays
Conti.
Row Major Arrays
฀ If two dimensional array elements are stored sequentially row by row then it
is called Row Major Array.
Column 1 Column 2 Column 3
A[1][1]

A[1][2] Row
1
Row A[1][1] A[1][2] A[1][3]

1 A[1][3]

A[2][1]
A[2][1] A[2][2] A[2][3]
Row
2 A[2][2] Row
2
A[2][3]
Conti.
 Array :
 LOC(A[i]) = Base Address + W * (i)
 W= word Size
 Example : base address =2000, W=2
 LOC(A[i]) = Base Address + W * (i)
 Loc(A[2]) = 2000 + 2 * (2)
 = 2004
Row Major Array
Address of A [i , j ] cann be given as-

LOC( A [i , j ] ) = Base Address + W * [M *(i) + (j)]

Base address = address of first element in array.


W= word size(no. of byte occupied by each element)
N= no. of row in Array
M= no.of column in Array
Column Major Array
Address of A [i , j ] cann be given as-

LOC( A [i , j ] ) = Base Address + W * [N *(j) + (i)]

Base address = address of first element in array.


W= word size(no. of byte occupied by each element)
N= no. of row in Array
M= no.of column in Array
Array Operations
 Traversal
 Insertion
 Deletion
 Merging
 Searching
 Sorting
Traversal
 The method of processing each element in the array exatly once is known as Traversal.
 Algorithm:
 Step1 : START <- 0
 Step2 : Repeat step3 while (START < N)
 Step3 : Read A[START]
 START <- START + 1
Insertion
 Insertion operation is used to insert a new element at specific position in one dimensional
array.
 Algorithm :
 Step1 : TEMP <- N-1
 POS <- POS – 1
 Step2 : Repeat step 3 while TEMP >= POS
 Step3 : A[TEMP +1] <- A[TEMP]
 TEMP <- TEMP -1
 Step4 : A[POS] <- X
 Step5 : N <- N+1
Deletion
 Deletion is used to delete an element from specific position from array.
 Algorithm :
 Step1 : POS <- POS – 1
 TEMP < - POS
 Step2 : Return A[POS]
 Step3 : Repeat step 4 while TEMP >= N-1
 Step4 : A[TEMP] <- A[TEMP + 1]
 TEMP <- TEMP + 1
 Step5 : N <- N-1
Merging
 This opeartion is used to merge 2 array in third array.
 Algorithm :
 Step1 : I <- 0
 J <- 0
 K <- 0
 Step2 : Repeat step3 while I<N1
 Step3 : C[k] <- A[I]
 K <- K + 1
 I _<- I +1
 Step4 : Repeat Step4 While J<N2
 Step5 : C[K] <- B[J]
 K <- K+1
 J <- J +1
Searching
 Searching is process of finding particular element from array.
 1. Linear Search
 2. Binary Search

 1. Linear Search :
 this method is also known as Sequential Search. It find element from sorted as well as
unsorted list.
Index Element
1 11
2 33
3 55
4 22
5 44
Binary Search
 Binary Search only work with sorted list.
Binary search Algorithm:
 Algorithm:
 1. low <- 0
 high <- N-1
 2. repeat up to step 4 while low<=high
3.mid<- (low+high) /2
4.If X<A[mid] then
high<- mid-1
Else if X> A[mid] then
Low <- mid+1
Else
write "search successful"
return 1
5. Write "search is not sucessful"
Return 0
Thank You…

You might also like