create account

To search an element in a given array of elements using linear search and recursive binary search. by jazzbel1

View this thread on: hive.blogpeakd.comecency.com
· @jazzbel1 ·
$1.29
To search an element in a given array of elements using linear search and recursive binary search.
![image.png](https://images.hive.blog/DQmbhqqkakDC61LbhxXNRBE7B1Zs1YAb8V9m925sMYktmfr/image.png)



Write program for searching an element in a given array of elements {45, 23, 89, 20, 67, 22, 19, 10, 60, 24, 90, 76, 52, 4, 98, 56}. Search an element using linear search and recursive binary search.


AIM: To write a program for searching an element in a given array of elements using linear search and recursive binary search.


Algorithm:
 
Linear Search (key, array, num of elements (n)):  
Step 1: set i to 1 and loop till n 
Step 2: if key is equal to array[i] set c equal to i 
Step 3: print element found at c + 1 
Step 4: if not equal go to step 2 and set i = i + 1 
Step 5: if i > n print element not found 
Step 6: Exit 


Binary Search (array, initial, end, key): 
Step 1: Find the middle element of array using, middle = initial + end / 2 
Step 2: if initial is greater than end value returns -1 
Step 3: If middle = key, return index 
Step 4: if middle > element, call the function with end_value = middle - 1 
Step 5: if middle < element, call the function with start_value = middle + 1 
Step 6: Exit

Code:

#include <stdio.h>
#include <stdlib.h>
void linearSearch (int key, int array[100], int n)
{
 int i, v;
 for (i=1; i<=n; i++)
 {
 if (key == array[i])
 {
 v = i;
 printf ("\n Element found at %d\n", v+1);
 }
 }
}
int BinarySearch(int array[], int low, int high, int key)
{
 if (low > high)
 {
 return -1;
 }
 int mid = (low + high) / 2;
 if (array[mid] == key)
 {
 return mid;
 }
 else if (array[mid] > key)
 {
 BinarySearch(array, low, mid - 1, key);
 }
 else if (array[mid] < key)
 {
 BinarySearch(array, mid + 1, high, key);
 }
}
void bubble_sort(int list[], int size)
{
 int temp, i, j;
 for (i = 0; i < size; i++)
 {
 for (j = i; j < size; j++)
 {
 if (list[i] > list[j])
 {
 temp = list[i];
 list[i] = list[j];
 list[j] = temp;
 }
 }
 }
}
int main()
{
 int key, n,choice;
 int array[]= {45,23,89,20,67,22,19,10,60,24,90,76,52,4,98,56};
 n = sizeof(array) / sizeof(array[0]);
 printf("The elements of the array are: - ");
 for(int i = 0; i < n; i++){
 printf("%d ", array[i]);
}
printf("\n Enter the search key: - ");
scanf("%d", &key);
printf("\n Enter your choice: - \n1.Linear Search\n2.Recursive Binary Search\n");
scanf("%d", &choice);
if(choice == 1)
{
 linearSearch(key, array, n);
}
else if(choice == 2)
{
 bubble_sort(array, n);
 int index = BinarySearch(array,0, n-1, key);
 if(index == -1 )
 {
 printf("Element not found ");
 }
 else
 {
 printf("Element found");
 }
}
else
{
 exit(0);
}
return 0;
}




Output:


![image.png](https://images.hive.blog/DQmVgUWy1auWzXTxYJQbEkEozHz4mZpJjTRFtrQDf2R35Wj/image.png)

Posted with [STEMGeeks](https://stemgeeks.net)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorjazzbel1
permlinkto-search-an-element-in-a-given-array-of-elements-using-linear-search-and-recursive-binary-search
categoryhive-163521
json_metadata{"tags":["stemgeeks","science","technology","engineering","programming","codeing","software","computers","computerscience","stem"],"image":["https://images.hive.blog/DQmbhqqkakDC61LbhxXNRBE7B1Zs1YAb8V9m925sMYktmfr/image.png","https://images.hive.blog/DQmVgUWy1auWzXTxYJQbEkEozHz4mZpJjTRFtrQDf2R35Wj/image.png"],"app":"stemgeeks/0.1","format":"markdown","canonical_url":"https://stemgeeks.net/@jazzbel1/to-search-an-element-in-a-given-array-of-elements-using-linear-search-and-recursive-binary-search"}
created2021-10-22 17:04:15
last_update2021-10-22 17:04:15
depth0
children1
last_payout2021-10-29 17:04:15
cashout_time1969-12-31 23:59:59
total_payout_value0.654 HBD
curator_payout_value0.640 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,696
author_reputation61,712,812,266
root_title"To search an element in a given array of elements using linear search and recursive binary search."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id107,224,756
net_rshares1,208,552,216,175
author_curate_reward""
vote details (45)
@hivebuzz ·
Congratulations @jazzbel1! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@jazzbel1/upvoted.png?202110221947"></td><td>You received more than 300 upvotes.<br>Your next target is to reach 400 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@jazzbel1) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>

properties (22)
authorhivebuzz
permlinkhivebuzz-notify-jazzbel1-20211022t201325
categoryhive-163521
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-10-22 20:13:24
last_update2021-10-22 20:13:24
depth1
children0
last_payout2021-10-29 20:13:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length627
author_reputation369,969,650,839,019
root_title"To search an element in a given array of elements using linear search and recursive binary search."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id107,227,854
net_rshares0