0% found this document useful (0 votes)
21 views

Dsaaa

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Dsaaa

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Memory allocation:

1.First fit : Allocates to the first hole that is big


enough
2.Best fit:Allocates the smallest hole that is big
enough
3.Worst fit: Allocates the largest hole
4.Next fit : same as first fit but start searching
always from last alloacated
Hole
AVL TREE
-it is a BST
-(-1,0,1)……………….balance factor
ADJACENCY MATRIX …. Space complexity Ѳ
(n^2)
AJACENCY LIST…….space complexity Ѳ(n+2e)
BFS & DFS ……Time complexity is 0(v+E)
PRIMS ALGORITHM
G(V,E) G’(V’,E’)……………………..V=vertex
E=edges
V’=V E’ C E
E’=(V-1)
Same for krushkal algo
Total cost = sab kuch plus kardo

Dijkstra ALGORITHM:
IF (d(u)+c(u,v)<d(u)) v=infinity
d(v)=d(v)+c(u,v)…………second time wala
formula

Hashing: Ki%m m=no. of box


h(k)=diya hoga exam meinusko he Ki bolte
Linear Probing : location(u+i)%m i=0 to (m-1)
Quadratic probing: (u+i^2) i=0 to (m-1)
Double hashing: u=h1(k)%m v=h2(k)%m
probing (u+v+i) %m
Algorithm to Convert Infix to Postfix Expression
1. Operator Precedence:
• Assign precedence to operators:
o * and / have higher precedence than + and -.
o Parentheses have the highest precedence.
2. Algorithm:
• Create an empty stack to store operators.
• Scan the infix expression from left to right.
• If the scanned character is an operand, add it to the postfix
expression.
• If the scanned character is an operator:
o While the stack is not empty and the precedence of the top
operator is greater than or equal to the precedence of the
current operator:
▪ Pop operators from the stack and add them to the postfix
expression.
o Push the current operator onto the stack.

• If the scanned character is an opening parenthesis, push it onto the


