1 /* =============================================================================
5 * =============================================================================
9 * Copyright (C) 2002 Michael Ringgaard. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the project nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE
36 * =============================================================================
38 * Modifed October 2007 by Chi Cao Minh
39 * -- Changed signature of comparison function
41 * =============================================================================
43 * For the license of bayes/sort.h and bayes/sort.c, please see the header
46 * ------------------------------------------------------------------------
48 * Unless otherwise noted, the following license applies to STAMP files:
50 * Copyright (c) 2007, Stanford University
51 * All rights reserved.
53 * Redistribution and use in source and binary forms, with or without
54 * modification, are permitted provided that the following conditions are
57 * * Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
60 * * Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in
62 * the documentation and/or other materials provided with the
65 * * Neither the name of Stanford University nor the names of its
66 * contributors may be used to endorse or promote products derived
67 * from this software without specific prior written permission.
69 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
70 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
71 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
72 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
73 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
74 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
75 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
77 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
78 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
79 * THE POSSIBILITY OF SUCH DAMAGE.
81 * =============================================================================
92 /* =============================================================================
94 * =============================================================================
97 swap (byte[] base, int a, int b, int width)
109 /* =============================================================================
111 * =============================================================================
114 public static void shortsort(byte[] base,
123 for(int p = (lo + width); p <= hi; p += width) {
124 if(cmp(base, p, max, n, offset) > 0) {
128 swap(base, max, hi, width);
133 /* =============================================================================
135 * =============================================================================
145 if (num < 2 || width == 0) {
150 * Pointers that keep track of
151 * where to start looking in
154 int[] lostk= new int[30];
155 int[] histk= new int[30];
160 int hi = start + (width * (num - 1));
175 size = (pvhi - pvlo) / pvwidth + 1;
177 if (size <= CUTOFF) {
179 shortsort(base, pvlo, pvhi, pvwidth, pvn, offset);
183 pvmid = pvlo + (size / 2) * pvwidth;
184 swap(base, pvmid, pvlo, pvwidth);
187 pvhiguy = pvhi + pvwidth;
192 } while (pvloguy <= pvhi && cmp(base, pvloguy, pvlo, pvn, offset) <= 0);
195 } while (pvhiguy > pvlo && cmp(base, pvhiguy, pvlo, pvn, offset) >= 0);
196 if (pvhiguy < pvloguy) {
199 swap(base, pvloguy, pvhiguy, pvwidth);
202 swap(base, pvlo, pvhiguy, pvwidth);
204 if ((pvhiguy - 1 - pvlo) >= (pvhi - pvloguy)) {
205 if (pvlo + pvwidth < pvhiguy) {
206 lostk[stkptr] = pvlo;
207 histk[stkptr] = pvhiguy - pvwidth;
211 if (pvloguy < pvhi) {
216 if (pvloguy < pvhi) {
217 lostk[stkptr] = pvloguy;
218 histk[stkptr] = pvhi;
221 if (pvlo + pvwidth < pvhiguy) {
222 pvhi = pvhiguy - pvwidth;
230 pvlo = lostk[stkptr];
231 pvhi = histk[stkptr];
238 /* =============================================================================
240 * =============================================================================
244 cmp(byte[] base, int p1, int p2, int n, int offset)
247 int s1 = p1 + offset;
248 int s2 = p2 + offset;
264 /* =============================================================================
268 * =============================================================================