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

ADA Lab Prgms - Part-1

The document contains implementations of various sorting algorithms (Selection Sort, Quick Sort, and Merge Sort) and graph algorithms (Kruskal’s and Prim’s) in C. Each program includes functions for sorting or finding minimum spanning trees, along with timing mechanisms to measure performance. The algorithms demonstrate different approaches to sorting and graph traversal, showcasing their respective efficiencies.
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)
5 views7 pages

ADA Lab Prgms - Part-1

The document contains implementations of various sorting algorithms (Selection Sort, Quick Sort, and Merge Sort) and graph algorithms (Kruskal’s and Prim’s) in C. Each program includes functions for sorting or finding minimum spanning trees, along with timing mechanisms to measure performance. The algorithms demonstrate different approaches to sorting and graph traversal, showcasing their respective efficiencies.
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/ 7

Analysis & Design of Algorithms Lab

Program 1: Selection Sort


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void swap(int* a, int* b) {


int tmp = *a;
*a = *b;
*b = tmp;
}

void selectionSort(int arr[], int n) {


long int i, j, midx = 0;
for (i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
midx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[midx])
midx = j;
}
// Swap the found minimum element with the first element
swap(&arr[midx], &arr[i]);
}
}

int main() {
int n = 10000;
int it = 0;

int tim3[10];
printf("A size Selection\n");

while (it++ < 10) {


int c[n];
for (int i = 0; i < n; i++) {
int no = rand() % n + 1;
c[i] = no;
}

clock_t start, end;


start = clock();
selectionSort(c, n);
end = clock();

tim3[it] = ((double)(end - start));

printf("%d, %d\n", n, tim3[it]);


n += 10000;
}

return 0;
}
Program 2: Quick Sort
• Hoare partition scheme: Uses first element as pivot
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void swap(int* a, int* b) {


int temp = *a;
*a = *b;
*b = temp;
}

int partition(int arr[], int low, int high) {


int pivot = arr[low];
int i = low;
int j = high;

while (i < j) {
// Find the first element greater than the pivot (from the left)
while (arr[i] <= pivot && i <= high - 1) i++;

// Find the first element smaller than the pivot (from the right)
while (arr[j] > pivot && j >= low + 1) j--;

if (i < j) swap(&arr[i], &arr[j]);


}

swap(&arr[low], &arr[j]);
return j;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int partitionIndex = partition(arr, low, high);

quickSort(arr, low, partitionIndex - 1);


quickSort(arr, partitionIndex + 1, high);
}
}

int main() {
int n, i;
clock_t start, end;
double time_taken;

printf("Enter the number of elements to be sorted: ");


scanf("%d", &n);

int arr[n];
printf("Generating %d random elements...\n", n);

srand(time(NULL));

for (i = 0; i < n; i++) arr[i] = rand() % 10000;

printf("Sorting the array using Quick Sort...\n");

start = clock(); // Start timer


quickSort(arr, 0, n - 1);
end = clock(); // Stop timer

time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;

printf("\nSorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nTime taken to sort: %.6f seconds\n", time_taken);
return 0;
}

• Lomuto partition scheme: Uses last element as pivot


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void quick_sort(int[], int, int);


int partition(int[], int, int);

int main() {
int n, i;
clock_t start, end;
double time_taken;

printf("Enter the number of elements to be sorted: ");


scanf("%d", &n);

int arr[n];
printf("Generating %d random elements...\n", n);

srand(time(NULL));

for (i = 0; i < n; i++) arr[i] = rand() % 10000;

printf("Sorting the array using Quick Sort...\n");

start = clock(); // Start timer


quick_sort(arr, 0, n - 1);
end = clock(); // Stop timer

printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;


printf("\nTime taken to sort %d elements: %lf seconds\n", n, time_taken);

return 0;
}

void quick_sort(int arr[], int low, int high) {


int pivot_index;
if (low < high) {
pivot_index = partition(arr, low, high);
quick_sort(arr, low, pivot_index - 1);
quick_sort(arr, pivot_index + 1, high);
}
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1, j, temp;

for (j = low; j < high; j++) {


if (arr[j] <= pivot) {
i++;
// Swap arr[i] and arr[j]
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

// Swap arr[i + 1] and arr[high]


temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}

Program 3: Merge Sort


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void merge_sort(int[], int, int);


void merge(int[], int, int, int);

int main() {
int n, i;
clock_t start, end;
double time_taken;

printf("Enter the number of elements to be sorted: ");


scanf("%d", &n);

int arr[n];
printf("Generating %d random elements...\n", n);
srand(time(NULL)); // Seed for random number generator

for(i = 0; i < n; i++) arr[i] = rand() % 10000;

printf("Sorting the array using Merge Sort...\n");


start = clock(); // Start timer

merge_sort(arr, 0, n - 1);

end = clock(); // Stop timer

printf("Sorted array:\n");
for(i = 0; i < n; i++) printf("%d ", arr[i]);

time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;


printf("\nTime taken to sort %d elements: %lf seconds", n, time_taken);
return 0;
}
void merge_sort(int arr[], int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
merge_sort(arr, low, mid);
merge_sort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}

void merge(int arr[], int low, int mid, int high) {


int i = low, j = mid + 1, k = 0;
int temp[high - low + 1];

while(i <= mid && j <= high) {


if(arr[i] <= arr[j]) temp[k++] = arr[i++];
else temp[k++] = arr[j++];
}
while(i <= mid) temp[k++] = arr[i++];
while(j <= high) temp[k++] = arr[j++];
for(i = low, k = 0; i <= high; i++, k++) arr[i] = temp[k];
}

Program 4: Kruskal’s Algorithm


#include <stdio.h>
#define MAX 100
#define INF 9999
typedef struct {
int u, v, cost;
} Edge;
int parent[MAX];

int find(int i) {
while (parent[i])
i = parent[i];
return i;
}

int unionSet(int i, int j) {


int a = find(i);
int b = find(j);
if (a != b) {
parent[a] = b;
return 1;
}
return 0;
}
int main() {
int n, e, i, j, u, v, cost;
Edge edges[MAX];
int totalCost = 0;
printf("Enter number of vertices: ");
scanf("%d", &n);
printf("Enter number of edges: ");
scanf("%d", &e);
printf("Enter edges (u v cost):\n");
for (i = 0; i < e; i++) {
scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].cost);
}
for (i = 0; i < e - 1; i++) {
for (j = 0; j < e - i - 1; j++) {
if (edges[j].cost > edges[j + 1].cost) {
Edge temp = edges[j];
edges[j] = edges[j + 1];
edges[j + 1] = temp;
}
}
}
printf("Edges in Minimum Cost Spanning Tree:\n");
for (i = 0, j = 0; i < e && j < n - 1; i++) {
u = edges[i].u;
v = edges[i].v;
cost = edges[i].cost;

if (unionSet(u, v)) {
printf("%d - %d : %d\n", u, v, cost);
totalCost += cost;
j++;
}
}

printf("Total cost of Minimum Spanning Tree: %d\n", totalCost);


return 0;
}

Program 5: Prim’s Algorithm


#include <stdio.h>
#include <limits.h>
#define MAX 100
#define INF 9999
int minKey(int key[], int mstSet[], int n) {
int min = INF, min_index;
for (int v = 0; v < n; v++)
if (mstSet[v] == 0 && key[v] < min) min = key[v], min_index = v;
return min_index;
}

void printMST(int parent[], int graph[MAX][MAX], int n) {


int cost = 0;
printf("Edge \tWeight\n");
for (int i = 1; i < n; i++) {
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
cost += graph[i][parent[i]];
}
printf("Total cost of Minimum Spanning Tree: %d\n", cost);
}

void primMST(int graph[MAX][MAX], int n) {


int parent[MAX]; // Array to store constructed MST
int key[MAX]; // Key values used to pick minimum weight edge
int mstSet[MAX]; // To represent set of vertices not yet included in MST
for (int i = 0; i < n; i++) {
key[i] = INF;
mstSet[i] = 0;
}
key[0] = 0; // Make key 0 so that this vertex is picked as first
vertex
parent[0] = -1; // First node is always root of MST
for (int count = 0; count < n - 1; count++) {
int u = minKey(key, mstSet, n);
mstSet[u] = 1;
for (int v = 0; v < n; v++) {
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}
printMST(parent, graph, n);
}

int main() {
int n, graph[MAX][MAX];
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix (0 for no edge):\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
if (graph[i][j] == 0)
graph[i][j] = INF;
}
}
printf("\nMinimum Cost Spanning Tree using Prim's Algorithm:\n");
primMST(graph, n);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy