Notes DSA
Notes DSA
Rollno: S23BARIN1M01122
Class: 3rd Ai M2
Notes: DSA
Data Structure:
Algorithms:
Algorithms are sets of steps for solving problems. Basic algorithms include
sorting, searching, and recursion.
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:
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
These data Structure are already defined and we just utilize them. Eg int,
floot double, char etc.
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.
2 9 11 13 1 7 18
0 1 2 3 4 5 6
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.
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
linear data structures are further divided into graph and tree based data
structures.
In graph data structure, each node is called vertex and each vertex is
connected to other vertices through edges.
2) Trees Data Structure
Search
Insertion
Deletion:
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.
Array’s:
Insert -at- start
Insert -at- end
Insert -at- location
Search
Binary Search
Selection – Sort
Bubble – Sort
Merge – Sort
Quick – Sort
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.
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
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
Stack:
Push
Pop
Push double 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
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
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.
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