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

Ds Algorithm

Uploaded by

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

Ds Algorithm

Uploaded by

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

EX.

NO:1 BINARY SEARCH


Algorithm
1. Start the program
2. Clear the screen
3. Declare variables count, i, arr[30], num, first, last, middle
4. Ask the user how many elements they would like to enter and store the value
in count
5. Use a for loop to input elements into the array arr
6. Ask the user for the element they want to search and store it in num
7. Set first=0, last=count-1, middle=(first+last)/2
8. Start a while loop with the condition first<=last
9. Within the while loop, check if arr[middle]<num
10. If true, set first=middle+1
11. Else if arr[middle]==num, print "num Found in the Array at the location"
and the index (middle+1)
12. Break out of the loop
13. Else, set last=middle-1
14. Calculate the new middle=(first+last)/2
15. End the while loop
16. End the program.
Ex.no:2 Stack
Algorithm:
1. Define an array called stack of size 100 and set top to -1.
2. Create a function push to add elements to the top of the stack.
3. Check if the stack is full, and if not increment top and add the value to the
stack.
4. Create a function pop to remove elements from the top of the stack.
5. Check if the stack is empty, and if not remove the top element and decrement
top.
6. Create a function display to print all elements in the stack.
7. Check if the stack is not empty, and then iterate from top to 0 and print the
elements.
8. If the stack is empty, print "stack is empty".
9. In the main function, display a menu and prompt the user to enter a choice.
10. Based on the choice, either push a value, pop an element, display the stack,
or exit the program.
11. Repeat the menu until the user chooses to exit.
12. End the program with a return sstatement.
EX.NO:3 QUEUE
Algorithm
1. Declare variables and initialize necessary values.
2. Display menu of queue operations.
3. Accept user choice.
4. Use switch case to implement different operations based on user choice.
5. For case 1 (Enqueue):
- Check if queue is full.
- If not full, accept value from user and insert it into the queue at the rear
position.
6. For case 2 (Dequeue):
- Check if queue is empty.
- If not empty, delete the element at the front of the queue and increment front
pointer.
7. For case 3 (Display):
- Check if queue is empty.
- If not empty, loop through the queue elements from front to rear and display
them.
8. For case 4 (Exit):
- Exit the program.
9. Handle default case for invalid user choice.
Ex.no 4 Binary tree traversal
Algorithm
1. Declare a structure treerec with lchild, rchild, and data fields.
2. Declare global variables item and rdata[] of type struct treerec.
3. Define functions inorder, preorder, and postorder for tree traversal.
4. Prompt user for the number of elements and input data for each element.
5. Create a binary tree using the input data.
6. Initialize item as the root node of the tree.
7. Perform inorder, preorder, and postorder traversals on the tree starting from
the root node.
8. Display the elements in each traversal order.
9. End the program.
Ex.no 5 BREADTH FIRST SEARCH
Algorithm
1. Read the number of vertices and edges from the user.
2. Initialize the adjacency matrix 'arr' and arrays 'visit' and 'assign' to store
visited and assigned vertices.
3. Read the edges and populate the adjacency matrix accordingly.
4. Read the initial vertex 'v' from the user.
5. Initialize a queue 'queue' and enqueue the initial vertex 'v'.
6. Perform a BFS traversal starting from 'v' by dequeuing and visiting adjacent
vertices.
7. Print the order of visited vertices.
8. End the program.
Ex.no:6 Depth first search
Algorithm
1. Initialize variables and arrays.
2. Input the number of vertices and edges.
3. Input the edges and store them in the adjacency matrix.
4. Input the initial vertex.
5. Print the initial vertex as the first visited vertex.
6. Mark the initial vertex as visited.
7. Create a loop to visit all vertices.
8. Check for adjacent unvisited vertices and push them onto the stack.
9. Pop a vertex from the stack and mark it as visited.
10. Print the visited vertex.
11. Increment the count of visited vertices.
12. Mark the visited vertex as unvisited and assign it.
13. Repeat until all vertices are visited.
14. End program.
Ex.no7 Merge Sort
Algorithm
1. Define a function to merge two subarrays.
2. Create two subarrays L and R.
3. Copy elements from original array to subarrays L and R.
4. Compare elements of subarrays L and R and merge them into the original
array in sorted order.
5. Define a function to recursively divide and merge subarrays.
6. Print the given array.
7. Sort the given array using the mergesort function.
8. Print the sorted array. 1. Define a function to merge two subarrays.
2. Create two subarrays L and R.
3. Copy elements from original array to subarrays L and R.
4. Compare elements of subarrays L and R and merge them into the original
array in sorted order.
5. Define a function to recursively divide and merge subarrays.
6. Print the given array.
7. Sort the given array using the mergesort function.
8. Print the sorted array.

EX.NO 8 QUICK SORT


Algorithm
1. Initialize variables i, j, pivot, and temp.
2. Check if the 'first' index is less than the 'last' index.
3. Set the pivot to the first element.
4. Initialize i to the first index and j to the last index.
5. While i is less than j, do the following:
6. Increment i while a[i] is less than or equal to the pivot and i is less than the
last index.
7. Decrement j while a[j] is greater than the pivot.
8. If i is less than j, swap a[i] with a[j].
9. Swap a[pivot] with a[j].
10. Recursively call quicksort for the subarrays before and after the pivot.

You might also like