DAA 6,7 Practical
DAA 6,7 Practical
Write a program to sort the university roll numbers of your class using Quick
sort method and determine the time required to sort the elements.
Algorithm:
Step 1: QUICKSORT (array A, start, end)
Step 2: {
Step 4: {
Step 8: }
Step 9: }
Partition algorithm:
Step 1: PARTITION (array A, start, end)
Step 2: {
Step 4: i = start- 1
Step 7: then i = i + 1
Step 9: }
Step 10:}
Step 13: }
Code:
#include<iostream>
int x=a[r];
int i=p-1;
for(int j=p;j<=r-1;j++){
if(a[j]<=x){
i=i+1;
swap(a[i],a[j]);
swap(a[i+1],a[r]);
return i+1;
QUICKSORT(a,p,q-1);
QUICKSORT(a,q+1,r);
int main(){
cout<<"ISHA 2203461\n";
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];}
QUICKSORT(a,0,n-1);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
OUTPUT:
7. Write a program to find shortest path from your home to college using
Dijkstra‘s algorithm.
Algorithm:
Step 1: Initialize the distance to the starting node as 0 and to all other nodes as infinity.
Step 2: Set the starting node as the current node.
Step 3: Mark all nodes as unvisited and add them to a priority queue (min-heap).
Step 4: For the current node, examine its unvisited neighbors. For each neighbor:
a. Calculate the distance from the start node to this neighbor.
b. If this new distance is less than the current known distance to this neighbor, update it.
Step 5: Once done with the neighbors, mark the current node as visited.
Step 6: Select the unvisited node with the smallest known distance as the new "current node"
and repeat steps 4-6.
Step 7: Continue until all nodes are visited or the shortest path to the destination is found.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits> // Include this header for INT_MAX
using namespace std;
#define V 4 // No of vertices
// Function to select the minimum vertex that is not yet processed
int selectMinVertex(vector<int>& value, vector<bool>& processed) {
int minimum = INT_MAX;
int vertex = -1; // Initialize to -1 to avoid uninitialized behavior
for (int i = 0; i < V; ++i) {
if (!processed[i] && value[i] < minimum)
{
vertex = i;
minimum = value[i];
} }
return vertex; }
// Function to print the shortest path from the source to a given vertex
void printPath(vector<int>& parent, int vertex) {
if (vertex == -1) return;
printPath(parent, parent[vertex]);
cout << vertex << " ";
}
// Dijkstra's algorithm to find the shortest path from start to target
void dijkstra(int graph[V][V], int start, int target) {
vector<int> parent(V, -1); // Stores the shortest path structure
vector<int> value(V, INT_MAX); // Keeps shortest path values to each vertex from source
vector<bool> processed(V, false); // TRUE -> Vertex is processed
parent[start] = -1; // Start node has no parent
value[start] = 0; // Start node has value=0 to get picked 1st
// Table header
cout << "\Isha 2203461\n";
cout << "------------------------------------------------------------\n";
cout << "Step\tVertex\tCurrent Distance\tPath\t\tTotal Weight\n";
cout << "------------------------------------------------------------\n";
// Include (V-1) edges to cover all V vertices
for (int i = 0; i < V - 1; ++i) {
// Select the best vertex by applying the greedy method
int U = selectMinVertex(value, processed);
if (U == -1) break; // If no valid vertex is found, exit early
processed[U] = true; // Include new vertex in the shortest path graph
// Print the current step details
cout << i + 1 << "\t\t" << U << "\t\t\t\t" << value[U] << "\t\t\t";
printPath(parent, U);
// Providing space between the path and the total weight
cout << "\t\t\t" << value[U] << "\n"; // Print the current total weight
// Relax adjacent vertices (not yet included in the shortest path graph)
for (int j = 0; j < V; ++j) {
/* 3 conditions to relax:
1. There is an edge from U to j.
2. Vertex j is not included in the shortest path graph.
3. The edge weight is smaller than the current edge weight.
*/
if (graph[U][j] != 0 && !processed[j] && value[U] != INT_MAX
&& (value[U] + graph[U][j] < value[j])) {
value[j] = value[U] + graph[U][j];
parent[j] = U; }
} }
// Print the distance and the path from source to the target
cout << "------------------------------------------------------------\n";
cout << "Shortest Path from Vertex " << start << " to Vertex " << target << ":\n";
cout << "Total Weight: " << value[target] << "\n";
cout << "Path: ";
printPath(parent, target);
cout << "\n------------------------------------------------------------\n";
}
int main() {
// Graph representation using adjacency matrix
int graph[V][V] = {
{0, 5, 4, 0}, // Connections from vertex 0
{5, 0, 0, 7}, // Connections from vertex 1
{4, 0, 0, 3}, // Connections from vertex 2
{0, 7, 3, 0} // Connections from vertex 3
};
int start = 0; // Home at vertex 0
int target = 3; // College at vertex 4
// Run Dijkstra's algorithm to find the shortest path from start to target
dijkstra(graph, start, target);
return 0;
}
Output: