Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S261432AbVAXDol (ORCPT ); Sun, 23 Jan 2005 22:44:41 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S261433AbVAXDol (ORCPT ); Sun, 23 Jan 2005 22:44:41 -0500 Received: from rwcrmhc12.comcast.net ([216.148.227.85]:427 "EHLO rwcrmhc12.comcast.net") by vger.kernel.org with ESMTP id S261432AbVAXDog (ORCPT ); Sun, 23 Jan 2005 22:44:36 -0500 Subject: Re: [patch 1/13] Qsort From: Charles R Harris To: linux-kernel@vger.kernel.org Content-Type: text/plain Date: Sun, 23 Jan 2005 20:44:33 -0700 Message-Id: <1106538274.32342.13.camel@tethys> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-3) Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2114 Lines: 101 Here are semi-templated versions of quicksort and heapsort, just for completeness. The quicksort uses the median of three. Chuck void quicksort0( *pl, *pr) { vp, SWAP_temp; *stack[100], **sptr = stack, *pm, *pi, *pj, *pt; for(;;) { while ((pr - pl) > 15) { /* quicksort partition */ pm = pl + ((pr - pl) >> 1); if ((*pm,*pl)) SWAP(*pm,*pl); if ((*pr,*pm)) SWAP(*pr,*pm); if ((*pm,*pl)) SWAP(*pm,*pl); vp = *pm; pi = pl; pj = pr - 1; SWAP(*pm,*pj); for(;;) { do ++pi; while ((*pi,vp)); do --pj; while ((vp,*pj)); if (pi >= pj) break; SWAP(*pi,*pj); } SWAP(*pi,*(pr-1)); /* push largest partition on stack */ if (pi - pl < pr - pi) { *sptr++ = pi + 1; *sptr++ = pr; pr = pi - 1; }else{ *sptr++ = pl; *sptr++ = pi - 1; pl = pi + 1; } } /* insertion sort */ for(pi = pl + 1; pi <= pr; ++pi) { vp = *pi; for(pj = pi, pt = pi - 1; pj > pl && (vp, *pt);) { *pj-- = *pt--; } *pj = vp; } if (sptr == stack) break; pr = *(--sptr); pl = *(--sptr); } } void heapsort0( *a, long n) { tmp; long i,j,l; /* The array needs to be offset by one for heapsort indexing */ a -= 1; for (l = n>>1; l > 0; --l) { tmp = a[l]; for (i = l, j = l<<1; j <= n;) { if (j < n && (a[j], a[j+1])) j += 1; if ((tmp, a[j])) { a[i] = a[j]; i = j; j += j; }else break; } a[i] = tmp; } for (; n > 1;) { tmp = a[n]; a[n] = a[1]; n -= 1; for (i = 1, j = 2; j <= n;) { if (j < n && (a[j], a[j+1])) j++; if ((tmp, a[j])) { a[i] = a[j]; i = j; j += j; }else break; } a[i] = tmp; } } - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/