Commit 1cc644d2 authored by Hans Buchmann's avatar Hans Buchmann

binary search

cia
parent a6a36a31
//-----------------------
//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;
}
......@@ -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)
......
......@@ -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
{
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment