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

ARRAY

The document contains two C programs: one for linear search and another for binary search using bubble sort. The linear search program prompts the user to input an array and a value to search, indicating whether the value is found. The binary search program sorts the array using bubble sort before searching for a specified item, also indicating if it is found or not.

Uploaded by

Samira Tasneem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

ARRAY

The document contains two C programs: one for linear search and another for binary search using bubble sort. The linear search program prompts the user to input an array and a value to search, indicating whether the value is found. The binary search program sorts the array using bubble sort before searching for a specified item, also indicating if it is found or not.

Uploaded by

Samira Tasneem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ARRAY

Linear Search
#include<stdio.h>
int create()
{
int i, n;
printf("Enter size of array: ");
scanf("%d", &n);
int a[n];
printf("Enter values: ");
for(i=0; i<n; i++){
scanf("%d",&a[i]);
}

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


printf("%d ", a[i]);
}
int search;
printf("\nEnter the value: ");
scanf("%d", &search);
int f=0;
for(i=0; i<n; i++){
if(a[i]==search){
f++;
break;
}
}
if(f==0){
printf("Not found");
}

else{
printf("Found");
}

int main()
{
create();
}
Binary Search with Bobble Sort
#include<stdio.h>
void searchItem(int n, int a[], int searchitem);
void bubblesort(int n, int a[])
{
int i,j,temp;
for(i=0; i<n-1; i++){
for(j=0; j<n-1-i; j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void searchItem(int n, int a[], int searchitem)
{
int start, end, mid;
start=0;
end=n-1;
while(start<=end){
mid=(start+end)/2;
if(a[mid]==searchitem){
printf("Found");
return;
}
else if(a[mid]<searchitem){
start=mid+1;
}
else{
end=mid-1;
}
}
printf("Not found");
}
void main()
{
int n,i,j,searchitem;
printf("Enter the size of array: ");
scanf("%d", &n);

int a[n];
printf("Enter the value of array: ");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
bubblesort(n,a);

printf("Sorted array: ");


for(i=0; i<n; i++){
printf("%d ", a[i]);
}
printf("\n");
printf("Enter the searchitem: ");
scanf("%d", &searchitem);

searchItem(n,a,searchitem);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy