Algorithm&Data Structures
Algorithm&Data Structures
PROGRAMMING FUNDAMENTALS
• An algorithm represents the thinking process for solving a problem in an abstract yet
precise way, rather than the answer itself.
#include <stdio.h>
int main() {
int n = 5;
int result = n * n;
printf("The square of %d is %d", n, result);
return 0;
}
Compute the average of three numbers
• Step 1:Start
• Step 2:Declare variables num1,num2,num3 and
average(num1,num2 and num3 holds the value of three
numbers)
• Step 3:Read values of num1,num2 and num3
• Step 4: Find the average using the formula : Average =
(num1+num2+num3)/3
• Step 5:Display average
• Step 6:End
#include <stdio.h>
int main() {
// Step 2: Declare variables
float num1, num2, num3, average;
// Step 3: Read values of num1, num2, and num3
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);
// Step 4: Calculate average
average = (num1 + num2 + num3) / 3;
// Step 5: Display average
printf("Average of %f, %f, and %f is: %f\n", num1, num2, num3,
average);
return 0;
}
Modification
Read
num1,num2,
num3d
Average=
(Num1+num2+num3)/3
Display
average
End
#include <stdio.h>
int main() {
int num;
// Prompt user to enter a number
printf("Enter an integer: ");
scanf("%d", &num);
// Check if the number is even or odd
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
#include <stdio.h> } else {
int main() { if (num2 > num3) {
// Step 2: Declare variables printf("num2 is the
largest.\n");
float num1, num2, num3;
} else {
// Step 3: Input num1, num2, and
num3 printf("num3 is the
largest.\n");
printf("Enter three numbers: ");
}
scanf("%f %f %f", &num1, &num2,
&num3); }
// Step 4: Check and display the
largest number
// Step 6: End
if (num1 > num2 && num1 >
num3) { return 0;
f(n)=O(g(n))
Big-O gives the upper bound of a function
O(g(n)) = { f(n): there exist positive constants c and
n0
such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }
• The above expression can be described as a function f(n) belongs to
the set O(g(n)) if there exists a positive constant c such that it lies
between 0 and cg(n), for sufficiently large n.
• For any value of n, the running time of an algorithm does not cross the
time provided by O(g(n)).
• For any value of n, the minimum time required by the algorithm is given
by Omega Ω(g(n)).
Theta Notation (Θ-notation)
C2g(n)
f(n)=(g(n))
Theta bounds the function within constants factors
• For a function g(n), Θ(g(n)) is given by the relation:
• Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0
• such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
• The above expression can be described as a function f(n) belongs
to the set Θ(g(n)) if there exist positive constants c1 and c2 such
that it can be sandwiched between c1g(n) and c2g(n), for
sufficiently large n.
• If a function f(n) lies anywhere in between c1g(n) and c2g(n) for
all n ≥ n0, then f(n) is said to be asymptotically tight bound.
Greedy Algorithm
•A greedy algorithm is an approach for solving a problem by
selecting the best option available at the moment. It doesn't worry
whether the current best result will bring the overall optimal result.
• The algorithm never reverses the earlier decision even if the choice
is wrong. It works in a top-down approach.
• This algorithm may not produce the best result for all the problems.
It's because it always goes for the local best choice to produce the
global best result.
Divide and Conquer Algorithm
•A divide and conquer algorithm is a strategy of solving a
large problem by
• breaking the problem into smaller sub-problems
• solving the sub-problems, and
• combining them to get the desired output.
How Divide and Conquer Algorithms Work?
• Here are the steps involved:
• Divide: Divide the given problem into sub-problems using
recursion.
• Conquer:Solve the smaller sub-problems recursively. If the
subproblem is small enough, then solve it directly.
• Combine: Combine the solutions of the sub-problems that are part
of the recursive process to solve the actual problem.
example.
Here, sort an array using the divide and conquer approach (ie. merge
sort).
Again, divide each subpart recursively into two halves until
you get individual elements.
3. Now, combine the individual elements in a sorted manner.
Here, conquer and combine steps go side by side.
• Thefollowing computer algorithms are based on divide-
and-conquer programming approach-
• Merge Sort
• Quick Sort
• Binary Search
• Strassen’s Matrix Multiplication
• Closer pair(points)
Introduction to Data Structures
• Since the invention of computers, people have been using the term
"Data" to refer to Computer Information, either transmitted or
stored. However, there is data that exists in order types as well.
• Data can be numbers or texts written on a piece of paper, in the form
of bits and bytes stored inside the memory of electronic devices, or
facts stored within a person's mind. As the world started modernizing,
this data became a significant aspect of everyone's day-to-day life, and
various implementations allowed them to store it differently.
• Data is a collection of facts and figures or a set of values or values of a
specific format that refers to a single set of item values. The data items
are then classified into sub-items, which is the group of items that are
not known as the simple primary form of the item.
Let us consider an example where an employee name can be broken down into
three sub-items: First, Middle, and Last. However, an ID assigned to an employee
will generally be considered a single item.
What is Data Structure?
• Data Structure is a branch of Computer Science. The study of data
structure allows us to understand the organization of data and the
management of the data flow in order to increase the efficiency of any
process or program.
• Data Structure is a particular way of storing and organizing data in the
memory of the computer so that these data can easily be retrieved and
efficiently utilized in the future when required. The data can be managed
in various ways, like the logical or mathematical model for a specific
organization of data is known as a data structure.
The scope of a particular data model depends on two factors:
1. First, it must be loaded enough into the structure to reflect the definite
correlation of the data with a real-world object.
2.Second, the formation should be so straightforward that one can adapt to
process the data efficiently whenever necessary.
• Some examples of Data Structures are Arrays, Linked Lists, Stack, Queue,
Trees, etc. Data Structures are widely used in almost every aspect of
Computer Science, i.e., Compiler Design, Operating Systems, Graphics,
Artificial Intelligence, and many more.
• Data Structures are the main part of many Computer Science Algorithms
as they allow the programmers to manage the data in an effective way. It
plays a crucial role in improving the performance of a program or
software, as the main objective of the software is to store and retrieve the
user's data as fast as possible.
Why should we learn Data Structures?
1.Data
Structures and Algorithms are two of the key aspects of
Computer Science.
2.Data Structures allow us to organize and store data, whereas
Algorithms allow us to process that data meaningfully.
3.Learning Data Structures and Algorithms will help us become
better Programmers.
4.We will be able to write code that is more effective and reliable.
5.We will also be able to solve problems more quickly and
efficiently.
Basic Terminologies related to Data Structures
• Data Structures are the building blocks of any software or program. Selecting the
suitable data structure for a program is an extremely challenging task for a
programmer.
• The following are some fundamental terminologies used whenever the data
structures are involved:
1.Data: We can define data as an elementary value or a collection of values. For
example, the Employee's name and ID are the data related to the Employee.
2.Data Items: A Single unit of value is known as Data Item.
3.Group Items: Data Items that have subordinate data items are known as Group
Items. For example, an employee's name can have a first, middle, and last name.
4.Elementary Items: Data Items that are unable to divide into sub-items are known
as Elementary Items. For example, the ID of an Employee.
5.Entity and Attribute: A class of certain objects is represented by an Entity. It
consists of different Attributes. Each Attribute symbolizes the specific property of
that Entity.
Types of Data Structure
• Basically, data structures are divided into two categories:
• Linear data structure
• Non-linear data structure
non
Primitive Data Structures
1.Primitive Data Structures are the data structures
consisting of the numbers and the characters that come in-
built into programs.
2.These data structures can be manipulated or operated directly
by machine-level instructions.
3.Basic data types like Integer, Float, Character,
and Boolean come under the Primitive Data Structures.
4.These data types are also called Simple data types, as they
contain characters that can't be divided further
Non-Primitive Data Structures
1.Non-Primitive Data Structures are those data structures
derived from Primitive Data Structures.
2.Thesedata structures can't be manipulated or operated directly
by machine-level instructions.
3.The focus of these data structures is on forming a set of data
elements that is either homogeneous (same data type)
or heterogeneous (different data types).
4.Based on the structure and arrangement of data, we can divide
these data structures into two sub-categories -
1. Linear Data Structures
2. Non-Linear Data Structures
Linear data structures