xref: /illumos-gate/usr/src/cmd/refer/shell.c (revision 2a8bcb4e)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate /*
167c478bd9Sstevel@tonic-gate  * SORTS UP.
177c478bd9Sstevel@tonic-gate  * IF THERE ARE NO EXCHANGES (IEX=0) ON A SWEEP
187c478bd9Sstevel@tonic-gate  * THE COMPARISON GAP (IGAP) IS HALVED FOR THE NEXT SWEEP
197c478bd9Sstevel@tonic-gate  */
20*11a8fa6cSceastha void
shell(int n,int (* comp)(),int (* exch)())21*11a8fa6cSceastha shell(int n, int (*comp)(), int (*exch)())
227c478bd9Sstevel@tonic-gate {
237c478bd9Sstevel@tonic-gate 	int igap, iplusg, iex, i, imax;
24*11a8fa6cSceastha 	igap = n;
25*11a8fa6cSceastha 	while (igap > 1) {
267c478bd9Sstevel@tonic-gate 		igap /= 2;
277c478bd9Sstevel@tonic-gate 		imax = n-igap;
28*11a8fa6cSceastha 		do {
29*11a8fa6cSceastha 			iex = 0;
30*11a8fa6cSceastha 			for (i = 0; i < imax; i++) {
317c478bd9Sstevel@tonic-gate 				iplusg = i + igap;
32*11a8fa6cSceastha 				if ((*comp)(i, iplusg)) continue;
337c478bd9Sstevel@tonic-gate 				(*exch) (i, iplusg);
34*11a8fa6cSceastha 				iex = 1;
357c478bd9Sstevel@tonic-gate 			}
36*11a8fa6cSceastha 		}
37*11a8fa6cSceastha 		while (iex > 0)
38*11a8fa6cSceastha 			;
397c478bd9Sstevel@tonic-gate 	}
407c478bd9Sstevel@tonic-gate }
41