Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using linear search algorithm trace the following list to search for the numbers

ID: 3729150 • Letter: U

Question

Using linear search algorithm trace the following list to search for the numbers (6, 5 and, 55) and calculate the number of comparisons? 5. 23 15 70 44 3 35 6. Using sequentialsearch algorithm trace the following list to search for the character P and M: 7. Use recursion write Function linearSearch () that search for a specific element using tir (sequential) search algorithm. Write a C program that use this function. 8. Write a C program that allow the user to enter array (character or integer) elements, the program contain the functions a. Function primary () that ensure that the array's elements are not duplicate. b. Function linear () that search for a specific element using linear (sequential) search algorithm.

Explanation / Answer

5)

#include<stdio.h>

int linear_search(int arr[],int ele){
int comparison = 0;
int check=0;
int i;
for(i = 0;i<8;i++){
comparison++;
if(arr[i]==ele){
check=1;
break;
}
}
printf("The no of comparisons for element %d = %d ",ele,comparison);
if(check!=1){
return -1;
}
return i;
}

int main() {
int arr[]={55,23,6,15,70,44,3,35};
int com = linear_search(arr,6);
if(com==-1){
printf("NOT FOUND ");
}
else{
printf("ELEMENT FOUND IN POSITION %d ",com);
}
com = linear_search(arr,5);
if(com==-1){
printf("NOT FOUND ");
}
else{
printf("ELEMENT FOUND IN POSITION %d ",com);
}
com = linear_search(arr,55);
if(com==-1){
printf("NOT FOUND ");
}
else{
printf("ELEMENT FOUND IN POSITION %d ",com);
}
return 0;
}

6)

#include<stdio.h>

int sequential_search(char arr[],char ele){
int comparison = 0;
int check=0;
int i;
for(i = 0;i<8;i++){
comparison++;
if(arr[i]==ele){
check=1;
break;
}
}
printf("The no of comparisons for character %c = %d ",ele,comparison);
if(check!=1){
return -1;
}
return i;
}

int main() {
char arr[]={'U','A','F','N','L','B','J','P'};
int com = sequential_search(arr,'M');
if(com==-1){
printf("NOT FOUND ");
}
else{
printf("CHARACTER FOUND IN POSITION %d ",com);
}
com = sequential_search(arr,'P');
if(com==-1){
printf("NOT FOUND ");
}
else{
printf("CHARACTER FOUND IN POSITION %d ",com);
}
return 0;
}

7)

#include<stdio.h>

int linear_search(int arr[],int len,int ele)
{
int position;
if (arr[len] == ele)
{
return len;
}
else if (len == -1)
{
return -1;
}
else
{
return (position = linear_search(arr, len - 1, ele));
}
}

int main() {
int arr[]={55,23,6,15,70,44,3,35};
int com = linear_search(arr,8,6);
if(com==-1){
printf("NOT FOUND ");
}
else{
printf("ELEMENT FOUND IN POSITION %d ",com);
}
com = linear_search(arr,8,5);
if(com==-1){
printf("NOT FOUND ");
}
else{
printf("ELEMENT FOUND IN POSITION %d ",com);
}
com = linear_search(arr,8,55);
if(com==-1){
printf("NOT FOUND ");
}
else{
printf("ELEMENT FOUND IN POSITION %d ",com);
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote