News for hackers. Stuff that matters.
Re:IBM's Asshole Test by a1369209993 on Thursday May 05, 2022 @12:51AM (#31269614)
> the only way you can write merge sort is if you memorize it (or spend an inordinate amount of time writing sorting algos)

Umm, what?

  void merge(int* d,int const* a,size_t za,int const* b,size_t zb)
    {
    size_t zd = za+zb,ia=0,ib=0;
    for(size_t i=0; i<zd ;i++)
      {
      if(ia < za && ib < zb)
        {
        if(a[ia] < b[ib]) d[i] = a[ia++];
        else              d[i] = b[ib++];
        }
      else if(ia < za)    d[i] = a[ia++];
      else if(ib < zb)    d[i] = b[ib++];
      else abort(); // should have i==zd now
      }
    }
  void msort(int* d,size_t z,int const* s)
    {
    if(z==1) *d = *s;
    if(z<=1) return;
    size_t h = z - (z>>1); // round up
    int* x = malloc(h * sizeof *x);
    if(!x) abort(); // out of memory
    msort(x,h,s+(z-h));
    msort(d+h,z-h,s);
    merge(d,d+h,z-h,x,h);
    }
  // not that this is any *good*, but it does *work*
Are you using a absurdly broad definition of what counts as memorizing something or something?

There are no comments on this comment yet.