Ice Sheet System Model  4.18
Code documentation
Functions
sorting.h File Reference

prototypes for sorting.h More...

Go to the source code of this file.

Functions

int binary_search (int *poffset, int target, int *sorted_integers, int num_integers)
 
template<typename doubletype >
int binary_search (int *poffset, doubletype target, doubletype *list, int length)
 

Detailed Description

prototypes for sorting.h

Definition in file sorting.h.

Function Documentation

◆ binary_search() [1/2]

int binary_search ( int *  poffset,
int  target,
int *  sorted_integers,
int  num_integers 
)

Definition at line 14 of file binary_search.cpp.

14  { /*{{{*/
15 
16  /*output: */
17  int offset=0; //offset, if found
18  int found=0; //found=0 if target is not found, 1 otherwise.
19 
20  /*intermediary: */
21  int *beg = NULL;
22  int *end = NULL;
23  int *mid = NULL;
24 
25  _assert_(sorted_integers);
26 
27  // point to beginning and end of the array
28  beg=sorted_integers;
29  end=sorted_integers+num_integers;
30  mid=beg+(int)(num_integers/2);
31 
32  if (*beg==target){
33  found=1;
34  offset=0;
35  }
36  else if(*(end-1)==target){
37  found=1;
38  offset=num_integers-1;
39  }
40  else{
41  while((beg <= end) && (*mid != target)){
42  // is the target in lower or upper half?
43  if (target < *mid) {
44  end = mid - 1; //new end
45  mid = beg + (end-beg)/2; //new middle
46  }
47  else {
48  beg = mid + 1; //new beginning
49  mid = beg + (end-beg)/2; //new middle
50  }
51  }
52 
53  //did we find the target?
54  if (*mid == target) {
55  found=1;
56  offset=mid-sorted_integers;
57  }
58  else {
59  found=0;
60  }
61  }
62 
63  /*Assign output pointers:*/
64  *poffset=offset;
65 
66  /*Return result: */
67  return found;
68 } /*}}}*/

◆ binary_search() [2/2]

template<typename doubletype >
int binary_search ( int *  poffset,
doubletype  target,
doubletype *  list,
int  length 
)

Definition at line 9 of file sorting.h.

9  { /*{{{*/
10  /*
11  * l[0] l[1] l[2] l[n] l[n+1] l[length-1]
12  * <-------+-----+-----+-----+ ... +-----+........+-------------->
13  * offset: -1 0 1 2 n length -1
14  *
15  * offset = -1 target < list[0]
16  * offset = n list[n] <= target < list[n+1]
17  * offset = length-1 list[length-1] <= target
18  */
19 
20  /*output: */
21  int offset = 0;
22  int found = 0;
23 
24  /*intermediary: */
25  int n0 = 0;
26  int n1 = int(length/2);
27  int n2 = length-1;
28 
29  if(target<list[n0]){
30  found = 1;
31  offset = -1;
32  }
33  else if(target>=list[n2]){
34  found = 1;
35  offset = length-1;
36  }
37  else{
38  for(;;){
39  /*did we find the target?*/
40  if(list[n1]<=target && list[n1+1]>target){
41  found = 1;
42  offset = n1;
43  break;
44  }
45  else if(target < list[n1]){
46  n2 = n1;
47  n1 = n0 + int((n2-n0)/2);
48  }
49  else{
50  n0 = n1;
51  n1 = n0 + int((n2-n0)/2);
52  }
53  }
54  }
55 
56  /*Assign output pointer and return*/
57  *poffset=offset;
58  return found;
59 } /*}}}*/
_assert_
#define _assert_(ignore)
Definition: exceptions.h:37