0% found this document useful (0 votes)
418 views2 pages

Search Algorithms Implementation

The document outlines an assignment to create a program that allows users to search for an element in a list using either Linear or Binary search methods. It includes an algorithm detailing the steps for both search methods and provides source code in C for implementation. Additionally, it includes sample outputs demonstrating the functionality of the program.

Uploaded by

ksett92
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)
418 views2 pages

Search Algorithms Implementation

The document outlines an assignment to create a program that allows users to search for an element in a list using either Linear or Binary search methods. It includes an algorithm detailing the steps for both search methods and provides source code in C for implementation. Additionally, it includes sample outputs demonstrating the functionality of the program.

Uploaded by

ksett92
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

1

Assignment 1 :- Write a program to search an element from a list. Give user the option
to perform Linear or Binary search.

Algorithm :-
Step 1: Start
Step 2: Input the number of elements and the elements of the list
Step 3: Input the element to search
Step 4: Display menu:
Option 1: Linear Search
Option 2: Binary Search
Step 5: If user chooses Linear Search:
Step 5.1: Traverse the list from beginning
Step 5.2: Compare each element with the search item
Step 5.3: If match found, display position and exit
Step 5.4: If end of list reached, display "Not found"
Step 6: If user chooses Binary Search:
Step 6.1: Sort the list in ascending order
Step 6.2: Initialize low = 0, high = n - 1
Step 6.3: Repeat while low <= high
Step 6.3.1: mid = (low + high) / 2
Step 6.3.2: If list[mid] == search item, display position and exit
Step 6.3.3: If list[mid] < search item, set low = mid + 1
Step 6.3.4: Else, set high = mid - 1
Step 6.4: If not found, display "Not found"
Step 7: End

Source Code :-
#include <stdio.h> if (arr[j] > arr[j + 1])
{
void linearSearch(int arr[], int n, int key) { int temp = arr[j];
for (int i = 0; i < n; i++) { arr[j] = arr[j + 1];
if (arr[i] == key) { arr[j + 1] = temp;
printf("Element found at position %d\n", i + 1); }
return; }
} }
} }
printf("Element not found\n");
} void binarySearch(int arr[], int n, int key)
{
void bubbleSort(int arr[], int n) bubbleSort(arr, n); // Sort before binary search
{ int low = 0, high = n - 1;
for (int i = 0; i < n - 1; i++)
{ while (low <= high)
for (int j = 0; j < n - i - 1; j++) {
{ int mid = (low + high) / 2;

DATA STRUCTURE
2

if (arr[mid] == key)
{ printf("Enter elements:\n");
printf("Element found at position %d\n", mid + for (int i = 0; i < n; i++)
1); scanf("%d", &arr[i]);
return;
} else if (arr[mid] < key) printf("Enter element to search: ");
{ scanf("%d", &key);
low = mid + 1;
}else printf("Choose search method:\n1. Linear Search\
{ n2. Binary Search\n");
high = mid - 1; scanf("%d", &choice);
}
} if (choice == 1)
printf("Element not found\n"); linearSearch(arr, n, key);
} else if (choice == 2)
binarySearch(arr, n, key);
int main() { else
int arr[100], n, key, choice; printf("Invalid choice\n");

printf("Enter number of elements: "); return 0;


scanf("%d", &n); }

Output :-

Linear search :- Binary Search :-

Enter number of elements: 5 Enter number of elements: 4


Enter elements: Enter elements:
9 5
4 4
3 9
7 1
6 Enter element to search: 4
Enter element to search: 7 Choose search method:
Choose search method: 1. Linear Search
1. Linear Search 2. Binary Search
2. Binary Search 2
1 Element found at position 2
Element found at position 4
--------------------------------
-------------------------------- Process exited after 15.51 seconds with return
Process exited after 23.07 seconds with return value 0
value 0 Press any key to continue . . .
Press any key to continue .

DATA STRUCTURE

You might also like