0% found this document useful (0 votes)
14 views11 pages

DS Notes New 1.1

The document provides an overview of data types, data structures, and algorithms, explaining their importance and classifications. It covers primitive, composite, abstract, and user-defined data types, as well as linear and non-linear data structures, and common operations on them. Additionally, it discusses algorithm characteristics, complexity, and trade-offs between time and space in algorithm design.

Uploaded by

Majhe Guruji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

DS Notes New 1.1

The document provides an overview of data types, data structures, and algorithms, explaining their importance and classifications. It covers primitive, composite, abstract, and user-defined data types, as well as linear and non-linear data structures, and common operations on them. Additionally, it discusses algorithm characteristics, complexity, and trade-offs between time and space in algorithm design.

Uploaded by

Majhe Guruji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Data

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.

Why Do We Need a Data Type?


• Imagine a universal data type that can hold any value like character, integer, float, or
complex number.
• Using such a universal data type creates major problem that large amount of memory
would be used even for storing small data.
• If use a universal data type, the system becomes very complex and inefficient.
• Different data types solve this problem by clearly defining:
o How the data is stored in memory.
o How the data is read and interpreted.
o What operations can be done on the data.
• Data types help in:
o Saving memory by using only what is needed.
o Making the interpretation of data easier and accurate.
o Avoiding confusion and errors during processing.

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"

2. Composite Data Types


Composite data types are created by combining primitive data types. They allow you to group
multiple values together, and are commonly found in high-level programming languages
Common Composite Data Types:
• Array
o A collection of elements of the same data type stored in contiguous memory
locations. It is usually fixed in size.
o Example: [1, 2, 3, 4] (array of integers)
• List
o A collection that can store elements of different data types.
o Lists are dynamic—they can grow or shrink in size.
o Example: ['apple', 3.5, 12] (contains string, float, and integer)
• Tuple
o Similar to a list but immutable (cannot be changed after creation).
o Can hold different data types.
o Example: (1, 'hello', 3.14)
• Set
o A collection of unique elements (no duplicates allowed).
o Unordered and mutable.
o Example: {1, 2, 3}
• Dictionary
o A collection of key-value pairs, where each key maps to a value.
o Keys are unique.
o Example: {'name': 'John', 'age': 30}

3. Abstract Data Types (ADTs)


An Abstract Data Type (ADT) is a data type that is defined by the operations that can be
performed on it, rather than how it is implemented.
Common Abstract Data Types:
• Stack
o A collection where the last element added is the first one removed (LIFO –
Last In, First Out).
o Example: Like a stack of books—only the top book can be removed or added.
• Queue
o A collection where the first element added is the first one removed (FIFO –
First In, First Out).
o Example: Like a line of people waiting at a counter.
• Linked List
o A linear collection of nodes where each node contains data and a
pointer/reference to the next node.
o Example: A train of connected compartments where each has a link to the
next.
• Tree
o A hierarchical structure where each element (node) may have one parent and
multiple children.
o Example: A family tree or a file system directory.
• Graph
o A set of nodes (vertices) connected by edges.
o Example: A social network—users are nodes, and friendships are edges.

4. User-defined Data Types


User-defined data types are custom data structures that programmers create by combining
existing data types (primitive or composite).
They help to represent things more clearly and make the code easier to read and manage.
These types are supported in many languages like C, C++, Java, Python, etc.
Common User-defined Data Types:
• Structure (struct)
o A collection of variables of different data types grouped under a single name.
o Useful for representing real-world objects like a student, employee, etc.

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.

Why are Data Structures Important?


• Efficiency
They help to work with large amounts of data quickly and save time and memory.
• Good Organization
Data is stored in a proper way, which makes it easy to find and use when needed.
• Better Speed and Performance
Programs run faster when you use the correct method (algorithm) and data structure.
• Saves Memory
Some data structures use memory only when needed, which saves space.
• Manages Data Smartly
It helps to store and get data quickly.
• Improves Thinking Skills
Learning DS helps you think in a clear and logical way.
• Follows Best Practices
Using DS helps you write code that is easy to understand, fix, and improve later

Types of Data Structures


Data structures are mainly divided into two types:
1. Primitive Data Structures
These are the basic or built-in types that the computer understands directly.
They store simple values.
Examples:
• Integer – Whole numbers (e.g., 5, -10)
• Float – Decimal numbers (e.g., 3.14, 0.01)
• Character – Single letters or symbols (e.g., 'A', 'b')
• Boolean – Only two values: true or false

2. Non-Primitive Data Structures


These are more complex structures that can store multiple values.
They are divided into two types: Linear and Non-Linear.

A) Linear Data Structures


In linear data structures, data is stored one after another in a sequence.
1. Arrays
• A group of elements stored in continuous memory locations.
• All elements are of the same type.
• Example: A list of marks of students: [75, 80, 90]
2. Linked Lists
• Made of nodes where each node contains data and a link to the next node.
• Example: Train compartments linked one after another.
3. Stacks
• Works on LIFO: Last In, First Out.
• Example: A stack of plates – the last plate added is removed first.
4. Queues
• Works on FIFO: First In, First Out.
• Example: People standing in a line – the first person goes first.

B) Non-Linear Data Structures


In non-linear structures, data is stored in a hierarchical or interconnected way.
1. Trees
• Data is arranged in levels, starting from the root.
• Example: A family tree or folder structure in a computer.
2. Graphs
• A collection of nodes (also called vertices) connected by edges.
• Example: Social networks like Facebook – people are nodes, friendships are
connections.

Common Operations on Data Structures


• Insertion – Adding a new element to the data structure.
Example: Add 40 to the list [10, 20, 30] → [10, 20, 30, 40]
• Deletion – Removing an existing element from the data structure.
Example: Remove 20 from [10, 20, 30] → [10, 30]
• Traversal – Visiting each element one by one to read or process it.
Example: Print all items in [1, 2, 3]
• Searching – Finding a particular element in the data structure.
Example: Find 5 in [2, 5, 7] → Found at position 2
• Sorting – Arranging elements in order (ascending or descending).
Example: Sort [3, 1, 2] → [1, 2, 3]
• Merging – Joining two data structures into one.
Example: Merge [1, 3] and [2, 4] → [1, 2, 3, 4]
• Splitting – Dividing a data structure into parts.
Example: Split [1, 2, 3, 4] into [1, 2] and [3, 4]
• Updating – Changing the value of an existing element.
Example: Change 6 to 10 in [5, 6, 7] → [5, 10, 7]
Algorithm
What is an Algorithm?
• An algorithm is a step-by-step method to solve a problem.
• It helps to convert a problem into a series of instructions.
• These instructions can then be written in a programming language.
• The final program is run by the computer to get the result or solution.

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.

Different Approaches for Designing Algorithms


When designing an algorithm, we usually follow one of two main approaches: Top-Down or
Bottom-Up.
1. Top-Down Approach
• This approach begins with the main problem and breaks it into smaller sub-problems.
• Step 1: Understand the full problem clearly.
• Step 2: Divide it into smaller parts or modules.
• Step 3: Keep breaking the parts down until each one is simple enough to solve easily.
• Step 4: Finally, write code for each small part and combine them to solve the main
problem.
• This approach focuses on the overall structure first, then goes into detail.
2. Bottom-Up Approach
• In this method, we start by solving the simplest components first, and then build up to
form the full solution.
• Step 1: Identify the basic building blocks of the problem.
• Step 2: Develop and test these small parts first.
• Step 3: Gradually combine them step-by-step.
• Step 4: Keep assembling until the full solution is ready.
• This approach focuses on solving small tasks first and gradually building the full
solution.
Algorithm Complexity
Algorithm complexity tells us how much time and memory an algorithm needs to solve a
problem. It helps us understand how efficient or slow an algorithm is when dealing with
small or large amounts of data.
Time Complexity
• Time Complexity tells us how much time an algorithm takes to run, based on the size
of the input.
• It helps us understand how fast or slow a program will be when the input becomes
large.
• Time complexity is calculated by counting the number of basic steps (operations) the
algorithm performs.
• It does not measure actual time in seconds, but how the time grows as input size
increases.
• Time complexity is important to choose the best algorithm, especially when working
with big data or limited processing time.
Common Notations Used:
1. Big O Notation (O) –
Describes the worst-case time an algorithm can take.
2. Omega Notation (Ω) –
Describes the best-case time, when everything goes well.
3. Theta Notation (Θ) –
Describes the average or exact time in most cases.
Common Types of Time Complexities:
• O(1) – Constant Time
Time does not depend on input size.
• O(log n) – Logarithmic Time
Time increases slowly even if input grows.
O(n²) – Quadratic Time
Time grows very fast with input.
• O(2ⁿ) – Exponential Time
Time doubles with every new input.
Very slow for large inputs.
2. Space Complexity
• Space Complexity tells us how much memory (RAM) an algorithm needs to run,
based on the input size (n).
• It includes:
o The memory used by the input data.
o The memory used for temporary variables, loops, recursion, and function calls
during execution.
• It is important to measure space complexity when you are working with limited
memory devices like mobile phones, embedded systems, or IoT devices.
• Just like time complexity, we write space complexity in terms of n to understand how
memory usage grows with input size.
• Optimizing space usage helps programs run faster and prevents out-of-memory errors.

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.

1. More Time, Less Space


• In this case, you write a program that uses less memory, but it needs to work harder or
repeat steps, so it takes more time.
• Useful when memory is limited.

2. More Space, Less Time


• Here, you use more memory to make the program run faster.
• Useful when you want quick results and memory is not a problem

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.

2. Compression (Saves space, needs time)


• Reducing file size using algorithms.
• Saves storage space.
• But: Takes time to compress and decompress files.
• Example: ZIP files take less space but need time to unzip when accessed.

You might also like