Name:Alok Kumar Sem: Mca (3) Roll:2301040
Name:Alok Kumar Sem: Mca (3) Roll:2301040
Week 1:
Q1:- Given an array of nonnegative integers, design a linear algorithm and implement it using
a program to find whether given key element is present in the array or not. Also, find total
number of comparisons for each input case. (Time Complexity = O(n), where n is the size of
input)
CODE:
#include <stdio.h>
int count(int a[], int n, int key) {
int count = 0;
for (int i= 0; i < n; i++) {
if (a[i] == key) {
count++;
}
}
return count;
}
int main() {
int test;
printf("Enter Number of test cases:\n");
scanf("%d", &test);
int n;
printf("Enter Size of the array:\n");
scanf("%d", &n);
int a[n];
scanf("%d", &a[i]);
}
int key;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Q2:- Given an already sorted array of positive integers, design an algorithm and implement it
using a program to find whether given key element is present in the array or not. Also, find
total number of comparisons for each input case. (Time Complexity = O(nlogn), where n is the
size of input).
CODE:-
#include <stdio.h>
int binarySearch(int arr[ ], int left, int right, int key, int *comparisons) {
while (left <= right) {
(*comparisons)++;
int mid = left + (right - left) / 2;
if (arr[mid] == key)
return mid;
return -1;
}
int main() {
int n, key, result, comparisons = 0;
scanf("%d", &arr[i]);
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
if (result != -1) {
printf("Key %d found at index %d.\n", key, result);
}
else {
printf("Key %d not found in the array.\n", key);
}
printf("Total number of comparisons: %d\n", comparisons);
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
OUTPUT:-
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Q3:- Given an already sorted array of positive integers, design an algorithm and implement it
using a program to find whether a given key element is present in the sorted array or not. For
an array arr[n], search at the indexes arr[0], arr[2], arr[4],.....,arr[2k] and so on. Once the
interval (arr[2k] < key < arr[ 2k+1] ) is found, perform a linear search operation from the index
2k to find the element searching).
int prev = 0;
// Finding the block where the element might be present
while (arr[(step < n ? step : n) - 1] < key) {
(*comparisons)++;
prev = step;
step += sqrt(n);
if (prev >= n)
return -1;
}
// Performing linear search within the block
}
// If element is found
(*comparisons)++;
if (arr[prev] == key)
return prev;
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
int T;
printf("Enter the number of test cases: ");
scanf("%d", &T);
while (T--) {
int n, key, result, comparisons = 0;
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Week 2:
Q1:- Given a sorted array of positive integers containing few duplicate elements, design an
algorithm and implement it using a program to find whether the given key element is present
in the array or not. If present, then also find the number of copies of given key. (Time
Complexity = O(log n)).
CODE:
#include <stdio.h>
int firstOccurrence(int a[], int n, int key) {
result = mid;
high = mid - 1; // Look for earlier occurrence
} else if (a[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
result = mid;
low = mid + 1; // Look for later occurrence
} else if (a[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
// Function to count frequency using binary search
int count(int a[], int n, int key) {
int first = firstOccurrence(a, n, key);
if (first == -1) {
return 0; // Key not found
}
int last = lastOccurrence(a, n, key);
return last - first + 1; // Frequency calculation
}
int main() {
int t;
printf("Enter Number of times to execute\n");
scanf("%d", &t);
}
int key;
printf("Enter the element to be searched:\n");
scanf("%d", &key);
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Q2:- Given a sorted array of positive integers, design an algorithm and implement it using a
program to find three indices i, j, k such that arr[i] + arr[j] = arr[k].
CODE:
#include <stdio.h>
// Function to find i, j, k such that arr[i] + arr[j] = arr[k]
} else {
j--;
}
}
}
scanf("%d", &T);
while (T--) {
int n;
// Input size of array
printf("Enter the size of the array: ");
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
scanf("%d", &n);
int arr[n];
// Input array elements
findIndices(arr, n);
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Q3:- Given an array of nonnegative integers, design an algorithm and a program to count the
number of pairs of integers such that their difference is equal to a given key, K.
CODE:
#include <stdio.h>
// Function to count pairs with a given difference K
return count;
}
int main() {
int T;
printf("Enter the number of test cases: ");
scanf("%d", &T);
while (T--) {
int n, K;
printf("%d\n", result);
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Week 3:
Q1: Given an unsorted array of integers, design an algorithm and a program to sort the array
using insertion sort. Your program should be able to find number of comparisons and shifts (
shifts -total number of times the array elements are shifted from their place) required for
sorting the array.
CODE:
#include <stdio.h>
arr[j + 1] = arr[j];
j--;
(*shifts)++;
}
arr[j + 1] = key;
(*shifts)++;
if (j >= 0) (*comparisons)++;
}
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
int main() {
int T;
printf("Enter number of test cases:\n");
scanf("%d", &T);
for (int t = 0; t < T; t++) {
int n;
printf("Enter the size of the array:\n");
scanf("%d", &n);
int arr[n];
printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int comparisons = 0, shifts = 0;
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Q2:- Given an unsorted array of integers, design an algorithm and implement a program to
sort this array using selection sort. Your program should also find number of comparisons and
number of n swaps required.
CODE:
#include <stdio.h>
void compSwap(int arr[], int n) {
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
swap++;
}
}
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Comparisons: %d\n", comp);
printf("Swaps: %d\n", swap);
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
int main() {
int T;
while (T--) {
int n;
scanf("%d", &arr[i]);
}
compSwap(arr, n);
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Q3:- Given an unsorted array of positive integers, design an algorithm and implement it using
a program to find whether there are any duplicate elements in the array or not. (use sorting)
(Time Complexity = O(n log n)).
CODE:
#include <stdio.h>
#include <stdlib.h>
}
return 0;
}
int main() {
int T;
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
scanf("%d", &arr[i]);
}
if (hasDuplicates(arr, n)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Week 4:
Q1:- Given an unsorted array of integers, design an algorithm and implement it using a
program to sort an array of elements by dividing the array into two subarrays and combining
these subarrays after sorting each one of them. Your program should also find number of
comparisons and inversions during sorting the array.
CODE:
#include <stdio.h>
void merge(int arr[], int temp[], int left, int mid, int right, int* comparisons, int* inversions) {
int i = left;
int j = mid + 1;
int k = left;
temp[k++] = arr[j++];
(*inversions) += (mid - i + 1);
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
}
}
while (T--) {
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Q2:- Given an unsorted array of integers, design an algorithm and implement it using a
program to sort an array of elements by partitioning the array into two subarrays based on a
pivot element such that one of the sub array holds values smaller than the pivot element while
another sub array holds values greater than the pivot element. Pivot element should be
selected randomly from the array. Your program should also find number of comparisons and
swaps required for sorting the array.
CODE:
#include <stdio.h>
void merge(int arr[], int temp[], int left, int mid, int right, int* comparisons, int* inversions) {
int i = left;
int j = mid + 1;
int k = left;
while (i <= mid && j <= right) {
(*comparisons)++;
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
(*inversions) += (mid - i + 1);
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (i = left; i <= right; i++) {
arr[i] = temp[i];
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
void mergeSort(int arr[], int temp[], int left, int right, int* comparisons, int* inversions) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, temp, left, mid, comparisons, inversions);
mergeSort(arr, temp, mid + 1, right, comparisons, inversions);
while (T--) {
int n; // Size of the array
OUTPUT:
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
Q3:- Given an unsorted array of integers, design an algorithm and implement it using a
program to find Kth smallest or largest element in the array. (Worst case Time Complexity =
O(n)).
CODE:
#include <stdio.h>
// Function to count pairs with a given difference K
return count;
}
int main() {
int T;
scanf("%d", &arr[i]);
}
// Input the key K
printf("Enter the key difference K: ");
scanf("%d", &K);
printf("%d\n", result);
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)
OUTPUT: