-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.c
130 lines (111 loc) · 3.91 KB
/
sorting.c
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
/* tab:8
*
* sorting.c - fast sequential sorting routines
*
*
* "Copyright (c) 1994,1995,1996 The Regents of the University of Maryland.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF MARYLAND HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Authors: David A. Bader <dbader@umiacs.umd.edu>
* Joseph F. Ja'Ja' <joseph@umiacs.umd.edu>
* Institute for Advanced Computer Studies
* Department of Electrical Engineering
* AV Williams Building
* College Park, MD 20742
*
* Version: 1.0
* Creation Date: October 20, 1994
* Filename: sorting.c
* History:
*/
#include "sorting.h"
int intcompare(int *i, int *j)
{
return(*i - *j);
}
void seq_radixsort(int *a, int n) {
/* Radix sort a list of n integers, a[], between 0 and M-1,
where M = 2^m, and n = 2^w */
register int
i,
j,
m,
w,
M,
pass;
int nextpass,
*count,
*bitArr,
*b;
bitArr = (int *)malloc(n*sizeof(int));
assert_malloc(bitArr);
b = (int *)malloc(n*sizeof(int));
assert_malloc(b);
w = sizeof(int) << 3; /* The number of bits in the key */
m = w >> 2; /* m = number of bits per pass */
M = 1 << m; /* The range of each pass */
if ((count = (int*)malloc(M*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: seq_radixsort count could not be malloc'ed\n");
/* Note that the loop below runs two passes each time so that
a[] and b[] don't need to be swapped */
for (pass=0 ; pass<(w/m) ; pass+=2) {
for (j=0 ; j<M ; j++) count[j] = 0;
for (i=0 ; i<n ; i++) count[bitArr[i] = bits(a[i],pass*m,m)]++;
for (j=1 ; j<M ; j++) count[j] += count[j-1];
for (i=n-1 ; i>=0 ; i--) b[--count[bitArr[i]]] = a[i];
nextpass = pass+1;
for (j=0 ; j<M ; j++) count[j] = 0;
for (i=0 ; i<n ; i++) count[bitArr[i] = bits(b[i],nextpass*m,m)]++;
for (j=1 ; j<M ; j++) count[j] += count[j-1];
for (i=n-1 ; i>=0 ; i--) a[--count[bitArr[i]]] = b[i];
}
free(count);
free(b);
free(bitArr);
}
void seq_radixsort_19(int *a, int n) {
/* Radix sort a list of n integers, a[], between 0 and M-1,
where M = 2^m, and n = 2^w */
/* Assume m = 19 */
register int
i,
j;
int *count,
*bitArr,
*b;
bitArr = (int *)malloc(n*sizeof(int));
assert_malloc(bitArr);
b = (int *)malloc(n*sizeof(int));
assert_malloc(b);
count = (int*)malloc(1024*sizeof(int));
assert_malloc(count);
/* Note that the loop below runs two passes each time so that
a[] and b[] don't need to be swapped */
for (j=0 ; j<1024 ; j++) count[j] = 0;
for (i=0 ; i<n ; i++) count[bitArr[i] = bits(a[i],0,10)]++;
for (j=1 ; j<1024 ; j++) count[j] += count[j-1];
for (i=n-1 ; i>=0 ; i--) b[--count[bitArr[i]]] = a[i];
for (j=0 ; j<512 ; j++) count[j] = 0;
for (i=0 ; i<n ; i++) count[bitArr[i] = bits(b[i],10,9)]++;
for (j=1 ; j<512 ; j++) count[j] += count[j-1];
for (i=n-1 ; i>=0 ; i--) a[--count[bitArr[i]]] = b[i];
free(count);
free(b);
free(bitArr);
}