mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Horst von Brand <vonbrand@inf.utfsm.cl>
To: thockin@sun.com
Cc: linux-kernel@vger.kernel.org
Subject: Re: [BK PATCH 1/2] Remove NGROUPS hardlimit (resend w/o qsort)
Date: Fri, 15 Nov 2002 13:30:51 -0300	[thread overview]
Message-ID: <200211151630.gAFGUqfk024668@pincoya.inf.utfsm.cl> (raw)
In-Reply-To: Your message of "Thu, 14 Nov 2002 15:26:13 -0800." <200211142326.gAENQE231767@scl2.sfbay.sun.com>

Timothy Hockin <th122948@scl2.sfbay.sun.com> said:

[...]

> +/* a simple shell-metzner sort */
> +static void groupsort(gid_t *grouplist, int gidsetsize)
> +{
> +	int right, left, cur, max, stride;
> +
> +	stride = gidsetsize / 2;

This guarantees bad performance for certain gidgetsizes... customary wisdom
(at least, Knuth sayeth so) is to go:

        for(stride = 1; stride < gidgetsize; stride = 3 * stride + 1)
		;
        do {
           stride /= 3;
           ...
        } while (stride > 1) {

> +	while (stride) {
> +		max = gidsetsize - stride;
> +
> +		for (left = 0; left < max; left++) {
> +			cur = left;
> +			while (cur >= 0) {
> +				right = cur + stride;
> +				if (grouplist[right] < grouplist[cur]) {
> +					gid_t tmp = grouplist[cur];
> +					grouplist[cur] = grouplist[right];
> +					grouplist[right] = tmp;
> +					cur -= stride;
> +				} else {
> +					break;
> +				}
> +			}

You should work by stuffing the new element in a temporary variable, and
just shift the greater ones up, then stuff the one in

My version of shellsort (h is stride, n is size) goes:

  /*
   * shellsort.c: Shell sort
   */

  #include "sort.h"

  void
  sort(double a[], int n)

  {
    int i, j, h;
    double tmp;

    for(h = 1; h < n; h = 3 * h + 1)
      ;

    do {
      h /= 3;
      for(i = h; i < n; i++) {
	tmp = a[i];
	for(j = i - h; j >= 0 && tmp < a[j]; j -= h)
	  a[j + h] = a[j];
	a[j + h] = tmp;
      }
    } while(h > 1);
  }

This gets around bad h (stride) values, and does just a bit more than 1
assignment per element moved in the inner loop (you do 3). Plus it is
shorter ;-)
-- 
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513

  reply	other threads:[~2002-11-15 16:24 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-14 23:26 Timothy Hockin
2002-11-15 16:30 ` Horst von Brand [this message]
     [not found] <mailman.1037316781.6599.linux-kernel2news@redhat.com>
2002-11-15  0:06 ` Pete Zaitcev
2002-11-15  0:14   ` Tim Hockin
2002-11-15  0:31     ` Pete Zaitcev
2002-11-15  0:46       ` Tim Hockin
2002-11-15  1:19         ` William Lee Irwin III
2002-11-15  1:45           ` Pete Zaitcev
2002-11-15  1:53             ` William Lee Irwin III
     [not found]         ` <3DD44742.2DFE4407@digeo.com>
2002-11-15  1:24           ` Tim Hockin
2002-11-15  1:30             ` Andrew Morton
2002-11-15  2:33               ` Tim Hockin
2002-11-15  2:41                 ` Andrew Morton
2002-11-15 15:13                   ` Alan Cox
2002-11-15  6:00               ` Aaron Lehmann
2002-11-15  1:04       ` Alan Cox
2002-11-15 13:32       ` Frank van Maarseveen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200211151630.gAFGUqfk024668@pincoya.inf.utfsm.cl \
    --to=vonbrand@inf.utfsm.cl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=thockin@sun.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®