Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
c-stuff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruce Cowan
c-stuff
Commits
a8686334
Commit
a8686334
authored
5 years ago
by
Bruce Cowan
Browse files
Options
Downloads
Patches
Plain Diff
Add a binary search which uses the standard library
parent
1a088a4b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/binary2.c
+42
-0
42 additions, 0 deletions
src/binary2.c
src/meson.build
+1
-0
1 addition, 0 deletions
src/meson.build
with
43 additions
and
0 deletions
src/binary2.c
0 → 100644
+
42
−
0
View file @
a8686334
#include
<stdio.h>
#include
<stdlib.h>
#include
"utils.h"
static
int
list
[]
=
{
8
,
11
,
17
,
22
,
29
,
38
,
51
,
58
,
65
,
67
,
74
,
81
,
87
,
90
,
96
,
97
};
#define N_ELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))
static
int
int_compare
(
const
void
*
a
,
const
void
*
b
)
{
static
int
passes
;
int
first
=
*
((
int
*
)
a
);
int
second
=
*
((
int
*
)
b
);
passes
++
;
printf
(
"
\n
----------Pass #%d----------
\n
"
,
passes
);
printf
(
"Comparing %d and %d
\n
"
,
first
,
second
);
int
ret
=
first
-
second
;
if
(
ret
<
0
)
printf
(
"%d < %d
\n
"
,
first
,
second
);
else
if
(
ret
>
0
)
printf
(
"%d > %d
\n
"
,
first
,
second
);
else
printf
(
"%d == %d
\n
"
,
first
,
second
);
return
ret
;
}
int
main
(
int
argc
,
char
**
argv
)
{
int
*
vals
=
get_ints
(
argc
,
argv
,
1
,
"What is the number to search for?"
);
int
key
=
vals
[
0
];
bsearch
(
&
key
,
list
,
N_ELEMENTS
(
list
),
sizeof
(
int
),
int_compare
);
return
0
;
}
This diff is collapsed.
Click to expand it.
src/meson.build
+
1
−
0
View file @
a8686334
...
...
@@ -2,6 +2,7 @@ executable('add', 'add.c', dependencies: utils_dep)
executable
(
'angle'
,
'angle.c'
,
dependencies
:
libm
)
executable
(
'array-length'
,
'array-length.c'
,
dependencies
:
types_dep
)
executable
(
'binary'
,
'binary.c'
)
executable
(
'binary2'
,
'binary2.c'
,
dependencies
:
utils_dep
)
executable
(
'complex'
,
'complex.c'
,
dependencies
:
libm
)
executable
(
'doubles'
,
'doubles.c'
)
executable
(
'einstein'
,
'einstein.c'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment