DSA 1
DSA 1
DSA 1
Set A
a) Create a random array of n integers. Accept a value x from user and use linear search algorithm to
check whether the number is present in the array or not and output the position if the number is
present.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
srand(time(0));
if (arr[i] == x)
return i;
return -1;
int main()
int n, x, position;
scanf("%d", &n);
int arr[i];
generateRandomArray(arr, n);
printf("\n");
scanf("%d", &x);
if (position != -1)
else
return 0;
}
b) Accept n values in array from user. Accept a value x from user and use sentinel linear search
algorithm to check whether the number is present in the array or not and output the position if the
number is present.
#include <stdio.h>
#include <stdlib.h>
void main()
int *arr;
int i, n, k, index;
scanf("%d", &n);
arr = (int*)malloc(sizeof(int));
scanf("%d", &arr[i]);
scanf("%d", &k);
if (index == 0)
else
if (num == array[i])
return i;
}
c) Accept n sorted values in array from user. Accept a value x from user and use binary search
algorithm to check whether the number is present in sorted array or not and output the position if
the number is present.
#include <stdio.h>
#include <stdlib.h>
void main()
int *arr;
int i, n, k, index;
scanf("%d", &n);
arr = (int*)malloc(sizeof(int));
scanf("%d", &arr[i]);
scanf("%d", &k);
if (index == 0)
else
int i=0,j=size,k;
while(i<=j)
k=(i+j)/2;
if(array[k] == num)
return(k);
else if(array[k]<num)
i=k+1;
else
j=k-1;
Set B
a) Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of
the city from user and use linear search algorithm to check whether the name is present in the file
and output the STD code, otherwise output “city not in the list”.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CITY_LENGTH 50
char line[MAX_LINE_LENGTH];
char cityName[MAX_CITY_LENGTH];
int stdCode;
if (file == NULL)
return -1;
if (strcmp(cityName, city) == 0)
{
fclose(file);
return stdCode;
fclose(file);
return -1;
int main() {
char city[MAX_CITY_LENGTH];
int stdCode;
scanf("%s", city);
if (stdCode != -1)
else
return 0;
}
b) Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name
of the city from user and use sentinel linear search algorithm to check whether the name is present
in the file and output the STD code, otherwise output “city not in the list”.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CITY_LENGTH 50
typedef struct
char city[MAX_CITY_LENGTH];
int stdCode;
City;
int i = 0;
i++;
return cities[i].stdCode;
return -1;
int main()
{
FILE *file;
char searchCity[MAX_CITY_LENGTH];
City cities[MAX_LINE_LENGTH];
int n = 0;
char line[MAX_LINE_LENGTH];
int stdCode;
if (file == NULL)
return 1;
n++;
fclose(file);
scanf("%s", searchCity);
if (stdCode != -1)
else
return 0;
}
c) Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD codes.
Accept a name of the city from user and use binary search algorithm to check whether the name is
present in the file and output the STD code, otherwise output “city not in the list”
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CITY_LENGTH 50
struct City
char city[MAX_CITY_LENGTH];
int stdCode;
};
if (cmp == 0)
return cities[mid].stdCode;
if (cmp < 0)
left = mid + 1;
else
right = mid - 1;
}
}
return -1;
int main()
FILE *file;
char searchCity[MAX_CITY_LENGTH];
int n = 0;
char line[100];
int stdCode;
n++;
fclose(file);
scanf("%s", searchCity);
if (stdCode != -1)
else
return 0;
}
Set c
b) If the file contains multiple occurrences of a given element, will binary search output the position
of first occurrence or last occurrence?
#include <stdio.h>
if (arr[mid] == target) {
} else {
return result;
} else {
return result;
int main() {
int target = 2;
if (firstOccurrence != -1) {
} else {
if (lastOccurrence != -1) {
} else {
return 0;
b) If the file contains multiple occurrences of a given element, will binary search output the position
of first occurence or last occurrence?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
if (arr[i] == target) {
int i = 0;
i++;
}
arr[size - 1] = last;
if (arr[mid] == target) {
return mid;
left = mid + 1;
right = mid - 1;
}
// Function to compare and time the search algorithms
start = clock();
end = clock();
printf("\nLinear Search: Element %d found at index %d, Time : %f sec\n", target, linearIndex,
linearTime);
start = clock();
end = clock();
printf("\nSentinel Search: Element %d found at index %d, Time : %f sec\n", target, sentinelIndex,
sentinelTime);
start = clock();
end = clock();
printf("\nBinary Search: Element %d found at index %d, Time : %f sec\n", target, binaryIndex,
binaryTime);
int main() {
arr[j + 1] = temp;
int target = 20; // Best case for linear and sentinel search
return 0;
If the file contains multiple occurrences of a given element, linear search will give the Page 12 of 57
position of the first occurrence, what modifications are required to get the last occurrence?
#include <stdio.h>
if (arr[i] == target) {
int main() {
if (lastOccurrence != -1) {
} else {
return 0;