LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
1.1 Objectives:
To provide a detailed overview of data structures and their classifications.
To illustrate the concepts of algorithm complexity and execution time.
To explain the role and types of arrays in solving complex programming problems.
1.2 Learning Outcomes:
Grasp the fundamental concepts of data structures and their classification.
Understand how to analyze and compare algorithm efficiency based on execution time.
Learn to implement and manipulate arrays effectively in programming.
Program : B.Sc. ELECTRICAL ENGINEERING
Course Name : DATA STRUCTURES AND ALGORITHMS Course Code: EC-304
Student Name : Class Roll:
Session : 2024-2028 Semester: 03
Course Instructor : DR. KHAWAJA TAHIR MEHMOOD
Lab Engineer : ENGR. MUHAMMAD NOOR DANISH
Lab Performed In : NETWORKING & SIMULATION LAB. (EED)
________________________________________
COURSE INSTRUCTOR / LAB ENGINEER
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
1.3 DATA STRUCTURES
Data may be organized in many different ways: the logical or mathematical model of a particular
organization of data is called a data structure. The choice of a particular data model depends on two
considerations. First, it must be rich enough in structure to mirror the actual relationships of the
data in the real world. On the other hand, the structure should be simple enough that one can
effectively process the data when necessary.
1.3.1 Classification of Data Structures
Data structures are generally classified into primitive and non-primitive data structures. Basic data
types such as integer, real, character and Boolean are known as primitive data structures. These
data types consist of characters that cannot be divided, and hence they are also called simple data
types. The simplest example of non-primitive data structure is the processing of complex numbers.
Very few computers are capable of doing arithmetic on complex numbers. Linked-lists, stacks,
queues, trees and graphs are examples of non-primitive data structures.
Figure 1. 1 classification of data structures
1.3.2 Execution Time Cases
There are three cases which are usually used to compare various data structure's execution time in
a relative manner.
Worst Case − this is the scenario where a particular data structure operation takes maximum
time it can take. If an operation's worst-case time is ƒ(n) then this operation will not take more
than ƒ(n) time where ƒ(n) represents function of n.
Average Case − this is the scenario depicting the average execution time of an operation of a
data structure. If an operation takes ƒ(n) time in execution, then m operations will take mƒ(n)
time.
Best Case − this is the scenario depicting the least possible execution time of an operation
of a data structure. If an operation takes ƒ(n) time in execution, then the actual operation may
take time as the random number which would be maximum as ƒ(n).
1.4 Algorithms
In computer programming terms, an algorithm is a set of well-defined instructions to solve a
particular problem. It takes a set of input(s) and produces the desired output. For example,
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
Algorithm 1: Add two numbers entered by the user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Algorithm 2: Find the largest number among three numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Algorithm 3: Find Roots of a Quadratic Equation ax2 + bx + c = 0
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part rp ← -b/2a
ip ← √(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
1.4.1 Qualities of a Good Algorithm
Input and output should be defined precisely.
Each step in the algorithm should be clear and unambiguous.
Algorithms should be most effective among many different ways to solve a problem.
An algorithm shouldn't include computer code. Instead, the algorithm should be written in
such a way that it can be used in different programming languages.
1.4.2 Algorithm Complexity
Suppose X is an algorithm and n is the size of input data, the time and space used by the algorithm
X are the two main factors, which decide the efficiency of X.
Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
Space Factor − Space is measured by counting the maximum memory space required by the
algorithm.
The complexity of an algorithm f(n) gives the running time and/or the storage space required by the
algorithm in terms of n as the size of input data.
1.5 Array in Data Structures
Arrays in data structures help solve some high-level problems like the "longest consecutive
subsequence" program or some easy tasks like arranging the same things in ascending order. The
concept is to collect many objects of the same kind.
1.5.1 What Are Arrays in Data Structures?
An array is a linear data structure that collects elements of the same data type and stores them in
contiguous and adjacent memory locations. Arrays work on an index system starting from 0 to (n-1),
where n is the size of the array.
Figure 1. 2 what is an Array
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
1.5.2 Why Do You Need an Array in Data Structures?
Figure 1. 3 why we need array?
Let's suppose a class consists of ten students, and the class has to publish their results. If you had
declared all ten variables individually, it would be challenging to manipulate and maintain the data.
If more students were to join, it would become more difficult to declare all the variables and keep
track of it. To overcome this problem, arrays came into the picture.
1.5.3 What Are the Types of Arrays?
There are majorly two types of arrays, they are:
1.5.3.1 One-Dimensional Arrays:
Figure 1. 4 one dimensional array
You can imagine a 1d array as a row, where elements are stored one after another.
1.5.3.2 Two-Dimensional Arrays:
Figure 1. 5 Two-dimensional array
You can imagine it like a table where each cell contains elements.
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
1.5.3.3 Three-Dimensional Arrays:
Figure 1. 6 Three-dimensional array
1.5.4 How Do You Declare an Array?
Figure 1. 7 how to declare array
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
1.6 Review Question:
Q 1.1: Why header files are included in any program?
Q 1.2: When we need array in any program?
Q 1.3: How we can create array which can store value of different data types?
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
Q 1.4: Write an algorithm to print the odd numbers of first N natural numbers?
Algorithms:
Q 1.5: Write a function sub-algorithm to find the exponential power if b represents base and p its
power?
Algorithms:
Q 1.6: Write an algorithm and program in C++ to create a 3x3 matrix by using for loops and display its
values?
Algorithms:
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
Code:
Output:
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN
LABORATORY MANUAL DATA STRUCTURES AND ALGORITHMS [EC - 304]
1.7 Conclusion
DEPARTMENT OF ELECTRICAL ENGINEERING BZU MULTAN