0% found this document useful (0 votes)
26 views39 pages

Name:Alok Kumar Sem: Mca (3) Roll:2301040

Uploaded by

lalshalam123
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)
26 views39 pages

Name:Alok Kumar Sem: Mca (3) Roll:2301040

Uploaded by

lalshalam123
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/ 39

NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

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);

for (int k = 0; k < test; k++) {

int n;
printf("Enter Size of the array:\n");
scanf("%d", &n);
int a[n];

printf("Enter Array Elements:\n");


for (int i = 0; i < n; i++) {
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

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

printf("Enter the element to be searched:\n");


scanf("%d", &key);
printf("Frequency of %d is : %d\n", key, count(a, n, key));
}
return 0;

}
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;

else if (arr[mid] < key)


left = mid + 1;
else
right = mid - 1;
}

return -1;
}

int main() {
int n, key, result, comparisons = 0;

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


scanf("%d", &n);
int arr[n];
printf("Enter the sorted array elements:\n");
for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

printf("Enter the key to be searched: ");


scanf("%d", &key);

result = binarySearch(arr, 0, n - 1, key, &comparisons);

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).

CODE: #include <stdio.h>


#include <math.h>
// Function to perform Jump Search
int jumpSearch(int arr[], int n, int key, int *comparisons) {
int step = sqrt(n);

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

while (arr[prev] < key) {


(*comparisons)++;
prev++;
if (prev == (step < n ? step : n)) // Reached end of the block or array
return -1;

}
// If element is found
(*comparisons)++;
if (arr[prev] == key)
return prev;
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

return -1; // Element not found


}
int main() {

int T;
printf("Enter the number of test cases: ");
scanf("%d", &T);
while (T--) {
int n, key, result, comparisons = 0;

// Input size of array


printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the sorted array elements:\n");

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


scanf("%d", &arr[i]);
}
printf("Enter the key to be searched: ");
scanf("%d", &key);

// Perform Jump Search


result = jumpSearch(arr, n, key, &comparisons);
if (result != -1) {
printf("Present %d\n", comparisons);
} else {

printf("Not Present %d\n", comparisons);


}
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

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) {

int low = 0, high = n - 1;


int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (a[mid] == key) {

result = mid;
high = mid - 1; // Look for earlier occurrence
} else if (a[mid] < key) {
low = mid + 1;
} else {

high = mid - 1;
}
}
return result;
}

// Function to perform binary search for the last occurrence


int lastOccurrence(int a[], int n, int key) {
int low = 0, high = n - 1;
int result = -1;
while (low <= high) {

int mid = low + (high - low) / 2;


if (a[mid] == key) {
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

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);

for (int r = 0; r < t; r++) {


int n;
printf("Enter Size of the array:\n");
scanf("%d", &n);
int a[n];
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

printf("Enter Array Elements (sorted):\n");


for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);

}
int key;
printf("Enter the element to be searched:\n");
scanf("%d", &key);

// Call the binary search-based count function


printf("Frequency of %d is : %d\n", key, count(a, n, key));
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

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]

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


for (int k = n - 1; k >= 2; k--) { // k starts from the end of the array
int i = 0;
int j = k - 1;
while (i < j) {

if (arr[i] + arr[j] == arr[k]) {


printf("%d %d %d\n", i, j, k);
return;
} else if (arr[i] + arr[j] < arr[k]) {
i++;

} else {
j--;
}
}
}

