1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/*
Theseus - maximum likelihood superpositioning of macromolecular structures
Copyright (C) 2004-2015 Douglas L. Theobald
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the:
Free Software Foundation, Inc.,
59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
-/_|:|_|_\-
*/
/* -/_|:|_|_\- */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "quicksort.h"
/* 15 is pretty good -- any array segment below this gets insorted */
#ifndef CUTOFF
# define CUTOFF 15
#endif
extern void
swapd(double *x, double *y)
{
double tempd;
tempd = *x;
*x = *y;
*y = tempd;
}
extern void
swapc(char **x, char **y)
{
char *tempc;
tempc = *x;
*x = *y;
*y = tempc;
}
/*
| void
| partial_quicksort (KEY_T *array1, COARRAY_T *array2, int lower, int upper)
|
| Abstract:
| Sort array1[lower..upper] into a partial order
| leaving segments which are CUTOFF elements long
| unsorted internally.
|
| Efficiency:
| I use a randomly generated pivot, which ensures maximum
| average efficiency. Also, you could use median of three
| method to choose a pivot, or just use the first item
| in the array (very bad if array is almost presorted).
|
| Method:
| Partial Quicksort with a sentinel (Robert Sedgewick)
|
| !! NOTE:
| array1[upper+1] must hold the maximum possible key.
*/
void
partial_quicksort2 (KEY_T *array1, COARRAY_T *array2, int lower, int upper)
{
int i, j, random_index;
KEY_T pivot;
if (upper - lower > CUTOFF)
{
swapd(&array1[lower], &array1[(upper+lower)/2]);
swapc(&array2[lower], &array2[(upper+lower)/2]);
i = lower;
j = upper + 1;
/* pivot = array1[lower]; */
srand(time(NULL));
random_index = (int) ( (double)(upper - lower) * (double) rand() / (double) RAND_MAX ) + lower;
pivot = array1[random_index];
while (1)
{
/*
* ---------------------- !! NOTE ----------------------
* ignoring NOTE above can lead to an infinite loop here
* -----------------------------------------------------
*/
do
i++;
while (LT(array1[i], pivot));
do
j--;
while (GT(array1[j], pivot));
if (j > i)
{
swapd(&array1[i], &array1[j]);
swapc(&array2[i], &array2[j]);
}
else
break;
}
swapd(&array1[lower], &array1[j]);
swapc(&array2[lower], &array2[j]);
partial_quicksort2 (array1, array2, lower, j - 1);
partial_quicksort2 (array1, array2, i, upper);
}
}
void
partial_quicksort2d (KEY_T *array1, KEY_T *array2, int lower, int upper)
{
int i, j, random_index;
KEY_T pivot;
if (upper - lower > CUTOFF)
{
swapd(&array1[lower], &array1[(upper+lower)/2]);
swapd(&array2[lower], &array2[(upper+lower)/2]);
i = lower;
j = upper + 1;
/* pivot = array1[lower]; */
srand(time(NULL));
random_index = (int) ( (double)(upper - lower) * (double) rand() / (double) RAND_MAX ) + lower;
pivot = array1[random_index];
while (1)
{
/*
* ---------------------- !! NOTE ----------------------
* ignoring NOTE above can lead to an infinite loop here
* -----------------------------------------------------
*/
do
i++;
while (LT(array1[i], pivot));
do
j--;
while (GT(array1[j], pivot));
if (j > i)
{
swapd(&array1[i], &array1[j]);
swapd(&array2[i], &array2[j]);
}
else
break;
}
swapd(&array1[lower], &array1[j]);
swapd(&array2[lower], &array2[j]);
partial_quicksort2d (array1, array2, lower, j - 1);
partial_quicksort2d (array1, array2, i, upper);
}
}
void
partial_quicksort (KEY_T *array, int lower, int upper)
{
int i, j, random_index;
KEY_T pivot;
if (upper - lower > CUTOFF)
{
swapd(&array[lower], &array[(upper+lower)/2]);
i = lower;
j = upper + 1;
/* pivot = array1[lower]; */
srand(time(NULL));
random_index = (int) ( (double)(upper - lower) * (double) rand() / (double) RAND_MAX ) + lower;
pivot = array[random_index];
while (1)
{
/*
* ---------------------- !! NOTE ----------------------
* ignoring NOTE above can lead to an infinite loop here
* -----------------------------------------------------
*/
do
i++;
while (LT(array[i], pivot));
do
j--;
while (GT(array[j], pivot));
if (j > i)
{
swapd(&array[i], &array[j]);
}
else
break;
}
swapd(&array[lower], &array[j]);
partial_quicksort (array, lower, j - 1);
partial_quicksort (array, i, upper);
}
}
/*
| void insort (KEY_T array1[], int len)
|
| Abstract: Sort array1[0..len-1] into increasing order.
|
| Method: Optimized insertion-sort (ala Jon Bentley)
*/
void
insort2 (KEY_T *array1, COARRAY_T *array2, int len)
{
int i, j;
KEY_T temp1;
COARRAY_T temp2;
for (i = 1; i < len; ++i)
{
j = i;
temp1 = array1[j];
temp2 = array2[j];
while ((j > 0) && GT(array1[j-1], temp1))
{
array1[j] = array1[j-1];
array2[j] = array2[j-1];
j--;
}
array1[j] = temp1;
array2[j] = temp2;
}
}
void
insort2d (KEY_T *array1, KEY_T *array2, int len)
{
int i, j;
KEY_T temp1;
KEY_T temp2;
for (i = 1; i < len; ++i)
{
j = i;
temp1 = array1[j];
temp2 = array2[j];
while ((j > 0) && GT(array1[j-1], temp1))
{
array1[j] = array1[j-1];
array2[j] = array2[j-1];
j--;
}
array1[j] = temp1;
array2[j] = temp2;
}
}
void
insort (KEY_T *array, int len)
{
int i, j;
KEY_T temp;
for (i = 1; i < len; ++i)
{
j = i;
temp = array[j];
while ((j > 0) && GT(array[j-1], temp))
{
array[j] = array[j-1];
j--;
}
array[j] = temp;
}
}
/*
| void quicksort (KEY_T array1[], int len)
|
| Abstract:
| Sort array1[0..len-1] into increasing order.
|
| Method:
| Use partial_quicksort() with a sentinel (ala Sedgewick)
| to reach a partial order, leave the unsorted segments of
| length <= CUTOFF to low-overhead straight insertion sort.
|
| !! NOTE:
| array1[len] must be a "sentinel" -- the largest
| possible value (e.g, array1[len] = HUGE_VAL;)
*/
void
quicksort2 (KEY_T *array1, COARRAY_T *array2, int len)
{
partial_quicksort2 (array1, array2, 0, len - 1);
insort2 (array1, array2, len);
}
void
quicksort2d (KEY_T *array1, KEY_T *array2, int len)
{
partial_quicksort2d (array1, array2, 0, len - 1);
insort2d (array1, array2, len);
}
void
quicksort (KEY_T *array, int len)
{
partial_quicksort (array, 0, len - 1);
insort (array, len);
}
|