stack.
• If the scanned character is a closing parenthesis, pop operators from
the stack and add them to the postfix expression until an opening
parenthesis is encountered. Pop the opening parenthesis.
• After scanning the entire infix expression, pop any remaining
operators from the stack and add them to the postfix expression.
Example:
Infix expression: A + B * C - D Postfix expression: ABC*+D-
Algorithm for Infix to Prefix Conversion
1. Reverse the infix expression:
• Reverse the order of characters in the infix expression, including
parentheses.
2. Convert the reversed infix expression to postfix:
• Use a stack to process the reversed infix expression.
• Iterate through the reversed expression:
o If the current character is an operand, push it onto the output
string.
o If the current character is an operator:
▪ While the stack is not empty and the precedence of the
top operator is greater than or equal to the precedence of
the current operator, pop operators from the stack and
add them to the output string.
▪ Push the current operator onto the stack.
o If the current character is an opening parenthesis, push it onto
the stack.
o If the current character is a closing parenthesis, pop operators
from the stack and add them to the output string until an
opening parenthesis is encountered. Pop the opening
parenthesis.
• After processing the entire reversed infix expression, pop any
remaining operators from the stack and add them to the output
string.
3. Reverse the postfix expression:
• Reverse the obtained postfix expression to get the prefix expression.
Example:
Infix Expression: A + B * C - D
Reversed Infix Expression: D - C * B + A
Postfix Expression (after conversion): DC*BA-+
Prefix Expression (after reversing postfix): -+A*BC D
Key Points:
• The key idea is to reverse the infix expression and then apply the
postfix conversion algorithm.
• The precedence of operators plays a crucial role in determining the
order of operations.
• The stack is used to store operators and handle operator
precedence.
• The final step is to reverse the obtained postfix expression to get the
prefix expression.
Sources and related content
Algorithm for Circular Linked List
Node Structure:
1. Create a node structure with two fields:
o data: To store the data value of the node.
o next: To store the address of the next node.
Basic Operations:
1. Insertion at the Beginning:
1. Create a new node.
2. If the list is empty:
o Set the next pointer of the new node to itself.
o Set the head pointer to the new node.
3. If the list is not empty:
o Set the next pointer of the new node to the current
head.
o Traverse the list to find the last node.
o Set the next pointer of the last node to the new node.
o Set the head pointer to the new node.
2. Insertion at the End:
1. Create a new node.
2. If the list is empty:
o Set the next pointer of the new node to itself.
o Set the head pointer to the new node.
3. If the list is not empty:
o Traverse the list to find the last node.
o Set the next pointer of the last node to the new node.
o Set the next pointer of the new node to the head.
3. Deletion at the Beginning:
1. If the list is empty, return.
2. If the list has only one node:
o Set the head pointer to NULL.
o Free the node.
3. If the list has more than one node:
o Set the head pointer to the next node.
o Traverse the list to find the last node.
o Set the next pointer of the last node to the new head.
o Free the old head.
4. Traversal:
1. If the list is empty, return.
2. Start from the head node.
3. Print the data of the current node.
4. Move to the next node.
5. Repeat steps 3 and 4 until you reach the head node again.
Merge Sort
1. Divide and Conquer:
o Divide the array into two halves.
2. Recursive Calls:
o Recursively sort the two halves.
3. Merge:
o Merge the two sorted halves into a single sorted array.
Quick Sort
1. Partition:
o Choose a pivot element.
o Rearrange the array such that elements smaller than
the pivot are on the left, and elements larger than the
pivot are on the right.
2. Recursive Calls:
o Recursively sort the subarrays on both sides of the
pivot.
Bubble Sort
1. Iterate:
o Repeat the following steps for each pass:
▪ Compare adjacent elements.
▪ If they are in the wrong order, swap them.
2. Optimization:
o In each pass, the largest element bubbles to its correct
position.
o The number of iterations can be reduced in subsequent
passes.
Insertion Sort
1. Iterate:
o For each element in the array:
▪ Compare the current element with the elements
in the sorted part.
▪ Shift elements to the right to create a space for
the current element.
▪ Insert the current element in its correct position.
Heap Sort
1. Build Heap:
o Construct a max-heap from the input array.
2. Heapify:
o Remove the root (maximum element) and replace it
with the last element.
o Heapify the reduced heap to maintain the max-heap
property.
3. Repeat:
o Repeat step 2 until the heap is empty.
Sources and related content
Time Complexity:
• Best Case: O(n log n)
• Average Case: O(n log n)
Worst Case: O(n log n) Space Complexity: O(1)
Selection Sort
1. Find Minimum:
o Find the minimum element in the unsorted part of the
array.
2. Swap:
o Swap the minimum element with the first element of
the unsorted part.
3. Repeat:
o Repeat steps 1 and 2 for the remaining unsorted part.
Time Complexity:
• Best Case: O(n^2)
• Average Case: O(n^2)
• Worst Case: O(n^2)
Space Complexity: O(1)
Stack
A stack is a linear data structure that follows the Last-In-First-Out
(LIFO) principle. It operates on two primary operations:
1. Push: Adds an element to the top of the stack.
2. Pop: Removes and returns the element from the top of the
stack.
Queue
A queue is a linear data structure that follows the First-In-First-
Out (FIFO) principle. It operates on two primary operations:
1. Enqueue: Adds an element to the rear of the queue.
2. Dequeue: Removes and returns the element from the front
of the queue.
Implementation:
Both stacks and queues can be implemented using arrays or
linked lists.
Array-based Implementation:
• Stack:
o Use an array to store elements.
o Maintain a top index to keep track of the top element.
o Push: Increment top and insert the element at the top
index.
o Pop: Decrement top and return the element at the
previous top index.
• Queue:
o Use an array to store elements.
o Maintain two indices: front and rear.
o Enqueue: Increment rear and insert the element at the
rear index.
o Dequeue: Increment front and return the element at
the previous front index.
Linked List-based Implementation:
• Stack:
o Use a singly linked list.
o Push: Create a new node and insert it at the beginning
of the list.
o Pop: Remove the first node from the list.
• Queue:
o Use a singly linked list.
o Enqueue: Create a new node and insert it at the end of
the list.
o Dequeue: Remove the first node from the list.
Key Points:
• Stacks and queues are fundamental data structures used in
various algorithms and data structures.
• The choice of implementation (array-based or linked list-
based) depends on the specific use case and performance
requirements.
• Understanding the LIFO and FIFO principles is crucial for
effective use of stacks and queues.
Binary Search Tree (BST) Algorithm
A Binary Search Tree (BST) is a binary tree where each node has at most two
children, and the nodes are arranged in a specific order:
Basic Operations:
1. Insertion:
• Start at the root node.
• Compare the key to be inserted with the current node's key.
• If the key is smaller, move to the left child.
• If the key is larger, move to the right child.
• If the current node is null, insert the new node at that position.
2. Deletion:
• Find the node to be deleted.
• If the node has no children, remove it.
• If the node has one child, replace it with its child.
• If the node has two children, find its inorder successor (the smallest
node in the right subtree), replace the node's key with the
successor's key, and then delete the successor.
3. Search:
• Start at the root node.
• Compare the key to be searched with the current node's key.
• If the key is smaller, move to the left child.
• If the key is larger, move to the right child.
• If the keys match, the node is found.

• BSTs are efficient for search, insertion, and deletion operations, especially
when the tree is balanced.
• To ensure efficient operations, it's often recommended to balance the BST
using techniques like AVL trees or Red-Black trees.

You might also like