printf("No sequence found\n");


}
int main() {
int T;
printf("Enter the number of test cases: ");

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

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


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Find the indices i, j, k

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

int countPairsWithDifference(int arr[], int n, int K) {


int count = 0;
// Iterate over each pair
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {

if (arr[j] - arr[i] == K || arr[i] - arr[j] == K) {


count++;
}
}
}

return count;
}
int main() {
int T;
printf("Enter the number of test cases: ");

scanf("%d", &T);
while (T--) {
int n, K;

// Input size of array

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


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

// Input array elements


NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

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


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);

// Input the key K


printf("Enter the key difference K: ");
scanf("%d", &K);

// Count pairs with difference K


int result = countPairsWithDifference(arr, n, K);

// Output the result

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>

void insertionSort(int arr[], int n, int *comparisons, int *shifts) {


*comparisons = 0;
*shifts = 0;

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

int key = arr[i];


int j = i - 1;

while (j >= 0 && arr[j] > key) {


(*comparisons)++;

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;

// Sort the array using insertion sort


insertionSort(arr, n, &comparisons, &shifts);

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

printf("comparisons = %d\n", comparisons);


printf("shifts = %d\n", shifts);
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

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) {

int comp = 0, swap = 0, temp;


for(int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
comp++;

if (arr[j] < arr[min_idx]) {


min_idx = j;
}
}
if (min_idx != i) {

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;

printf("Enter the number of test cases: ");


scanf("%d", &T);

while (T--) {
int n;

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


scanf("%d", &n);
int arr[n];
printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {

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>

int compare(const void* a, const void* b) {


return (*(int*)a - *(int*)b);
}
int hasDuplicates(int arr[], int n)
{

qsort(arr, n, sizeof(int), compare);


for (int i = 1; i < n; i++) {
if (arr[i] == arr[i - 1]) {
return 1; // Duplicate found
}

}
return 0;
}
int main() {
int T;

printf("Enter the number of test cases: ");


scanf("%d", &T);
while (T--) {
int n;
printf("\nEnter the size of the array: ");

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;

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)

}
}

// Function to implement merge sort and count comparisons and inversions


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);


merge(arr, temp, left, mid, right, comparisons, inversions);
}
}
int main() {

int T; // Number of test cases


printf("Enter the number of test cases: ");
scanf("%d", &T);

while (T--) {

int n; // Size of the array


printf("\nEnter the size of the array: ");
scanf("%d", &n);
int arr[n], temp[n];
// Input the array elements

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


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int comparisons = 0, inversions = 0;
// Sort the array and count comparisons and inversions
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

mergeSort(arr, temp, 0, n - 1, &comparisons, &inversions);


// Output the sorted array
printf("Sorted array: ");

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


printf("%d ", arr[i]);
}
printf("\n");
// Output the total number of comparisons and inversions

printf("Total comparisons: %d\n", comparisons);


printf("Total inversions: %d\n", inversions);
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

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)

// Function to implement merge sort and count comparisons and inversions

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);

merge(arr, temp, left, mid, right, comparisons, inversions);


}
}
int main() {
int T; // Number of test cases

printf("Enter the number of test cases: ");


scanf("%d", &T);

while (T--) {
int n; // Size of the array

printf("\nEnter the size of the array: ");


scanf("%d", &n);
int arr[n], temp[n];
// Input the array elements
printf("Enter the elements of the array: ");

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


scanf("%d", &arr[i]);
}
int comparisons = 0, inversions = 0;
// Sort the array and count comparisons and inversions
mergeSort(arr, temp, 0, n - 1, &comparisons, &inversions);
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

// Output the sorted array


printf("Sorted array: ");
for (int i = 0; i < n; i++) {

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


}
printf("\n");
// Output the total number of comparisons and inversions
printf("Total comparisons: %d\n", comparisons);

printf("Total inversions: %d\n", inversions);


}
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 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

int countPairsWithDifference(int arr[], int n, int K) {


int count = 0;
// Iterate over each pair
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {

if (arr[j] - arr[i] == K || arr[i] - arr[j] == K) {


count++;
}
}
}

return count;
}

int main() {
int T;

printf("Enter the number of test cases: ");


scanf("%d", &T);
while (T--) {
int n, K;
// Input size of array

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


scanf("%d", &n);
int arr[n];
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

// Input array elements


printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);
}
// Input the key K
printf("Enter the key difference K: ");
scanf("%d", &K);

// Count pairs with difference K


int result = countPairsWithDifference(arr, n, K);

// Output the result

printf("%d\n", result);
}
return 0;
}
NAME:ALOK KUMAR SEM: MCA(3) ROLL:2301040(6)

OUTPUT:

You might also like