Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. find lower bound

find lower bound

Scheduled Pinned Locked Moved Algorithms
algorithms
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    liquid_
    wrote on last edited by
    #1

    Below, there is an algorithm which I wrote to find lower bound of the given key in a sorted table. In other words, it finds a key which is <= of a given one. I wonder whether there is a more compact algorithm or maybe more efficient.

    #include <stddef.h>

    /*
    * Perform a binary lower bound search.
    *
    */

    static size_t blsize;
    static const void *skey;
    static int (*cmpf)(const void *e1, const void *e2);

    static void *lbnd(const void *base, size_t size)
    {
    const char *p,*p1;
    int c;
    size_t n;

    if(size==0) return NULL;
    if(size==1)
    {
    	c = (\*cmpf)(skey,base);
    	if(c<0)
    		return NULL;
    	else
    		return (void \*)base;
    }
    n = size >> 1;
    p = (char \*)base + n\*blsize;
    c = (\*cmpf)(skey,p);
    if(c==0) return (void \*)p;
    if(c<0)
    {
    	return lbnd(base,n);
    }
    else
    {
    	p1 = lbnd((char\*)p + blsize,size - n - 1);
    	if(p1==NULL)
    		return (void \*)p;
    	else
    		return (void \*)p1;
    }
    

    }

    void *lbound(const void *key, const void *base0, size_t nmemb, size_t size, int (*compar)(const void *e1, const void *e2))
    {
    blsize = size;
    cmpf = compar;
    skey = key;
    return lbnd(base0,nmemb);
    }

    The static function lbnddoes the search using static variables which contain constant values because lbnd is called recursively and to minimize code overhead in parameters passing.

    N L A 3 Replies Last reply
    0
    • L liquid_

      Below, there is an algorithm which I wrote to find lower bound of the given key in a sorted table. In other words, it finds a key which is <= of a given one. I wonder whether there is a more compact algorithm or maybe more efficient.

      #include <stddef.h>

      /*
      * Perform a binary lower bound search.
      *
      */

      static size_t blsize;
      static const void *skey;
      static int (*cmpf)(const void *e1, const void *e2);

      static void *lbnd(const void *base, size_t size)
      {
      const char *p,*p1;
      int c;
      size_t n;

      if(size==0) return NULL;
      if(size==1)
      {
      	c = (\*cmpf)(skey,base);
      	if(c<0)
      		return NULL;
      	else
      		return (void \*)base;
      }
      n = size >> 1;
      p = (char \*)base + n\*blsize;
      c = (\*cmpf)(skey,p);
      if(c==0) return (void \*)p;
      if(c<0)
      {
      	return lbnd(base,n);
      }
      else
      {
      	p1 = lbnd((char\*)p + blsize,size - n - 1);
      	if(p1==NULL)
      		return (void \*)p;
      	else
      		return (void \*)p1;
      }
      

      }

      void *lbound(const void *key, const void *base0, size_t nmemb, size_t size, int (*compar)(const void *e1, const void *e2))
      {
      blsize = size;
      cmpf = compar;
      skey = key;
      return lbnd(base0,nmemb);
      }

      The static function lbnddoes the search using static variables which contain constant values because lbnd is called recursively and to minimize code overhead in parameters passing.

      N Offline
      N Offline
      NeverHeardOfMe
      wrote on last edited by
      #2

      “I think the bubble sort would be the wrong way to go.” :)

      L 1 Reply Last reply
      0
      • N NeverHeardOfMe

        “I think the bubble sort would be the wrong way to go.” :)

        L Offline
        L Offline
        liquid_
        wrote on last edited by
        #3

        Maybe yes maybe no. The question is not about sorting but about searching the sorted table, though.

        N 1 Reply Last reply
        0
        • L liquid_

          Below, there is an algorithm which I wrote to find lower bound of the given key in a sorted table. In other words, it finds a key which is <= of a given one. I wonder whether there is a more compact algorithm or maybe more efficient.

          #include <stddef.h>

          /*
          * Perform a binary lower bound search.
          *
          */

          static size_t blsize;
          static const void *skey;
          static int (*cmpf)(const void *e1, const void *e2);

          static void *lbnd(const void *base, size_t size)
          {
          const char *p,*p1;
          int c;
          size_t n;

          if(size==0) return NULL;
          if(size==1)
          {
          	c = (\*cmpf)(skey,base);
          	if(c<0)
          		return NULL;
          	else
          		return (void \*)base;
          }
          n = size >> 1;
          p = (char \*)base + n\*blsize;
          c = (\*cmpf)(skey,p);
          if(c==0) return (void \*)p;
          if(c<0)
          {
          	return lbnd(base,n);
          }
          else
          {
          	p1 = lbnd((char\*)p + blsize,size - n - 1);
          	if(p1==NULL)
          		return (void \*)p;
          	else
          		return (void \*)p1;
          }
          

          }

          void *lbound(const void *key, const void *base0, size_t nmemb, size_t size, int (*compar)(const void *e1, const void *e2))
          {
          blsize = size;
          cmpf = compar;
          skey = key;
          return lbnd(base0,nmemb);
          }

          The static function lbnddoes the search using static variables which contain constant values because lbnd is called recursively and to minimize code overhead in parameters passing.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I don't think I understand the your requirements correctly, because to me it looks like any key <= x will be fine, and since the table is sorted, you could just take the first one and return an error if it is > x

          1 Reply Last reply
          0
          • L liquid_

            Maybe yes maybe no. The question is not about sorting but about searching the sorted table, though.

            N Offline
            N Offline
            NeverHeardOfMe
            wrote on last edited by
            #5

            Oh good grief... did none of you CP geeks get the reference[^] ? :sigh:

            1 Reply Last reply
            0
            • L liquid_

              Below, there is an algorithm which I wrote to find lower bound of the given key in a sorted table. In other words, it finds a key which is <= of a given one. I wonder whether there is a more compact algorithm or maybe more efficient.

              #include <stddef.h>

              /*
              * Perform a binary lower bound search.
              *
              */

              static size_t blsize;
              static const void *skey;
              static int (*cmpf)(const void *e1, const void *e2);

              static void *lbnd(const void *base, size_t size)
              {
              const char *p,*p1;
              int c;
              size_t n;

              if(size==0) return NULL;
              if(size==1)
              {
              	c = (\*cmpf)(skey,base);
              	if(c<0)
              		return NULL;
              	else
              		return (void \*)base;
              }
              n = size >> 1;
              p = (char \*)base + n\*blsize;
              c = (\*cmpf)(skey,p);
              if(c==0) return (void \*)p;
              if(c<0)
              {
              	return lbnd(base,n);
              }
              else
              {
              	p1 = lbnd((char\*)p + blsize,size - n - 1);
              	if(p1==NULL)
              		return (void \*)p;
              	else
              		return (void \*)p1;
              }
              

              }

              void *lbound(const void *key, const void *base0, size_t nmemb, size_t size, int (*compar)(const void *e1, const void *e2))
              {
              blsize = size;
              cmpf = compar;
              skey = key;
              return lbnd(base0,nmemb);
              }

              The static function lbnddoes the search using static variables which contain constant values because lbnd is called recursively and to minimize code overhead in parameters passing.

              A Offline
              A Offline
              Alain Rist
              wrote on last edited by
              #6

              Hi,

              liquid_ wrote:

              I wonder whether there is a more compact algorithm or maybe more efficient

              As you use size_t you are probably using a C++ compiler here. Then if Key is any type with operator<(const Key& other) defined:

              #include <algorithm>
              std::min_element(/* const Key* */ base, base + /*size_t*/ nElts /* = number of elements*/);

              returns a const Key* pointing to the smallest key in the array. cheers, AR

              When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

              L 1 Reply Last reply
              0
              • A Alain Rist

                Hi,

                liquid_ wrote:

                I wonder whether there is a more compact algorithm or maybe more efficient

                As you use size_t you are probably using a C++ compiler here. Then if Key is any type with operator<(const Key& other) defined:

                #include <algorithm>
                std::min_element(/* const Key* */ base, base + /*size_t*/ nElts /* = number of elements*/);

                returns a const Key* pointing to the smallest key in the array. cheers, AR

                When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                L Offline
                L Offline
                liquid_
                wrote on last edited by
                #7

                Thanks for constructive reply. Firstly, I used size_t only for shortage of unsigned int. The rest of code is written in pure C and it should be just such - a more universal one. Secondly, analyzing the std::min_element function it is clear that it has linear complexity. What I tried to aim is logarithmic complexity. However, comparing my implementation of

                lbound(const void* key, void *base, unsigned int nmemb, unsigned int size, int (*compare)(const void *e1, const void *e2))

                with bsearch implementation, it seemed to me that my lbound is not so compact and efficient as bsearch standard implementation (two additional checks - size==0 and size==1, and one more comparison after size==1 check). Maybe its the matter of lower bound specific. bsearch just returns NULL if it does not find a key, lbound returns NULL only if key is lower than first element of table. Regards

                A 1 Reply Last reply
                0
                • L liquid_

                  Thanks for constructive reply. Firstly, I used size_t only for shortage of unsigned int. The rest of code is written in pure C and it should be just such - a more universal one. Secondly, analyzing the std::min_element function it is clear that it has linear complexity. What I tried to aim is logarithmic complexity. However, comparing my implementation of

                  lbound(const void* key, void *base, unsigned int nmemb, unsigned int size, int (*compare)(const void *e1, const void *e2))

                  with bsearch implementation, it seemed to me that my lbound is not so compact and efficient as bsearch standard implementation (two additional checks - size==0 and size==1, and one more comparison after size==1 check). Maybe its the matter of lower bound specific. bsearch just returns NULL if it does not find a key, lbound returns NULL only if key is lower than first element of table. Regards

                  A Offline
                  A Offline
                  Alain Rist
                  wrote on last edited by
                  #8

                  Hi,

                  liquid_ wrote:

                  code is written in pure C and it should be just such - a more universal one.

                  This is really planet-dependant :)

                  liquid_ wrote:

                  What I tried to aim is logarithmic complexity.

                  If your data are unsorted you will have to check each and any for smaller than current minimum, so you will never get under linear complexity. If your data are sorted the problem is trivial. More generally you should think that more man*hours have been spent on each part of the Standard C++ Library than you can provide in your total professional life. Good luck, AR

                  When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                  L 1 Reply Last reply
                  0
                  • A Alain Rist

                    Hi,

                    liquid_ wrote:

                    code is written in pure C and it should be just such - a more universal one.

                    This is really planet-dependant :)

                    liquid_ wrote:

                    What I tried to aim is logarithmic complexity.

                    If your data are unsorted you will have to check each and any for smaller than current minimum, so you will never get under linear complexity. If your data are sorted the problem is trivial. More generally you should think that more man*hours have been spent on each part of the Standard C++ Library than you can provide in your total professional life. Good luck, AR

                    When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                    L Offline
                    L Offline
                    liquid_
                    wrote on last edited by
                    #9

                    The assumption is that data are sorted. I'd just like to know if I found this trivial solution. Concerning the philosophy of using ready made STL or creating own solutions, it depends on particular needs. Many programs are written in C not in C++ because of various reasons (eg. UNIX or LINUX areas). I use pure C or C++ with STL or other available libraries (eg. BOOST, MFC, .NET aso) as well. Most of software I do is made just for fun or to search/find interesting solutions, or to resolve some problems I meet in my job. It is not my core business. Anyway, thanks for your points. Regards

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups