DS Notes New 1.1
DS Notes New 1.1
Introduction
• Data refers to any type of information that a computer can process or store.
• Computers process data to perform tasks, make decisions, do calculations, and solve
problems.
• The classification of data is based on its nature, size, and how it will be used in a
program.
• It can be in different forms such as:
o Numbers (e.g., 10, 3.14)
o Text (e.g., "Hello")
o Images
o Audio , Videos
o Sensor readings, etc.
Data Types
• A data type defines the kind of data that can be stored in a variable.
• It also decides:
o What operations can be performed on the data.
o How much memory the data will occupy.
1. Primitive Data Types
These are the most basic types of data and are directly supported by programming languages.
They are used to represent simple values.
Common Primitive Data Types:
• Integer (int)
o Represents whole numbers (positive, negative, or zero).
o Example: 5, -7, 42
• Floating-point (float or double)
o Used to represent decimal or real numbers.
o Example: 3.14, -7.25, 0.001
• Character (char)
o Represents a single character such as a letter, digit, or symbol.
o Example: 'A', 'b', '1'
• Boolean (bool)
o Represents truth values: either true or false.
o Example: true, false
• String (str)
o Represents a sequence of characters (text).
o Example: "Hello", "123", "Good morning"
Data Structure
What is a Data Structure?
It is a way to organize and store data in a computer so that it can be used efficiently.
They are designed in a way that makes accessing and processing of the data a little easier and
simpler.
Examples:
• A list of names can be stored in a straight line like an array.
• A family tree can be arranged in a branching structure like a tree.
• A friend network (like on social media) can be shown using connected points like a
graph.
Characteristics of Algorithms
• Well-Defined Steps
Each instruction must be clear and simple.
There should be no confusion about what to do at any step.
• Finite
The algorithm must stop after a certain number of steps.
It should not run forever or go into an endless loop.
• Input
An algorithm should take some input values to work with.
Input is the starting data for the algorithm.
• Output
After completing the steps, the algorithm should give an output.
Output is the result after processing the input.
• Effectiveness
All steps in the algorithm must be basic.
Each step should be performed by a person or a computer easily.
• General
The algorithm should work for a variety of inputs.
It should solve a group of similar problems, not just one.
Formats of an Algorithm
An algorithm is a step-by-step way to solve a problem. To make it easy to read and
understand, we can describe an algorithm in different formats. Two common ways are:
1. Using Natural Language
This means writing the algorithm in plain English or simple words.
• We write each step clearly and in the correct order.
• There should be no difficult words or confusing terms.
• The goal is to help anyone (even beginners) understand the logic.
2. Using Flowcharts
A flowchart is a visual way of showing the steps of an algorithm using shapes and arrows.
• Oval shape is used for "Start" and "End".
• Rectangle is used for actions like taking input or showing output.
• Diamond is used for decisions (like if/else conditions).
• Arrows show the direction of flow from one step to another.
Algorithm Cases
1. Best Case
o This is the fastest the algorithm can finish.
o It happens when everything goes perfectly.
o Example: In a search, if the item is found at the very beginning of the list.
2. Worst Case
o This is the slowest the algorithm can run.
o It shows the maximum time it will take.
o Example: If the item is not found or is at the end of the list.
3. Average Case
o This is the expected time the algorithm usually takes for random inputs.
o It gives a realistic idea of performance in normal situations.
Trade-Off
• A trade-off means giving up one thing to gain another.
• In computer science, it usually involves choosing between time and space (memory)
when designing algorithms or systems.
• We can't always have both the fastest and the most memory-efficient program, so we
have to balance the two depending on the situation.
• Example: If you want your program to run faster, it might need more memory. If you
want to save memory, it might run slower.
Time and Space Trade-Off
• Time refers to how fast an algorithm runs.
• Space refers to how much memory it uses.
• Computers have limited resources.
• When writing or designing an algorithm, we must decide what is more important:
o Speed of execution, or
o Efficient use of memory
• Optimizing for one might increase the need for the other.
Real-World Examples
1. Caching (Fast but memory-heavy)
• Keeping data in memory for quick access later.
• Saves time by not recalculating or refetching data.
• Example: Browsers store (cache) images and pages so the next time you open them,
they load instantly.