Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Cbinsearch
//gcc 7.4.0 //write a function to search for a particular item from a sorted array of integers using the binary search method; and show how this function may be used in a program(use pointer) //code is created by Rezaul Hoque on May 31,2021 //please contact at jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; //binary search works like this: it compares the searched key with the middle element of the sorted array; if the searched key is smaller than the middle item than first half of the array is searched otherwise the second half is searched; in all circumstances only half of the sorted items is taken into account; #include <stdio.h> int binSearch(int*,int,int,int);//binSearch function takes one integer pointer and three integers as parameters //the name of the array itself is a constant pointer to the first element of the array; so it is better to make good use of the array name, the constant pointer int binSearch(int *a, int low, int high, int key) { if (high >= low) {//as long as high is greater or equal to low do the following int mid = low + (high - low) / 2; //calculate the middle item if (*(a+mid) == key) return mid; // *(a+mid) is value stored in the middle item of the array; here if the searched key is equal to that value then return the middle item //if the searched key is smaller than the middle item then do binary search in the first half of the array; note in first half the high is set to mid-1, the ending point of the search if (*(a+mid) > key) return binSearch(a, low, mid - 1, key); //if the searched key is higher than the middle item then do binary search in the second half of the array; note in second half the low is set to mid+1, the starting point of the search return binSearch(a, mid + 1, high, key); } return -1; //the searched key is not found } int main() { int a[] = {15, 21, 33, 44, 50, 60}; int key = 0; //get the key from user printf("Enter Search Item: "); scanf("%d", &key); int size = sizeof(a)/sizeof(a[0]);//size of arrays is calculated with the aid of sizeof operator; sizeof(a) yields the number of bytes needed to store 5 integers of array a; 10 in this case; and sizeof(a[0]) gives the number of bytes needed to store first element of the array, which is 2 in this case; so, 10/2 yields 5; // int found = binSearch(a, 0, size, key);//found is assigned the return values of binSearch() if(found == -1)//if the searched key is not found printf("%d The item is not found\n", key); else printf("\n%d The item is found at %d\n", key, found); return 0; }
run
|
edit
|
history
|
help
0
Lab 9 v0.8
extra causes class UK assisment
HW22
Spring 2017 Lab 4 v1.1
Adding sums of two 1-D arrays
Stub Program for Problem 4 HW 2
assignment 3 celina
Sunday assignment late v3
open(): No such file or directory
la wea afortunada