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

Dsa File Split

The document presents two algorithms for searching elements in an array: linear searching and binary searching. The linear search example demonstrates searching for an element in a fixed-size array, while the binary search example requires a sorted array and allows for dynamic input of the number of elements. Both algorithms provide feedback on whether the searched element is found and its position in the array.

Uploaded by

ayush22092008
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)
4 views3 pages

Dsa File Split

The document presents two algorithms for searching elements in an array: linear searching and binary searching. The linear search example demonstrates searching for an element in a fixed-size array, while the binary search example requires a sorted array and allows for dynamic input of the number of elements. Both algorithms provide feedback on whether the searched element is found and its position in the array.

Uploaded by

ayush22092008
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

AIM : Linear Searching

#include <stdio.h>

int main()

int arr[5] = {10, 25, 30, 45, 50};

int i, key, found = 0;

printf("Enter the element to search: ");

scanf("%d", &key);

for(i = 0; i < 5; i++)

if(arr[i] == key)

printf("Element %d found at position %d\n", key, i + 1);

found = 1;
break;

if(found == 0)

printf("Element %d not found in the array\n", key);

return 0;

OUTPUT :
AIM: Binary Searching
#include <stdio.h>

int main() {

int arr[100], n, i, key;

int low, high, mid, found = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter %d elements in sorted order:\n", n);

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

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

printf("Enter element to search: ");

scanf("%d", &key);

low = 0;

high = n - 1;

while(low <= high) {

mid = (low + high) / 2;

if(arr[mid] == key) {

found = 1;

break;

else if(arr[mid] < key) {

low = mid + 1;

else {

high = mid - 1;
}

if(found == 1)

printf("Element found at position %d\n", mid + 1);

else

printf("Element not found\n");

return 0;

OUTPUT:

You might also like