0% found this document useful (0 votes)
5 views3 pages

Assignment 4

Uploaded by

csaiml23094
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)
5 views3 pages

Assignment 4

Uploaded by

csaiml23094
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/ 3

Linear Search

#include<stdio.h>
#include<stdbool.h>
int main(){
int arr[100];
int n ;
printf("Size of Array:");
scanf("%d",&n);
for(int i=0; i<n; i++){
scanf("%d",&arr[i]);
}
for(int i=0; i<n; i++){
printf("%d ",arr[i]);
}
int x=7;
bool flag = false;
for(int i=0; i<n; i++){
if(arr[i]==x)
flag = true;
}
if(flag==true) printf("\n%d Exists",x);
else printf("\n%d Does not Exist",x);
return 0;
}

Output:
Size of Array:5
47382
47382
7 Exists

Binary search
#include<stdio.h>
#include<stdbool.h>
int binarysearch(int arr[] , int n , int keyword){
int start = 0;
int end = n-1;
while(start <= end){
int mid = (start+end)/2;

if(arr[mid]==keyword){
return 1;
}
if(keyword>mid){
start = mid +1;
}
if(keyword<mid){
end = mid;
}
}
return 0;
}
int main(){
int keyword = 5;
int arr[] = {1,2,3,4,5,6};
int index = binarysearch(arr,6,keyword);
if(index){
printf("Keyword is Present");
}
else{
printf("Keyword is Absent");
}
}

Output:
Keyword is Present

Insertion sort
#include<stdio.h>
int main(){
int arr[7] = {7,4,5,9,8,2,1};
int n = 7;
for(int i=1; i<=n-1; i++){
int j=i;
while(j>=1 && arr[j]<arr[j-1]){
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
for(int i=0; i<n; i++){
printf("%d\n",arr[i]);
}
return 0;
}

Output:
1
2
4
5
7
8
9

You might also like