 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:  Posted with [STEMGeeks](https://stemgeeks.net)
author | jazzbel1 |
---|---|
permlink | to-search-an-element-in-a-given-array-of-elements-using-linear-search-and-recursive-binary-search |
category | hive-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"} |
created | 2021-10-22 17:04:15 |
last_update | 2021-10-22 17:04:15 |
depth | 0 |
children | 1 |
last_payout | 2021-10-29 17:04:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.654 HBD |
curator_payout_value | 0.640 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,696 |
author_reputation | 61,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_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 107,224,756 |
net_rshares | 1,208,552,216,175 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
slider2990 | 0 | 1,138,073,949 | 15% | ||
trans-juanmi | 0 | 425,330,779 | 20% | ||
freebornsociety | 0 | 847,894,232 | 5% | ||
tipy | 0 | 4,894,179,723 | 100% | ||
baneenabear | 0 | 1,568,989,039 | 100% | ||
upfundme | 0 | 969,405,182 | 3% | ||
antisocialist | 0 | 294,618,045,198 | 50% | ||
tonimontana | 0 | 5,409,557,492 | 100% | ||
enforcer48 | 0 | 132,750,014,286 | 15% | ||
guurry123 | 0 | 597,196,600 | 2% | ||
harpreetjanda | 0 | 551,410,434 | 2% | ||
ctime | 0 | 123,705,545,055 | 1% | ||
limka | 0 | 309,803,795 | 74.29% | ||
memehub | 0 | 79,196,316,856 | 100% | ||
scholaris | 0 | 89,416,255,966 | 100% | ||
aninsidejob | 0 | 5,909,445,741 | 100% | ||
gurseerat | 0 | 717,825,212 | 2% | ||
stemgeeks | 0 | 30,643,324,321 | 30% | ||
stemcuration | 0 | 549,284,360 | 30% | ||
babytarazkp | 0 | 4,222,658,044 | 40% | ||
abh12345.stem | 0 | 3,301,207,635 | 100% | ||
stem.alfa | 0 | 109,368,102 | 100% | ||
yggdrasil.laguna | 0 | 40,917,761 | 15% | ||
chapmain | 0 | 109,863,405 | 100% | ||
stuntman.mike | 0 | 3,337,438,941 | 15% | ||
rmsadkri | 0 | 63,506,584,782 | 100% | ||
discohedge | 0 | 4,545,211,638 | 4% | ||
dorkpower | 0 | 3,037,649,789 | 100% | ||
stemcur | 0 | 969,646,330 | 100% | ||
brofund-stem | 0 | 513,663,851 | 15% | ||
rikarivka | 0 | 5,923,997,275 | 100% | ||
rupinder | 0 | 139,069,061 | 2% | ||
yayogerardo | 0 | 2,417,449,848 | 50% | ||
stemline | 0 | 1,837,268,450 | 15% | ||
meestemboom | 0 | 1,002,228,817 | 50% | ||
academiccuration | 0 | 874,724,596 | 100% | ||
tfranzini | 0 | 4,354,981,758 | 32% | ||
dronegirl | 0 | 315,600,344 | 100% | ||
brofi | 0 | 326,501,479,665 | 4% | ||
juecoree.stem | 0 | 668,134,679 | 100% | ||
memecurator | 0 | 682,905,774 | 100% | ||
memesupport | 0 | 3,509,107,442 | 100% | ||
holovision.stem | 0 | 127,196,523 | 50% | ||
star.stem | 0 | 1,645,292,281 | 50% | ||
solominer.stem | 0 | 640,671,164 | 100% |
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>
author | hivebuzz |
---|---|
permlink | hivebuzz-notify-jazzbel1-20211022t201325 |
category | hive-163521 |
json_metadata | {"image":["http://hivebuzz.me/notify.t6.png"]} |
created | 2021-10-22 20:13:24 |
last_update | 2021-10-22 20:13:24 |
depth | 1 |
children | 0 |
last_payout | 2021-10-29 20:13:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 627 |
author_reputation | 369,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_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 107,227,854 |
net_rshares | 0 |