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

Untitled Document-1

Uploaded by

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

Untitled Document-1

Uploaded by

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

Merge Sort:

1. Start the program.


2. Declare an array arr[] = {12, 11, 13, 5, 6, 7} and calculate its size.
3. Print the original array using the printArray() function.
4. Call the mergeSort() function with the array, starting index, and
ending index.
5. Inside mergeSort(), divide the array into two halves recursively
until each subarray contains one element.
6. In the merge() function, merge the divided parts back in sorted
order.
7. Print the sorted array using the printArray() function.
8. End the program by returning 0.

Heap Sort:

1. Start the program.


2. Define the heapify() function to maintain the heap structure.
3. Define the heapSort() function to build the heap and sort the
array.
4. Define the printArray() function to print the array elements.
5. In the main() function, declare an array arr[] = {12, 11,
13, 5, 6, 7} and calculate its size.
6. Call heapSort() to sort the array.
7. Use printArray() to display the sorted array.
8. End the program by returning 0.
Quick Sort:

1. Start the program.


2. Define the swap() function to exchange two values.
3. Define the partition() function to place the pivot element in the
correct position.
4. Define the quickSort() function to sort the array by dividing it
around the pivot and recursively sorting the subarrays.
5. Define the printArray() function to print the array elements.
6. In main(), declare the array and calculate its size.
7. Call quickSort() to sort the array.
8. Print the sorted array using printArray().
9. End the program

Knapsack Problem:

1. Start the program.


2. Define the knapsack() function to find the maximum value that
can be carried:
● Initialize a 2D array dp to store the intermediate results.
● Use nested loops to fill the table by checking if an item can fit
in the knapsack.
● Update the table based on the maximum value possible at
each step.
3. In main(), declare the value array val[] = {60, 100, 120}
and weight array wt[] = {10, 20, 30}.
4. Set the maximum weight capacity W = 50.
5. Call the knapsack() function to find and print the maximum value
that can be carried in the knapsack.
6. End the program.
Obtain the topological ordering of vertices in a given diagram

1. Start the program.


2. Define a Graph class to represent the graph.
3. In the class, initialize the number of vertices and an adjacency list.
4. Define a function to add directed edges.
5. Define a utility function for depth-first traversal and pushing
vertices onto a stack.
6. Define a function to perform topological sorting:
● Iterate through all vertices and call the utility function if a
vertex is unvisited.
● Print the vertices in topological order from the stack.
7. In main(), create a graph and add edges.
8. Call the topological sort function to display the result.
9. End the program.

Greedy algorithm to find minimum number of coins to make change for a


given value of indian currency. Assume that we have infinite supply of
denominations in India currency

1. Start the program


2. Define the findMinCoins() function to calculate the minimum
number of coins/notes needed:
● Initialize an array of available denominations.
● Create a vector to store the result.
● Iterate through each denomination and use a loop to subtract
from the amount while keeping track of the coins/notes used.
3. Print the total number of coins/notes required and the
denominations used.
4. In main(), declare a variable to store the amount.
5. Prompt the user to enter the amount.
6. Call findMinCoins() with the entered amount.
7. End the program.
Implement breadth first search and depth first search

1. Start the program


2. Define a Graph class to represent the graph with vertices and
edges.
3. Implement a function to add edges between vertices.
4. Implement the BFS() function to traverse the graph using a queue
and print visited vertices.
5. Implement the DFS() function to traverse the graph using
recursion and print visited vertices.
6. In main(), create a graph and add edges to it.
7. Call the BFS() function starting from vertex 0.
8. Call the DFS() function starting from vertex 0.
9. End the program.

Use prim's algorithm to find minimum spanning tree

1. Start the program


2. Define constants and a function to find the vertex with the
minimum key value not yet included in the MST.
3. Define a function to print the edges and their weights in the MST.
4. Implement the function to construct the MST, initializing arrays for
parent pointers, key values, and MST set status.
5. Set all key values to infinity and the first vertex's key value to 0,
then use a loop to find the minimum key vertex and update the
keys for its adjacent vertices.
6. In main(), define a graph using an adjacency matrix.
7. Call the function to compute and display the MST.
8. End the program.
Find short path using Dijkstra's algorithm

1. Start the program and include necessary libraries.


2. Define a Graph class to represent the graph.
3. Initialize the number of vertices and the adjacency list in the
constructor.
4. Implement a function to add edges with weights between vertices.
5. Implement the dijkstra() function to find the shortest paths
from the source vertex, initializing distances, using a priority
queue, and updating distances to neighboring vertices.
6. In main(), create a graph with a specified number of vertices and
add edges with weights.
7. Call the dijkstra() function starting from a specified source
vertex.
8. End the program.

Multiply two matrices recursively

1. Start the program and include necessary libraries.


2. Define a function to multiply two matrices recursively.
3. Define a function to print a matrix.
4. In main(), prompt the user to enter the size of the matrices.
5. Initialize two input matrices and a result matrix filled with zeros.
6. Prompt the user to input the elements of the first matrix.
7. Prompt the user to input the elements of the second matrix.
8. Call the recursive function to multiply the two matrices.
9. Output the resultant matrix.
10. End the program.

You might also like