[13980] | 1 | Index: ../trunk-jpl/src/c/shared/Sorting/binary_search.cpp
|
---|
| 2 | ===================================================================
|
---|
| 3 | --- ../trunk-jpl/src/c/shared/Sorting/binary_search.cpp (revision 13574)
|
---|
| 4 | +++ ../trunk-jpl/src/c/shared/Sorting/binary_search.cpp (revision 13575)
|
---|
| 5 | @@ -63,62 +63,49 @@
|
---|
| 6 | /*Return result: */
|
---|
| 7 | return found;
|
---|
| 8 | } /*}}}*/
|
---|
| 9 | -int binary_search(int* poffset,double target,double* sorted_doubles,int num_doubles){ /*{{{*/
|
---|
| 10 | +int binary_search(int* poffset,double target,double* list,int num_doubles){ /*{{{*/
|
---|
| 11 |
|
---|
| 12 | /*output: */
|
---|
| 13 | - int offset=0;
|
---|
| 14 | - int found=0; /*found=0: not found.
|
---|
| 15 | - found=1: found, and target is == to value at offset
|
---|
| 16 | - found=2: found, and target is > to value at offset and < to value at offset+1
|
---|
| 17 | - */
|
---|
| 18 | + int offset = 0;
|
---|
| 19 | + int found = 0; /*found = 0: not found.
|
---|
| 20 | + found = -1: found, and target is < first element
|
---|
| 21 | + found = 1: found, and target is == to value at offset
|
---|
| 22 | + found = 2: found, and target is > to value at offset and < to value at offset+1
|
---|
| 23 | + found = 3: found, and target is >= last value */
|
---|
| 24 |
|
---|
| 25 | /*intermediary: */
|
---|
| 26 | - double *beg = NULL;
|
---|
| 27 | - double *end = NULL;
|
---|
| 28 | - double *mid = NULL;
|
---|
| 29 | + int n0 = 0;
|
---|
| 30 | + int n1 = int(num_doubles/2);
|
---|
| 31 | + int n2 = num_doubles-1;
|
---|
| 32 |
|
---|
| 33 | - // point to beginning and end of the array
|
---|
| 34 | - beg = sorted_doubles;
|
---|
| 35 | - end = sorted_doubles+num_doubles;
|
---|
| 36 | - mid = beg+(int)(num_doubles/2.0);
|
---|
| 37 | -
|
---|
| 38 | - if (target<*beg){
|
---|
| 39 | + if(target<list[n0]){
|
---|
| 40 | + found = -1;
|
---|
| 41 | offset = -1;
|
---|
| 42 | - found = 0;
|
---|
| 43 | }
|
---|
| 44 | - if (*beg==target){
|
---|
| 45 | - found = 1;
|
---|
| 46 | - offset = 0;
|
---|
| 47 | + else if(target>=list[n2]){
|
---|
| 48 | + found = 3;
|
---|
| 49 | + offset = n2-1;
|
---|
| 50 | }
|
---|
| 51 | - else if(*(end-1)==target){
|
---|
| 52 | - found = 1;
|
---|
| 53 | - offset = num_doubles-1;
|
---|
| 54 | - }
|
---|
| 55 | else{
|
---|
| 56 | - while((beg <= end) && !( target>=*mid && target<*(mid+1)) ){
|
---|
| 57 | - // is the target in lower or upper half?
|
---|
| 58 | - if (target < *mid) {
|
---|
| 59 | - end = mid - 1; //new end
|
---|
| 60 | - mid = beg + (end-beg)/2; //new middle
|
---|
| 61 | + while(!found){
|
---|
| 62 | + /*did we find the target?*/
|
---|
| 63 | + if(list[n1]<=target && list[n1+1]>target){
|
---|
| 64 | + found = 1;
|
---|
| 65 | + offset = n1;
|
---|
| 66 | + break;
|
---|
| 67 | }
|
---|
| 68 | - else {
|
---|
| 69 | - beg = mid + 1; //new beginning
|
---|
| 70 | - mid = beg + (end-beg)/2; //new middle
|
---|
| 71 | + if(target < list[n1]){
|
---|
| 72 | + n2 = n1;
|
---|
| 73 | + n1 = n0 + int((n2-n0)/2);
|
---|
| 74 | }
|
---|
| 75 | + else{
|
---|
| 76 | + n0 = n1;
|
---|
| 77 | + n1 = n0 + int((n2-n0)/2);
|
---|
| 78 | + }
|
---|
| 79 | }
|
---|
| 80 | -
|
---|
| 81 | - //did we find the target?
|
---|
| 82 | - if( target>*mid && target<*(mid+1)){
|
---|
| 83 | - found=2;
|
---|
| 84 | - offset=mid-sorted_doubles;
|
---|
| 85 | - }
|
---|
| 86 | - else if( target==*mid){
|
---|
| 87 | - found=1;
|
---|
| 88 | - offset=mid-sorted_doubles;
|
---|
| 89 | - }
|
---|
| 90 | - else {
|
---|
| 91 | - found=0;
|
---|
| 92 | - }
|
---|
| 93 | +
|
---|
| 94 | + //did we find an exact target?
|
---|
| 95 | + if(list[n1]==target) found = 2;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /*Assign output pointers:*/
|
---|