0% found this document useful (0 votes)
5 views5 pages

Algorithm DS

The document outlines various algorithms for data structures and searching/sorting techniques, including array manipulation, stack and queue operations, binary search tree traversals, linear and binary search, and sorting methods like bubble sort, insertion sort, and quick sort. Each algorithm is described step-by-step, detailing the operations involved and conditions for execution. The document serves as a comprehensive guide for implementing fundamental data structures and algorithms.

Uploaded by

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

Algorithm DS

The document outlines various algorithms for data structures and searching/sorting techniques, including array manipulation, stack and queue operations, binary search tree traversals, linear and binary search, and sorting methods like bubble sort, insertion sort, and quick sort. Each algorithm is described step-by-step, detailing the operations involved and conditions for execution. The document serves as a comprehensive guide for implementing fundamental data structures and algorithms.

Uploaded by

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

1.

Algorithm
1. Input the number of elements (N):
o Read the integer N from the user.
2. Read N elements into an array:
o Initialize an empty array.
o Use a loop to read N elements from the user and append them to the array.
3. Insert an element at a given index:
o Read the index and the element to be inserted.
o Check if the index is valid (0 <= index <= length of array).
o If valid, insert the element at the specified index.
o If invalid, display an error message.
4. Delete an existing element:
o Read the index of the element to be deleted.
o Check if the index is valid (0 <= index < length of array).
o If valid, delete the element at the specified index.
o If invalid, display an error message.
5. Display the modified array.
2. Algorithm
1. Initialize an empty stack.
2. Read the postfix expression from left to right.
3. For each symbol in the postfix expression:
o If the symbol is an operand (e.g., a variable or number):
 Push it onto the stack.
o If the symbol is an operator (e.g., +, -, *, /):
 Pop the top two elements from the stack (these are the operands).
 Create a new string by concatenating the operator and the two operands in
the order: operator operand1 operand2.
 Push this new string back onto the stack.
4. After processing all symbols, the stack will contain one element, which is the prefix
expression.
5. Pop the final result from the stack.

3. Algorithm
1. Initialize an array to hold stack elements and set an integer variable top to -1
(indicating the stack is empty).
2. Define the following operations:
o Push(element):
 Check if the stack is full. If not, increment top and assign element to
stack[top].

o Pop():
 Check if the stack is empty. If not, retrieve the element at stack[top],
decrement top, and return the element.
o Peek():
 Check if the stack is empty. If not, return the element at stack[top].
o IsEmpty():
 Return true if top is -1; otherwise, return false.
o IsFull():
 Check if top is equal to the last index of the array (size - 1). If yes, return
true; otherwise, return false.

4. Algorithm
1. Define a Node structure:
o Each node should contain a value and a pointer/reference to the next node.
2. Initialize the stack:
o Create a Stack class with a head pointer set to None (indicating an empty stack).
3. Define the following operations:
o Push(element):
 Create a new node with the given element.
 Set the new node's next pointer to the current head.
 Update the head to point to the new node.
o Pop():
 Check if the stack is empty. If not, retrieve the head element, update the
head to the next node, and return the element.
o Peek():
 Check if the stack is empty. If not, return the value of the head node.
o IsEmpty():
 Return true if the head is None; otherwise, return false.
5. Algorithm
1. Initialize an array to hold queue elements and set two integer variables, front and
rear, to -1 (indicating an empty queue).
2. Define the following operations:
o Enqueue(element):
 Check if the queue is full. If not:
 If the queue is empty (i.e., front == -1), set both front and rear
to 0.
 Increment rear and assign element to queue[rear].
o Dequeue():
 Check if the queue is empty. If not:
 Retrieve the element at queue[front].
 If front equals rear, set both front and rear to -1 (indicating the
queue is now empty). Otherwise, increment front.
 Return the retrieved element.
o Peek():
 Check if the queue is empty. If not, return the element at queue[front].
o IsEmpty():
 Return true if front is -1; otherwise, return false.
o IsFull():
 Check if rear is equal to the last index of the array (size - 1). If yes, return
true; otherwise, return false.
6. Algorithm
1. Define a Node structure:
o Each node should contain a value and a pointer/reference to the next node.
2. Initialize the queue:
o Create a Queue class with pointers for front and rear, both initialized to None.
3. Define the following operations:
o Enqueue(element):
 Create a new node with the given element.
 If the queue is empty (i.e., front is None):
 Set both front and rear to the new node.
 Otherwise:
 Set the next of the current rear to the new node.
 Update rear to point to the new node.
o Dequeue():
 Check if the queue is empty. If not:
 Retrieve the value of the front node.
 Update front to point to the next node.
 If front becomes None, set rear to None as well (indicating the
queue is now empty).
 Return the retrieved value.
o Peek():
 Check if the queue is empty. If not, return the value of the front node.
o IsEmpty():
 Return true if front is None; otherwise, return false.
7.Algorithm: Binary Search Tree (BST) traversals are methods for visiting each node in the tree
in a specific order. The three most common types of traversals are:
1. In-order Traversal
2. Pre-order Traversal
3. Post-order Traversal
Each of these traversals can be implemented either recursively or iteratively. Below, I’ll outline
the algorithms for each type of traversal.
1. In-order Traversal (Left, Root, Right)
Algorithm:
1. Start from the root node.
2. Recursively traverse the left subtree.
3. Visit (process) the root node.
4. Recursively traverse the right subtree.
2. Pre-order Traversal (Root, Left, Right)
Algorithm:
1. Start from the root node.
2. Visit (process) the root node.
3. Recursively traverse the left subtree.
4. Recursively traverse the right subtree.
3. Post-order Traversal (Left, Right, Root)
Algorithm:
1. Start from the root node.
2. Recursively traverse the left subtree.
3. Recursively traverse the right subtree.
4. Visit (process) the root node.

8. A. Linear Search
Algorithm:
1. Start at the beginning of the list.
2. Iterate through each element in the list.
3. For each element, check if it is equal to the target item.
4. If a match is found, return the index of the element.
5. If the end of the list is reached and no match is found, return -1 (indicating the item is not
in the list).
B. Binary Search
Precondition: The list must be sorted.
Algorithm:
1. Set two pointers: low at the start of the list (0) and high at the end of the list (length - 1).
2. While low is less than or equal to high:
o Calculate the middle index: mid = (low + high) // 2.
o If the middle element is equal to the target item, return mid.
o If the target item is less than the middle element, update high to mid - 1 (search
in the left half).
o If the target item is greater than the middle element, update low to mid + 1
(search in the right half).
3. If the loop ends and the item is not found, return -1.

9.
A. Bubble Sort
Algorithm:
1. Start from the first element in the list.
2. Compare the current element with the next element.
3. If the current element is greater than the next element, swap them.
4. Move to the next element and repeat steps 2-3 until the end of the list.
5. Repeat the entire process for n-1 passes, where n is the length of the list.
6. If no swaps are made during a pass, the list is sorted, and you can stop early.
B. Insertion Sort
Algorithm:
1. Start with the second element (index 1) of the list.
2. Compare it with the elements before it (in the sorted part).
3. Shift all larger elements one position to the right.
4. Insert the current element into its correct position.
5. Repeat for all elements in the list.
C. Quick Sort
Algorithm:
1. Choose a pivot element from the list.
2. Partition the list into two sub-lists:
o Elements less than or equal to the pivot.
o Elements greater than the pivot.
3. Recursively apply the same steps to the sub-lists.
4. Combine the sub-lists and the pivot back together.

You might also like