Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours
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
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
algorithmique
cours
Commits
ffa1b6ee
Verified
Commit
ffa1b6ee
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added code for find complexity
parent
bd350f28
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
source_codes/complexity/.gitignore
+1
-5
1 addition, 5 deletions
source_codes/complexity/.gitignore
source_codes/complexity/search.c
+62
-0
62 additions, 0 deletions
source_codes/complexity/search.c
with
63 additions
and
5 deletions
source_codes/complexity/.gitignore
+
1
−
5
View file @
ffa1b6ee
search
sum
sum_one
sum_one_opt
sum_thousand
sum_thousand_opt
This diff is collapsed.
Click to expand it.
source_codes/complexity/search.c
0 → 100644
+
62
−
0
View file @
ffa1b6ee
#include
<stdbool.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
#define SIZE 1000
void
init
(
int
n
,
int
tab
[])
{
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
tab
[
i
]
=
3
*
i
;
printf
(
"%d "
,
tab
[
i
]);
}
printf
(
"
\n
"
);
}
bool
is_present_linear
(
int
n
,
int
tab
[],
int
elem
,
int
*
steps
)
{
*
steps
=
-
1
;
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
*
steps
=
i
;
if
(
tab
[
i
]
==
elem
)
{
return
true
;
}
else
if
(
elem
<
tab
[
i
])
{
return
false
;
}
}
return
false
;
}
bool
is_present_binary_search
(
int
n
,
int
tab
[],
int
elem
,
int
*
steps
)
{
*
steps
=
0
;
int
left
=
0
;
int
right
=
n
-
1
;
while
(
left
<=
right
)
{
*
steps
+=
1
;
int
mid
=
(
right
+
left
)
/
2
;
if
(
tab
[
mid
]
<
elem
)
{
left
=
mid
+
1
;
}
else
if
(
tab
[
mid
]
>
elem
)
{
right
=
mid
-
1
;
}
else
{
return
true
;
}
}
return
false
;
}
int
main
()
{
srand
(
time
(
NULL
));
int
tab
[
SIZE
];
init
(
SIZE
,
tab
);
// we should init randomly and sort but... I'm lazy
int
elem
=
rand
()
%
(
4
*
SIZE
);
int
steps
=
0
;
bool
is_present
=
is_present_linear
(
SIZE
,
tab
,
elem
,
&
steps
);
printf
(
"Condition %d is present is %s in %d steps
\n
"
,
elem
,
is_present
?
"true"
:
"false"
,
steps
);
bool
is_present_b
=
is_present_binary_search
(
SIZE
,
tab
,
elem
,
&
steps
);
printf
(
"Condition %d is present is %s in %d steps
\n
"
,
elem
,
is_present_b
?
"true"
:
"false"
,
steps
);
return
EXIT_SUCCESS
;
}
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