Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Frank Tetzel <s1445051@mail.zih.tu-dresden.de> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Simple program to implement binary search |
| Date | 2015-11-12 17:45 +0100 |
| Message-ID | <20151112174530.49d8a2ea@archthink> (permalink) |
| References | <af16475b-9f9c-46f8-98c5-9353f82423da@googlegroups.com> |
> // Program to perform binary search
>
> #include <stdio.h>
> #include <stdbool.h>
>
> bool binary_search(int search_value, int numbers[], int min_val, int
> max_val);
>
> bool search(int value, int values[], int n);
>
> int main(void)
> {
> int array[] = {1, 2, 3, 4, 5};
> int number;
>
> printf("Name your number: \n");
> scanf("%i", &number);
>
> if (search(number, array, 5))
> {
> printf("\nFound your number!\n\n");
> return 0;
> }
> else
> {
> printf("\nDidn't find your number.\n\n");
> return 1;
> }
> }
>
> // Function to perform binary search
> bool binary_search(int search_value, int numbers[], int min_val, int
> max_val) {
>
> if (max_val < min_val)
> return false;
>
> int mid_index = (max_val + min_val) / 2;
>
> if (search_value > numbers[mid_index])
> binary_search(search_value, numbers, mid_index+1, max_val);
> else if (search_value < numbers[mid_index])
> binary_search(search_value, numbers, min_val, mid_index-1);
> else
> return true;
>
> return false;
> /** I had to add the last return false; part because
> without it the compiler generated a warning:
> warning: control reaches end of non-void function **/
Think about it. The compiler has a reason to say that. Try to follow
all possible paths in this function. Which of them does not have a
return statement? And is this correct? This return false at the end is
wrong.
Keep in mind that this is a recursive function. Think about the
recursion stack and what one return statement does.
> }
>
> // Function that searches for required value using binary_search
> function bool search(int value, int values[], int n)
> {
> int min = 0;
> int max = n-1;
>
> if (binary_search(value, values, min, max))
> return true;
>
> else
> return false;
> }
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Simple program to implement binary search Alla _ <modelling.data@gmail.com> - 2015-11-12 08:12 -0800
Re: Simple program to implement binary search Frank Tetzel <s1445051@mail.zih.tu-dresden.de> - 2015-11-12 17:45 +0100
Re: Simple program to implement binary search John Gordon <gordon@panix.com> - 2015-11-12 16:49 +0000
Re: Simple program to implement binary search Alla _ <modelling.data@gmail.com> - 2015-11-12 10:11 -0800
Re: Simple program to implement binary search Richard Heathfield <rjh@cpax.org.uk> - 2015-11-12 18:13 +0000
Re: Simple program to implement binary search Alla _ <modelling.data@gmail.com> - 2015-11-12 10:24 -0800
Re: Simple program to implement binary search Richard Heathfield <rjh@cpax.org.uk> - 2015-11-12 18:41 +0000
Re: Simple program to implement binary search Chad <cdalten@gmail.com> - 2015-11-12 11:56 -0800
Re: Simple program to implement binary search Richard Heathfield <rjh@cpax.org.uk> - 2015-11-12 16:53 +0000
Re: Simple program to implement binary search Alla _ <modelling.data@gmail.com> - 2015-11-12 10:23 -0800
Re: Simple program to implement binary search Richard Heathfield <rjh@cpax.org.uk> - 2015-11-12 18:35 +0000
Re: Simple program to implement binary search Alla _ <modelling.data@gmail.com> - 2015-11-12 10:50 -0800
Re: Simple program to implement binary search supercat@casperkitty.com - 2015-11-12 11:17 -0800
Re: Simple program to implement binary search Barry Schwarz <schwarzb@dqel.com> - 2015-11-12 11:20 -0800
Re: Simple program to implement binary search Alla _ <modelling.data@gmail.com> - 2015-11-12 23:20 -0800
Re: Simple program to implement binary search Richard Heathfield <rjh@cpax.org.uk> - 2015-11-13 07:23 +0000
Re: Simple program to implement binary search Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-12 10:20 -0800
Re: Simple program to implement binary search Alla _ <modelling.data@gmail.com> - 2015-11-12 10:57 -0800
Re: Simple program to implement binary search Richard Heathfield <rjh@cpax.org.uk> - 2015-11-12 19:01 +0000
Re: Simple program to implement binary search Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-14 11:19 -0800
Re: Simple program to implement binary search Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-14 12:52 -0800
Re: Simple program to implement binary search Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-15 12:48 -0800
Re: Simple program to implement binary search raltbos@xs4all.nl (Richard Bos) - 2015-11-17 22:54 +0000
Re: Simple program to implement binary search Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 13:47 -0800
Re: Simple program to implement binary search "Charles Richmond" <numerist@aquaporin4.com> - 2015-11-21 18:26 -0600
Re: Simple program to implement binary search Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-22 08:19 -0500
Re: Simple program to implement binary search mark.bluemel@gmail.com - 2015-11-26 01:08 -0800
Re: Simple program to implement binary search Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 13:42 -0800
Re: Simple program to implement binary search "Charles Richmond" <numerist@aquaporin4.com> - 2015-11-23 15:47 -0600
Re: Simple program to implement binary search Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-24 08:44 -0800
Re: Simple program to implement binary search Les Cargill <lcargill99@comcast.com> - 2015-11-16 22:52 -0600
Re: Simple program to implement binary search Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-17 01:02 -0800
Re: Simple program to implement binary search Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 13:27 -0800
Re: Simple program to implement binary search Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-13 21:41 -0800
csiph-web