AOA 1-8 Merged
AOA 1-8 Merged
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() {
scanf("%d", &n);
scanf("%d", &a[i]);
temp = a[i];
j = i - 1;
a[j + 1] = a[j];
j--;
a[j + 1] = temp;
printf("%d\t", a[i]);
} else {
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>
*a = *b;
*b = temp;
// Partition function
while (i < j) {
i++;
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]);
// QuickSort function
if (l < h) {
int j = partition(arr, l, h); // Partition the array and get the pivot index
}
printf("\n");
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
quicksort(arr, 0, n - 1);
printArray(arr, n);
return 0;
}
OUTPUT :
BINARY SEARCH ALGORITHM:
INPUT :
#include <stdio.h>
if (arr[mid] == key) {
return mid;
low = mid + 1;
} else {
high = mid - 1;
return -1;
int main() {
int n;
scanf("%d", &n);
int arr[n];
int key;
scanf("%d", &key);
if (index == -1) {
} else {
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;
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
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);
return 0;
}