Binary search is said to be the oldest way to make a search. However, the difficulty of achieving it has been acknowledged by the whole IT fields. Knuth put the algorithm forward as early 1946 in the book "
Sorting and Searching" but not until 1962 did the first program without any bugs come to the world. About only 10% programmers are able to write it without any mistake.
#include
<
stdio.h
>
#define
NUMMAX 7
void
main()
{
float num[NUMMAX];
double temp = 0;
printf("Plz input %d numbers, each of which ends with enter:\n", NUMMAX);
/**//* Input the numbers */
for(int i = 0; i < NUMMAX; i++)
{
scanf("%f", num + i); //The 2nd parameter of scanf is actually the address of variable you want to store
}
printf("%s\n", "***The initial arrays***");
for(int i = 0; i < NUMMAX; i++)
{
printf("%g ", num[i]);
}
printf("\n");
/**//* Sort the arrays by bubble */
for(int j = 0; j < NUMMAX; j++)
{
for(int i = NUMMAX - 1; i > 0; i--)
{
if(num[i] < num[i - 1])
{
temp = num[i];
num[i] = num[i - 1];
num[i - 1] = temp;
}
}
}
printf("%s\n", "***The bubble-sorted arrays***");
for(int i = 0; i < NUMMAX; i++)
{
printf("%g ", num[i]);
}
printf("\n");
float target = 0;
printf("%s\n", "Plz input the number you'd like to find: ");
scanf("%f", &target);
/**//* The bubble sort process */
int low = 0, high = NUMMAX - 1;
int i = 0;
do{
i = (low + high) / 2;
if(num[i] < target)
{
low = i + 1;
}
if(num[i] > target)
{
high = i - 1;
}
if(num[i] == target)
{
break;
}
}while(i != (low + high) / 2);
if(num[i] != target)
printf("%s\n", "Not exit");
else
printf("Got it, the position is %d\n", i);
}
Since its the first time I've ever use the C language, I tried to look up a lot of books and articles to make the grammar correct.
All the referances is paced embedded the codes and the notes are as follows:
1. For the function scanf, the 2nd parameter of scanf is actually the address of variable you want to store.
2. Scanf and printf almost have the same parameter, Where specifier is the most significant one and defines the type and the interpretation of the value of the coresponding argument.
3. Pay close attention to the process of Binary search. Many mistakes may occur in the tens lines of codes.
1
/**/
/* The bubble sort process */
2
int
low
=
0
, high
=
NUMMAX
-
1
;
3
int
i
=
0
;
4
5
do
{
6 i = (low + high) / 2;
7 if(num[i] < target)
8 {
9 low = i + 1;
10 }
11 if(num[i] > target)
12 {
13 high = i - 1;
14 }
15 if(num[i] == target)
16 {
17 break;
18 }
19 }
while
(i
!=
(low
+
high)
/
2
);
20
21
if
(num[i]
!=
target)
22
printf(
"
%s\n
"
,
"
Not exit
"
);
23
else
24
printf(
"
Got it, the position is %d\n
"
, i);
3.1 In each time of loop, low is iterated by i + 1, not i, or the search won't get the last element of your array. The same to high.
3.2 Simple as it may seem, let me analyse the whole structure in detail. It may have something to do with the knowledge of Discrete Mathmatics.
The simple block indicates a general principle for iterative process, initialization, saving and terminal.
Initialization: Iterative process begins after the invariant (or assertion) is initialized to be true, often by give a initial value to the argument or so.
Saving: In the iterative process, we must make sure the inviariant always be correct. So it can be proved by Mathematical Induction that both before and after the iteration, the assertion is always correct.
Terminal: Wherever the loop ends, the invariant is always correct.
All the 3 steps above prove the truth that the loop can be properly ended with the correction of invariant. To do this, let us take a look at the validation of any function.
To validate whether a funtion or assertion is correct or not, the most popular way is Syllogism.