Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
glkink
/
C-programming
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
60e9c083
authored
3 years ago
by
glkink
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
a711c2bf
master
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
0 deletions
binaarneotsing
binaarneotsing
0 → 100644
View file @
60e9c083
#include <stdio.h>
int kahend(int massiiv[], int esimene, int viimane, int otsi)
{
int m;
if (esimene > viimane)
return -1;
m = (esimene + viimane)/2;
if (massiiv[m] == otsi)
return m;
else if (otsi > massiiv[m])
return kahend(massiiv, m+1, viimane, otsi);
else
return kahend(massiiv, esimene, m-1, otsi);
}
int main()
{
int esimeneElement, viimaneElement, elemendid, otsiElement, massiivOG[100], loend, n;
printf("Sisesta elementide arv:\n");
scanf("%d", &n);
printf("Sisesta %d taisarvu:\n", n);
for (elemendid = 0; elemendid < n; elemendid++)
scanf("%d", &massiivOG[elemendid]);
printf("Sisesta vaartus mida otsid:\n");
scanf("%d", &otsiElement);
esimeneElement = 0;
viimaneElement = n - 1;
loend = kahend(massiivOG, esimeneElement, viimaneElement, otsiElement);
if (loend == -1)
printf("%d pole massiivis!\n", otsiElement);
else
printf("Arv %d on massiivis kohas %d.\n", otsiElement, loend + 1);
return 0;
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment