0% found this document useful (0 votes)
15 views19 pages

AOA 1-8 Merged

The document contains various algorithms implemented in C, including Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Binary Search, Knapsack, and Dijkstra's algorithm. Each algorithm includes input prompts, sorting or searching logic, and output displays for results and performance metrics. Time complexities for best, worst, and average cases are also discussed for sorting algorithms.

Uploaded by

sharibpatel950
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)
15 views19 pages

AOA 1-8 Merged

The document contains various algorithms implemented in C, including Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Binary Search, Knapsack, and Dijkstra's algorithm. Each algorithm includes input prompts, sorting or searching logic, and output displays for results and performance metrics. Time complexities for best, worst, and average cases are also discussed for sorting algorithms.

Uploaded by

sharibpatel950
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/ 19

AIM: WRITE A PROGRAM FOR SELECTION SORT

 Input:

#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, j, smallest, temp;
int outer_for_count = 0, inner_for_count = 0, swap_count = 0;
printf("How many elements: ");
scanf("%d", &n);
// Dynamically allocate memory for the array
int *x = (int *)malloc(n * sizeof(int));
if (x == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &x[i]);
}
// Selection Sort Algorithm
for (i = 0; i < n - 1; i++) {
outer_for_count++; // Counting iterations for the outer for loop
smallest = i;
for (j = i + 1; j < n; j++) {
inner_for_count++; // Counting iterations for the inner for loop
if (x[j] < x[smallest]) {
smallest = j;
}
}
// Swap elements if necessary
if (smallest != i) {
swap_count++;
temp = x[smallest];
x[smallest] = x[i];
x[i] = temp;
}
}
// Display sorted array
printf("Elements of array after sorting: ");
for (i = 0; i < n; i++) {
printf("%d\t", x[i]);
}
// Display loop iteration counts
printf("\nTotal outer for loop iterations: %d", outer_for_count);
printf("\nTotal inner for loop iterations: %d", inner_for_count);
printf("\nTotal swap operations: %d", swap_count);
// Determine case based on swaps
if (swap_count == 0) {
printf("\nTime Complexity: O(n^2) (Best Case - Already Sorted)\n");
} else if (swap_count == n - 1) {
printf("\nTime Complexity: O(n^2) (Worst Case )\n");
} else {
printf("\nTime Complexity: O(n^2) (Average Case)\n");
}
// Free dynamically allocated memory
free(x);
return 0;
}
 OUTPUT:

BEST CASE:

WORST CASE:

AVERAGE CASE:
AIM: WRITE A PROGRAM FOR INSERTION SORT

 Input:
#include <stdio.h>

int main() {

int n, i, j, temp, a[50];

int input_for_count = 0, sorting_for_count = 0, print_for_count = 0;

int while_count = 0; // Counter for while loop

printf("How many elements: ");

scanf("%d", &n);

printf("Enter the elements of the array: ");

for (i = 0; i < n; i++) {

scanf("%d", &a[i]);

input_for_count++; // Counting input for loop iterations

for (i = 1; i < n; i++) {

sorting_for_count++; // Counting sorting for loop iterations

temp = a[i];

j = i - 1;

while (j >= 0 && a[j] > temp) {

a[j + 1] = a[j];

j--;

while_count++; // Counting while loop iterations

a[j + 1] = temp;

printf("Elements of array after sorting: ");

for (i = 0; i < n; i++) {

printf("%d\t", a[i]);

print_for_count++; // Counting print for loop iterations

// Display loop iteration counts

printf("\nTotal input for loop iterations: %d", input_for_count);

printf("\nTotal sorting for loop iterations: %d", sorting_for_count);

printf("\nTotal print for loop iterations: %d", print_for_count);

printf("\nTotal while loop iterations: %d", while_count);

// Estimating Time Complexity


if (while_count == 0) {

printf("\nTime Complexity: O(n) (Best Case - Already Sorted)\n");

} else if (while_count == (n * (n - 1)) / 2) {

printf("\nTime Complexity: O(n^2) (Worst Case - Reverse Sorted)\n");

} else {

printf("\nTime Complexity: O(n^2) (Average Case)\n");

return 0;

 OUTPUT:

BEST CASE:

WORST CASE:

AVERAGE CASE:
AIM: TO IMPLEMENT, ANALYSE AND COMPARE MERGE SORT AND QUICK SORT ALGORITHM.

Code:

#include <stdio.h>
#include <stdlib.h>
void merge(int A[], int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], R[n2];
for (int i = 0; i < n1; i++)
L[i] = A[p + i];
for (int j = 0; j < n2; j++)
R[j] = A[q + 1 + j];
int i = 0, j = 0, k = p;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
A[k] =
L[i]; i++;
} else {
A[k] =
R[j]; j++;
}
k++
while (i < n1) {
A[k] =
L[i]; i++;
k++;
}
while (j < n2) {
A[k] =
R[j]; j++;
k++;
}
}
void mergeSort(int A[], int p, int r) {
if (p < r) {
int q = (p + r) / 2;
mergeSort(A, p, q);
mergeSort(A, q + 1, r);
merge(A, p, q, r); }}
void printArray(int A[], int size)
{
for (int i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int A[n];
printf("Enter %d elements: ",
n); for (int i = 0; i < n; i++)
scanf("%d", &A[i]);
printf("Given array: ");
printArray(A, n);
mergeSort(A, 0, n - 1);
printf("Sorted array: ");
printArray(A, n);
return 0;
}
OUTPUT:
QUICK SORT

INPUT :

#include <stdio.h>

// Function to swap two elements

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

int temp = *a;

*a = *b;

*b = temp;

// Partition function

int partition(int arr[], int l, int h) {

int pivot = arr[l]; // Take the first element as pivot

int i = l; // Pointer to place the pivot correctly

int j = h; // Pointer to scan the array

while (i < j) {

// Increment i until we find an element greater than or equal to pivot

while (arr[i] <= pivot && i <= h) {

i++;

// Decrement j until we find an element smaller than or equal to pivot

while (arr[j] > pivot && j >= l) {

j--;

}
// Swap if i is less than j

if (i < j) {

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

// Swap the pivot element with the element at j to place the pivot in its
correct position

swap(&arr[l], &arr[j]);

return j; // Return the index where the pivot is placed

// QuickSort function

void quicksort(int arr[], int l, int h) {

if (l < h) {

int j = partition(arr, l, h); // Partition the array and get the pivot index

quicksort(arr, l, j); // Recursively sort the left subarray

quicksort(arr, j + 1, h); // Recursively sort the right subarray

// Function to print the array

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

}
printf("\n");

int main() {

int n;

// Take array size input from the user

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

// Take array elements input from the user

printf("Enter the elements of the array: \n");

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Call quicksort to sort the array

quicksort(arr, 0, n - 1);

// Print the sorted array

printf("Sorted array: \n");

printArray(arr, n);

return 0;

}
OUTPUT :
BINARY SEARCH ALGORITHM:

INPUT :

#include <stdio.h>

int binary_search(int arr[], int n, int key) {

int low = 0, high = n - 1;

while (low <= high) {

int mid = (low + high) / 2;

if (arr[mid] == key) {

return mid;

} else if (arr[mid] < key) {

low = mid + 1;

} else {

high = mid - 1;

return -1;

int main() {

int n;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements in sorted order: ");

for (int i = 0; i < n; i++) {


scanf("%d", &arr[i]);

int key;

printf("Enter the key to search for: ");

scanf("%d", &key);

int index = binary_search(arr, n, key);

if (index == -1) {

printf("Key not found.\n");

} else {

printf("Key found at index %d\n", index);

return 0;

OUTPUT :
#include <stdio.h>
void knapsack(int n, float weight[], float profit[], float capacity) {
float x[20], tp = 0;
int i;
float u = capacity;

for (i = 0; i < n; i++)


x[i] = 0.0;

for (i = 0; i < n; i++) {


if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];

tp = tp + (x[i] * profit[i]);

printf("\nMaximum profit is: %f", tp);


}

int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;

printf("\nEnter the no. of objects: ");


scanf("%d", &num);

printf("\nEnter the weights and profits of each object: ");


for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

printf("\nEnter the capacity of knapsack: ");


scanf("%f", &capacity);

for (i = 0; i < num; i++) {


ratio[i] = profit[i] / weight[i];
}

for (i = 0; i < num; i++) {


for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}

knapsack(num, weight, profit, capacity);


return 0;
}
#include <stdio.h>
#define INF 9999 // Maximum number of distance
int graph[10][10], dist[10], prev[10], visited[10];

int minDistance(int dist[], int visited[], int n) {


int min = INF, minIndex;
for (int v = 0; v < n; v++) {
if (visited[v] == 0 && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}
return minIndex;
}

void dijkstra(int src, int n) {


int i, count, u, v;
for (i = 0; i < n; i++) {
dist[i] = INF;
visited[i] = 0;
prev[i] = -1;
}
dist[src] = 0;
for (count = 0; count < n - 1; count++) {
u = minDistance(dist, visited, n);
visited[u] = 1;
for (v = 0; v < n; v++) {
if (!visited[v] && graph[u][v] && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
prev[v] = u;
}
}
}
}
void printPath(int prev[], int j) {
if (prev[j] == -1)
return;
printPath(prev, prev[j]);
printf("%d ", j);
}

int main() {
int n, m, i, j;
printf("Enter the number of vertices: ");
scanf("%d", &n);
printf("Enter the number of edges: ");
scanf("%d", &m);

for (i = 0; i < n; i++) {


for (j = 0; j < n; j++) {
graph[i][j] = 0;
}
}
printf("Enter the edges and their weights:\n");
for (i = 0; i < m; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
graph[u][v] = w;
}
int src;
printf("Enter the source vertex: ");
scanf("%d", &src);
dijkstra(src, n);
printf("Vertex Distance Path\n");
for (i = 0; i < n; i++) {
printf("%d %d %d ", i, dist[i], src);
printPath(prev, i);
printf("\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