Name: Muhammad Umar Jamil
Rollno: S23BARIN1M01122
Class: 3rd Ai M2
Notes: DSA
DSA ( Data Structure & Algorithms)
Introduction:
Data Structure:
Data structures are ways of organizing and storing data in a computer. It is a
way of arranging data on a computer so that it can be accessed and updated
efficiently Common data structures include arrays, linked lists, stacks, queues,
trees, and graphs.
Algorithms:
Algorithms are sets of steps for solving problems. Basic algorithms include
sorting, searching, and recursion.
Even Odd Algorithm:
1. Start
2. Input Number
3. Check if number % 2 = = 0
4. Print Even
(End of step 3)
5. Else of step (3)
6. Print Odd
(End of step 3)
7. End
Greater and Smaller Algorithm:
1) Start
2) Input a & b from user
3) Check if a > b
4) Print a is greater than b
5) Else if of step (3) a < b
6) Print a is smaller than b
7) Else of step (3)
8) Print a and b are equal (end of step (3)
9) End
Loop:
Three types of loop:
For loop:
When we know number of iteration in advance.
While loop:
When we don’t know number of iteration and it depends on user then
best is while loop.
Do – While loop:
If you wanna run atleast once whether the condition is true or not.
Example:
1) Start
2) Set i 1
3) While i < = 10
4) Print ali
5) Set i i+1
(repeat step 3)
6) End
Types of Data Structure:
There are two types of Data Structure:
Primitive Data Structure
Non- Primitive Data Structure
Primitive Data Structure:
These data Structure are already defined and we just utilize them. Eg int,
floot double, char etc.
Non- Primitive Data Structure
Structure:
These Data Structure are user
user-defined.
defined. Eg Array, Stack, hoshtable, Queue.
There are two other types of data structures:
Linear data structure
Non-linear
linear data structure
Linear data structures:
In linear data structures, the elements are arranged in sequence one after the
other. Since elements are arranged in particular order, they are easy to implement.
1. Array Data Structure
Structure:
In an array, elements in memory are arranged in continuous memory. All
the elements of an array are of the same type. And, the type of elements that can
be stored in the form of arrays is determined by the programming language
2 9 11 13 1 7 18
0 1 2 3 4 5 6
2. Stack Data Structure
Structure:
In stack data structure, elements are stored in the LIFO principle. That is,
the last element stored in a stack will be removed first.
3. Queue Data Structure
Structure:
Unlike stack, the queue data structure works in the FIFO principle where
first element stored in the queue will be removed first.
4. Linked List Data Structure
In linked list data structure, data elements are connected through a series
of nodes. And, each node contains the data items and address to the next node.
Non linear data structures
structures:
Unlike linear data structures, elements in non
non-linear data structures are
not in any sequence. Instead they are arranged in a hierarchical manner where
one element will be connected to one or more elements.
Non-linear
linear data structures are further divided into graph and tree based data
structures.
1) Graph Data Structure:
ructure:
In graph data structure, each node is called vertex and each vertex is
connected to other vertices through edges.
2) Trees Data Structure
Similar to a graph, a tree is also a collection of vertices and edges.
However, in tree data structure, th
there
ere can only be one edge between two
vertices.
Data structure operations::
Search
Search operations are used to find a specific data element in a data
structure. These operations typically employ a compare function to
determine if two data elements ar
are equal.
Traversal
Traversal operations are used to visit each node in a data structure in a
specific order. This technique is typically employed for printing, searching,
displaying, and reading the data stored in a data structure
structure.
Insertion
Insertion operations add new data elements to a data structure. You
can do this at the data structure's beginning, middle, or end.
Deletion:
Deletion operations remove data elements from a data structure. These
operations are typically performed on nod
nodes
es that are no longer needed.
Splitting:
Data splitting is when data is divided into two or more subsets.
Typically, with a two-part split, one part is used to evaluate or test the data
and the other to train the model.
Data splitting is an important aspect of data science, particularly for creating
models based on data.
Sort:
Sort operations are used to arrange the data elements in a data
structure in a specific order. This can be done using various sorting
algorithms, such as insertion sort, bubble sort, merge sort, and quick sort.
Merge:
Merge operations are used to combine two data structures into one.
This operation is typically used when two data structures need to be
combined into a single structure.
Algorithm:
A step by step to solve problems is called Algorithm.
Input > Processing > Output
Finitness (Limit)
Effectiveness (Important for its work)
General
Array’s:
Insert -at- start
Insert -at- end
Insert -at- location
Search
Binary Search
Selection – Sort
Bubble – Sort
Merge – Sort
Quick – Sort
Insert – at – start (Array, N, Value)
Arr:
2 9 11 13 1 7 18
0 1 2 3 4 5 6 7 8 9
Value: 2
1. Start
2. Check if array[0] = empty
3. Set arr[0] value
4. Else of step 2
5. Set i 0
6. While ( arr[i]! = empty)
7. set i i+1
end of loop (6)
8. While (i >=0)
9. Set arr[i] arr[i-1]
10. Set i i-1
End of loop 8
11. Set arr[0] value
12. End
Insertion – At – End
1. Start
2. Set i 0
3. While (i < N)
4. Check if (array[i] ! = Null)
5. set i i+1
end of loop (3)
6. Check if ( i != N)
Set array[i] Value
7. Else of stop (6)
8. Show array is full
End of if (6)
9. End.
Insert - At- Location
Arr:
2 9 11 13 1 7 18
0 1 2 3 4 5 6 7 8 9
Value: 2
1. Start
2. Check if (arr [loc] = empty
3. Set arr [loc] value
4. Else of step 3
5. Set i 0
6. While ( arr[i]! = empty)
7. set i i+1
end of loop (6)
8. While (i>=loc)
9. Set arr[i] arr[i-1]
10. Set i i-1
End of loop (8)
End of if (2)
11. Set arr[loc] value
12. End
Search ( Array[ ], N, Value )
1. Start
2. Set i 0
3. While ( i < N)
4. Check if ( array[i] = value)
5. Return i
6. Break
7. Else of step (4)
8. Output number not found
End of step (4)
End of step (3)
9. End.
Binary – Search ( Array[ ], N, value)
1. Start
2. Set start 0
3. Set end N-1
4. While (start <= end)
5. Set center (start + End )/2
6. If ( array [center] = value)
7. Return center
8. Else if ( value > array [center])
9. Set start center
10. Else if ( value < array [center])
11. Set end center
12. Else of step (6)
13. Show number not found
End of step (6)
End of step (4)
14. End
Quick Sort
Quick sort is a sorting algorithm that work by dividing an array into smaller sub-
arrays based on a chosen pivot element. Is then recursively sorts these sub-arrays.
Pivot Value:
The pivot value is the specific element chosen from the array in the quick
sort algorithm. It serve as a refrence point for partitioning the array into two
groups.
P: is typically used to denote the start index of the array or sub-array being sorted.
Q: is use to denote the end index of the array or sub-array being sorted.
Example:
15 135 65 91 76 13 12 804 99
p q
Pivot: 15
SELECTION – SORT (Array, N)
1. Start
2. Set i 0
3. While ( i < N-1)
4. set j i+1
5. While ( j < n- 1)
6. Check if (a [i] > [j] )
7. Set temp[i] a[ j ]
8. Set a[ j] a [i]
9. Set a[ i ] temp
End of if (7)
10. j j +1
End of loop (8)
11. i i +1
End of loop (9)
12. End
Bubble – SORT (Array, N)
1. Start
2. Set i 0
3. While ( i < n)
4. set j i
5. While ( j < n- 1- i)
6. Check if (arr [i] > arr[ j+1] )
7. Set temp arr[ j ]
8. Set arr[ j] a [ j+1]
9. Set arr[ j+1] temp
10. set j j +1
End of loop (5)
11. set i i +1
End of loop (3)
12. End
Stack:
Push
Pop
Push double end
Push ( Stack[ ], maxvalue, value)
i. Start
ii. Check if ( top = = max-value )
iii. Print “ Stack over flow”
iv. Else of if (2)
v. set Top Top +1
vi. set Stack[Top] value
End of if (2)
vii. End
Pop ( Stack [ ])
1) Start
2) Check if ( Top 1 == -1 AND Top 2 == max 4)
3) Print “ Stack Underflow”
4) Else of if (2)
5) Check if (user wants positive number)
6) Return Stack [Top1]
7) set Top3 Top3-1
8) Else of if (5)
9) Return Stack[Top2]
10) set Top2 Top 2+1
11) End
Push – DE Stack ( Stack[ ], value)
i. Start
ii. If ( top2 – Top1 == 1)
iii. show “ Stack over flow”
iv. Else of if (2)
v. Check if (value > 0)
vi. set Top Top 1+1
vii. set Stack[Top1] value
viii. Else of if (5)
ix. Top 2 Top 2-1
x. set Stack[Top2] value
End of if (2) and (5)
xi. End
Application Of Stack
Ways to write Expression:
There are three ways to write expression.
Infinix: operator lies between operands; a+b
Post fix: operator lies after operands e.g; a.b.+
Prefix: operator lies before operands e.g; +ab.
Rules:
1) Make two columns i.e, stack & expression.
2) Leave operand empty in stack and write in expression.
3) If the operator below is greater than one above or in other words gets solve
first then it’ll give it place with itself in stack.
4) If the one below is smaller, or equal to the one above then the one already in
stacks gets out of the stack and give place to the other.
5) If there are already two operators in stack and we have to place third one
too, then the one come later will goes out of stack first.
6) In case of paranthesis or brackets, we have to ignore any expression out side
the bracket until its closing bracket comes.
7) Second operand will, that’s why, moves the operand outside of the stack
that’s inside the bracket only and will not say anything to the one outside the
bracket.
Example:
i) a+b*c / 2-8
Stack Expression
a a a
+ + a
b + ab
* +,* ab
c +,* abc
/ +, / abc*
2 +, / abc*2
- - abc*2/+
8 - abc*2/+8-
ii) c – ( 3*2+8) / f
c c
- - c
c -c c
3 -c c,3
* -c* c,3,
2 -c* c,3,2
+ -c+ c,3,2*
8 -c+ c,3,2*8
) - c,3,2*8+
/ -/ c,3,2*8+
f c,3,2*8+f /-
Queue:
A queue is a linear data structure that follows the first in first out (FIFO)
principle.
Enqueue
Dequeue
Enqueue:
To insert any value in queue is called enqueue.
Dequeue:
To remove any value in queue is called dequeue.
Enqueue ( queue, value, maxvalue)
i. Start
ii. Check if ( f+1 % maxvalue == r)
iii. Print Queue is full
iv. set f (f +1)% maxvalue
v. Queue[f] value
vi. End
Dequeue ( queue)
i. Start
ii. Check if ( f == r)
iii. Print Queue is Empty
iv. Return queue [r]
v. set r (r +1)% maxsize
vi. End