|
|
Bugzilla Link |
572 |
Created on |
Oct 16, 2009 13:04 |
Resolution |
FIXED |
Resolved on |
Oct 20, 2009 11:29 |
Version |
1.00beta |
OS |
Linux |
Architecture |
PC |
Extended Description
/*
* SAC - bug report
* ================
*
* automatically generated on Fri Oct 16 13:00:52 BST 2009
*
* using sac2c v1.00-beta (Buchette d'Anjou) rev 16226 for linux-gnu_i686
* built Fri Jul 24 15:22:58 BST 2009.
* by user asv on host obelix for linux-gnu.
*
* The compiler was called by
* sac2c bitonic_nonrec.sac -mt -v3
*
* The contents of bitonic_nonrec.sac is:
*/
use StdIO: all;
use Array: all;
#define SIZE 1048576 /* 2^20 */
#define ASC 1
#define DESC 0
#define SWAP(array, __i, __j, order) \
do { \
if (order == ASC) { \
if (array[__i] > array[__j]) { \
t = array[__i]; \
array[__i] = array[__j]; \
array[__j] = t; \
} \
} \
else { \
if (!(array[__i] > array[__j])){ \
t = array[__i]; \
array[__i] = array[__j]; \
array[__j] = t; \
} \
} \
} while (false)
#define MERGE(array, order) \
do { \
__m = shape(array)[0] / 2; \
for (__i = 0; __i<__m; __i++){ \
SWAP(array, __i, (__i+__m), order); \
} \
} while (false)
int[.]
bitonic_sort(int[.] array, int direction) {
n = shape(array)[0];
m = n / 2;
/*
* Dividing array in subarrays: 2, 4, 8, 16, ... 2^N
* elements accordingly, sorting each part, alterning
* descending sorting and ascending sorting.
*/
for (i = 2; i <= n; i *= 2) {
m = n/i;
arr = reshape([m, i], array);
/* Independently inner merge and sort each part */
arr = with {
([0] <= [j] < [m]) {
part = arr[j];
if (direction == ASC) order = (j % 2 == 0? ASC: DESC);
else order = (j % 2 == 1? ASC: DESC);
/* Merge part first and the second half */
MERGE(part, order);
/* Recursively merge part, dividing the part
* by two each time, until we get part of
* size 2
*/
for (counter = 1; counter < i; counter *= 2) {
len = i / counter;
part1 = reshape([counter, len], part);
part1 = with {
( [0] <= [p] < [counter]) {
res = part1[p];
MERGE(res, order);
}: res;
}: genarray([counter],genarray([len], 0));
part = reshape([i], part1);
}
}: part;
} : genarray([m], genarray([i], 0));
array = reshape([n], arr);
}
return (array);
}
int
main() {
ip = FibreScanIntArray();
num = ip[3];
ip = bitonic_sort(ip, ASC);
print(ip[num]);
return (0);
}