Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
C
cpp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edu
cpp
Commits
1cc644d2
Commit
1cc644d2
authored
Nov 09, 2020
by
Hans Buchmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
binary search
cia
parent
a6a36a31
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
137 additions
and
4 deletions
+137
-4
5-pointer/src/binary-search-generic.cc
5-pointer/src/binary-search-generic.cc
+112
-0
5-pointer/src/binary-search.cc
5-pointer/src/binary-search.cc
+9
-0
5-pointer/src/main-args.cc
5-pointer/src/main-args.cc
+2
-2
5-pointer/src/string-demo.cc
5-pointer/src/string-demo.cc
+1
-1
5-pointer/src/string.cc
5-pointer/src/string.cc
+7
-0
ziele-vz.md
ziele-vz.md
+6
-1
No files found.
5-pointer/src/binary-search-generic.cc
0 → 100644
View file @
1cc644d2
//-----------------------
//binary-search
//(c) H.Buchmann FHNW 2011
//-----------------------
#include <iostream>
/*
data ordered i<j -> data[i]<=data[j]
returns index
0<=index<len
if val in data then data[index]==val
*/
/*
data |---|---|---|---|---|---|---|---|---|...
index 0 1 2 3 4 5 6 7 8 |
len 9
before
beg<end
end-beg: number of elements
m
0 1 2 3 | 5 6 7 8 9
data |---|---|---|---|---|---|---|---|---|...
index | |
beg end
m=floor((beg+end)/2)
beg<=m<end
if val<data[m]
m
|
data |---|---|---|---|...|...|...|...|...|...
| |
beg end
if (val>=data[m])
data |...|...|...|...|---|---|---|---|---|...
| |
beg end
*/
/*
1. recursive
2. while loop
*/
template
<
typename
T
>
const
T
*
binary_search
(
T
val
,
const
T
*
beg
,
const
T
*
end
)
{
while
((
end
-
beg
)
>
1
)
{
auto
m
=
beg
+
(
end
-
beg
)
/
2
;
if
(
val
<*
m
)
{
end
=
m
;
}
else
{
beg
=
m
;
}
}
return
beg
;
}
template
<
typename
T
>
void
singleTest
(
T
val
,
const
T
*
beg
,
const
T
*
end
)
{
std
::
cout
<<
"start--------
\n
"
;
auto
res
=
binary_search
(
val
,
beg
,
end
);
std
::
cout
<<
"res="
<<
res
<<
"
\n
"
;
}
template
<
typename
T
>
void
selfTest
(
const
T
*
beg
,
const
T
*
end
)
{
for
(
auto
v
=
beg
;
v
!=
end
;
++
v
)
{
auto
loc
=
binary_search
(
*
v
,
beg
,
end
);
if
(
*
v
!=*
loc
)
{
std
::
cout
<<
"error
\n
"
;
return
;
}
}
std
::
cout
<<
"ok
\n
"
;
}
void
setData
(
int
d
[],
unsigned
len
)
{
int
v
=
0
;
for
(
unsigned
i
=
0
;
i
<
len
;
++
i
)
{
d
[
i
]
=
v
;
v
+=
2
;
}
}
static
const
unsigned
LEN
=
(
1
<<
4
);
int
data
[
LEN
];
int
main
(
int
argc
,
char
*
args
[])
{
//crash if data on stack
setData
(
data
,
LEN
);
// singleTest(100'000'000,data,LEN);
selfTest
(
data
,
data
+
LEN
);
return
0
;
}
5-pointer/src/binary-search.cc
View file @
1cc644d2
...
...
@@ -47,7 +47,16 @@ if (val>=data[m])
*/
/* return:
index: 0<=<index<len
test if val in data
data[index]==val
*/
unsigned
binarySearch
(
int
val
,
int
data
[],
unsigned
len
)
{
/* your work */
}
void
singleTest
(
int
val
,
int
data
[],
unsigned
len
)
...
...
5-pointer/src/main-args.cc
View file @
1cc644d2
...
...
@@ -6,9 +6,9 @@
#include <iostream>
//Java void main(String args[])
//int main(int argc,char*args[]) alternative
//int main(int argc,char*
args[]) alternative
int
main
(
int
argc
,
char
**
args
)
//| |----- array of pointer (char*)
//| |----- array of pointer (char*)
array of strings
//| array of strings
//+------------------- length of array
{
...
...
5-pointer/src/string-demo.cc
View file @
1cc644d2
...
...
@@ -17,7 +17,7 @@ int main(int argc,char* args[])
s
[
7
]
=
'e'
;
s
[
8
]
=
'l'
;
s
[
9
]
=
't'
;
//not a proper end of string no terminating zero
// s[10='\0'; //<--out of range
// s[10
]
='\0'; //<--out of range
// char* sP=s;
std
::
cout
<<
"s= "
<<
s
<<
"
\n
"
;
return
0
;
...
...
5-pointer/src/string.cc
View file @
1cc644d2
...
...
@@ -77,6 +77,13 @@ int String::isHexDigit(char ch)
//implementation
unsigned
String
::
len
(
char
*
s
)
{
if
(
s
==
0
)
return
0
;
unsigned
i
=
0
;
//index in s
while
(
s
[
i
]
!=
'\
0
)
{
i
=
i
+
1
;
//++i i++
}
return
i
;
}
...
...
ziele-vz.md
View file @
1cc644d2
...
...
@@ -75,4 +75,9 @@
-
``string.cc``
exercises.pdf Aufgaben 5.1, 5.2, 5.4, 5.8, 5.9
## 12.11.2020
-
5-pointer
-
Strings
``string.cc``
exercises.pdf Aufgaben 5.2, 5.4, 5.8, 5.9
-
6-cia
-
Die Klasse
``Point``
Folien Seite 7
-
exercises.pdf Aufgaben 1.1, 1.2, 1.3
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