Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PRANAV GAUR

22BCE2634
❖Linear Search
❖ Aim
Implementing linear search using C language.
❖ Algorithm
1. Start the program.
2. Declare an integer array arr and initialize it with some values.
3. Declare integer variables n and x to represent the size of the array and the element
to search, respectively.
4. Call the linearSearch function with the array, size of array, and the element to
search as parameters.
5. Inside the linearSearch function:
a. Declare an integer variable i and initialize it to 0.
b. Use a for loop to iterate over the array elements:
i. If the current element is equal to x, return the current index i.
c. If the element is not found in the array, return -1.
6. Print the index of the element found in the array or a message indicating the
element was not found.
7. End the program.
❖ Program Code
#include <stdio.h>

int linearSearch(int arr[], int n, int x) {


int i;
for (i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}

int main() {
int arr[] = { 2, 4, 6, 8, 10, 12 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 8;
int index = linearSearch(arr, n, x);
if (index == -1)
printf("Element not found in the array");
else
printf("Element found at index %d", index);
return 0;
}

❖ Output

❖ Result
Implementation of linear search using c language complete.
❖Binary Search
❖ Aim
Implementing binary search using C language.
❖ Algorithm
1. Start the program.
2. Declare an integer array arr and initialize it with some values.
3. Declare integer variables n and x to represent the size of the array and the element
to search, respectively.
4. Call the binarySearch function with the array, size of array, and the element to
search as parameters.
5. Inside the binarySearch function:
a. Declare integer variables low, high, and mid, and initialize low to 0 and high to
(n-1).
b. Use a while loop to perform binary search:
i. Calculate the middle index of the array by taking the average of low and high,
and store it in mid.
ii. If the value at the mid index is equal to x, return mid.
iii. If the value at the mid index is less than x, set low to mid+1.
iv. If the value at the mid index is greater than x, set high to mid-1.
c. If the element is not found in the array, return -1.
6. Print the index of the element found in the array or a message indicating the
element was not found.
7. Repeat steps 2-6 with another array and element to search.
8. End the program.
❖ Program Code
#include <stdio.h>

int binarySearch(int arr[], int n, int x) {


int low = 0;
int high = n-1;
int mid;
while(low<=high){
mid = (low + high)/2;
if(x == arr[mid]){
return mid;
}
else if(arr[mid] < x){
low = mid+1;
}
else{
high = mid-1;
}
}
return -1;
}

int main() {
int arr[] = { 2, 4, 6, 8, 10, 12 ,33};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 8;
int index = binarySearch(arr, n, x);
if (index == -1)
printf("Element not found in the array\n");
else
printf("Element found at index %d\n", index);
int arr2[] = { 3, 11, 16, 28,37, 49, 61 };
int n2 = sizeof(arr) / sizeof(arr[0]);
int x2 = 61;
int index2 = binarySearch(arr2, n2, x2);
if (index2 == -1)
printf("Element not found in the array\n");
else
printf("Element found at index %d\n", index2);
return 0;
}

❖ Output

❖ Result
Implementation of binary search using c language complete.

You might also like