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

Linear and Binary Search in C

data structure

Uploaded by

teamfnatic28
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)
29 views3 pages

Linear and Binary Search in C

data structure

Uploaded by

teamfnatic28
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

1.

Define Linear Search


Linear Search is a simple searching algorithm that scans each element in a list or array one by
one from the beginning until the desired element is found or the end of the list is reached. If the
target is present, its index is returned; otherwise, a "not found" result is indicated. It works on
both sorted and unsorted data.

2. Explain Binary Search and its Time Complexity


Binary Search is an efficient algorithm for finding an element in a sorted array. It repeatedly
divides the search interval in half:
Start with the entire sorted array.
Compare the target element to the middle element.
If equal, the search is successful.
If the target is less, continue the search in the left half.
If the target is greater, search the right half.
This process continues until the element is found or the interval is empty.
Time Complexity:
Best case: (if the element is at the middle in the first check)
Average and worst case: , where is the number of elements, because the
search space is halved each step.

3. Dry Run: Search for 25 in Using Binary Search


Let's perform a binary search for 25:
Initial array:
Start (left) = 0, End (right) = 4
Step 1:
Mid = (0 + 4) / 2 = 2 (integer division)
Array = 20
25 > 20 ⇒ Search the right half (left = 3, right = 4)
Step 2:
Mid = (3 + 4) / 2 = 3
Array = 25
25 == 25 ⇒ Element found at index 3

4. Implement Linear Search in C

#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key)
return i; // Return the index if found
}
return -1; // Return -1 if not found
}

int main() {
int arr[] = {10, 15, 20, 25, 30};
int n = sizeof(arr)/sizeof(arr);
int key = 25;
int result = linearSearch(arr, n, key);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}

5. Implement Binary Search in C

#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key)
return mid; // Return the index if found
else if (arr[mid] < key)
left = mid + 1; // Search right half
else
right = mid - 1; // Search left half
}
return -1; // Return -1 if not found
}

int main() {
int arr[] = {10, 15, 20, 25, 30};
int n = sizeof(arr)/sizeof(arr);
int key = 25;
int result = binarySearch(arr, n, key);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}

These examples cover fundamental searching techniques for arrays in C.

You might also like