efficent coding lab
efficent coding lab
efficent coding lab
Aim:
develop a program and measure the running time for binary search with divide and conquer
program:
#include <stdio.h>
#include <time.h>
if (arr[mid] == key) {
return mid;
int main()
if (result == -1) {
} else {
return 0;
Output:
2. develop a program and measure the running time for merge sort with divide and conquer
using c
Program:
#include <stdio.h>
#include <time.h>
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
return 0;
}
Output:
Sorted array:
11 12 22 25 34 64 90
3. develop a program and measure the running time for Quick sort with divide and conquer
using c
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
clock_t start, end;
double cpu_time_used;
start = clock(); // Start time measurement
quickSort(arr, 0, n - 1);
end = clock(); // End time measurement
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Sorted array: \n");
printArray(arr, n);
printf("\nTime taken: %f seconds\n", cpu_time_used);
return 0;
}
Output:
Sorted array:
1 5 7 8 9 10
Time taken: 0.000001 seconds
4. Develop a program and measure the running time for estimating minimum cost
spanning tree with greedy method using java.
Program:
import java.util.ArrayList;
import java.util.PriorityQueue;
class Graph {
int V;
ArrayList<ArrayList<Node>> adjList;
public Graph(int V)
{
this.V = V;
adjList = new ArrayList<>();
for (int i = 0; i < V; i++) {
adjList.add(new ArrayList<>());
}
}
public void addEdge(int u, int v, int weight) {
adjList.get(u).add(new Node(v, weight));
adjList.get(v).add(new Node(u, weight));
}
public void primMST() {
PriorityQueue<Node> pq = new PriorityQueue<>();
boolean[] visited = new boolean[V];
pq.add(new Node(0, 0)); // Add the starting vertex with weight 0
int cost = 0;
while (!pq.isEmpty()) {
Node node = pq.poll();
int u = node.vertex;
if (visited[u]) continue;
visited[u] = true;
cost += node.weight;
for (Node neighbor : adjList.get(u)) {
if (!visited[neighbor.vertex]) {
pq.add(neighbor);
}
}
}
7. develop a program and measure the running time for identifying solution for traveling salesperson
problem with dynamic programming using c
Program:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define MAX_N 10
int n;
int graph[MAX_N][MAX_N];
// Function to find the minimum cost Hamiltonian cycle using dynamic programming
if (mask == (1 << n) - 1) {
if (memo[current][mask] != -1) {
return memo[current][mask];
memo[current][mask] = minCost;
return minCost;
int main() {
scanf("%d", &n);
scanf("%d", &graph[i][j]);
memo[i][j] = -1;
return 0;
Output:
/tmp/b47SILh8lX.o
0 10 15 20
10 0 35 25
15 35 0 30
20 25 30 0
5. develop a program and measure the running time for estimating single
source shortest paths with greedy method
Program:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include <time.h>
#define MAX_VERTICES 100
struct Graph {
int vertices;
int** adjMatrix;
};
graph->vertices = vertices;
graph->adjMatrix[i][j] = 0;
return graph;
void addEdge(struct Graph* graph, int src, int dest, int weight) {
graph->adjMatrix[src][dest] = weight;
}
// Function to find the vertex with the minimum distance value
min = distance[v];
minIndex = v;
return minIndex;
int distance[MAX_VERTICES];
bool visited[MAX_VERTICES];
visited[i] = false;
distance[src] = 0;
visited[u] = true;
printSolution(distance, vertices);
int main() {
scanf("%d", &vertices);
scanf("%d", &edges);
int source;
scanf("%d", &source);
dijkstra(graph, source);
return 0;
Output:
6. develop a program and measure the running time for optimal binary search
tree with dynamic programming using java
Program:
import java.util.Scanner;
sum += probabilities[k];
return sum;
int n = probabilities.length;
sumProbabilities[i][i] = probabilities[i];
cost[i][j] = 0;
cost[i][j] = Float.MAX_VALUE;
sumOfProbabilities(probabilities, i, j);
int n = scanner.nextInt();
probabilities[i] = scanner.nextFloat();
optimalBST(probabilities);
scanner.close();
}
Output:
Minimum cost of optimal binary search tree: 35.0Running time: 0.016 seconds