This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -0,0 +1,64 @@
#include <iostream>
#include <time.h>
//------------------------------------------------------------------------------
using namespace std;
//------------------------------------------------------------------------------
const int MAX = 30;
//------------------------------------------------------------------------------
class cSort
{
public:
void sort( int* arr, int len )
{
int mi, mx, z = 0; findMinMax( arr, len, mi, mx );
int nlen = ( mx - mi ) + 1; int* temp = new int[nlen];
memset( temp, 0, nlen * sizeof( int ) );
for( int i = 0; i < len; i++ ) temp[arr[i] - mi]++;
for( int i = mi; i <= mx; i++ )
{
while( temp[i - mi] )
{
arr[z++] = i;
temp[i - mi]--;
}
}
delete [] temp;
}
private:
void findMinMax( int* arr, int len, int& mi, int& mx )
{
mi = INT_MAX; mx = 0;
for( int i = 0; i < len; i++ )
{
if( arr[i] > mx ) mx = arr[i];
if( arr[i] < mi ) mi = arr[i];
}
}
};
//------------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
srand( time( NULL ) ); int arr[MAX];
for( int i = 0; i < MAX; i++ )
arr[i] = rand() % 140 - rand() % 40 + 1;
for( int i = 0; i < MAX; i++ )
cout << arr[i] << ", ";
cout << endl << endl;
cSort s; s.sort( arr, MAX );
for( int i = 0; i < MAX; i++ )
cout << arr[i] << ", ";
cout << endl << endl;
return system( "pause" );
}
//------------------------------------------------------------------------------

View file

@ -0,0 +1,69 @@
/* NetRexx */
options replace format comments java crossref savelog symbols binary
import java.util.List
icounts = [int -
1, 3, 6, 2, 7, 13, 20, 12, 21, 11 -
, 22, 10, 23, 9, 24, 8, 25, 43, 62, 42 -
, 63, 41, 18, 42, 17, 43, 16, 44, 15, 45 -
, 14, 46, 79, 113, 78, 114, 77, 39, 78, 38 -
]
scounts = int[icounts.length]
System.arraycopy(icounts, 0, scounts, 0, icounts.length)
lists = [ -
icounts -
, countingSort(scounts) -
]
loop ln = 0 to lists.length - 1
cl = lists[ln]
rep = Rexx('')
loop ct = 0 to cl.length - 1
rep = rep cl[ct]
end ct
say '['rep.strip.changestr(' ', ',')']'
end ln
return
method getMin(array = int[]) public constant binary returns int
amin = Integer.MAX_VALUE
loop x_ = 0 to array.length - 1
if array[x_] < amin then
amin = array[x_]
end x_
return amin
method getMax(array = int[]) public constant binary returns int
amax = Integer.MIN_VALUE
loop x_ = 0 to array.length - 1
if array[x_] > amax then
amax = array[x_]
end x_
return amax
method countingSort(array = int[], amin = getMin(array), amax = getMax(array)) public constant binary returns int[]
count = int[amax - amin + 1]
loop nr = 0 to array.length - 1
numbr = array[nr]
count[numbr - amin] = count[numbr - amin] + 1
end nr
z_ = 0
loop i_ = amin to amax
loop label count while count[i_ - amin] > 0
array[z_] = i_
z_ = z_ + 1
count[i_ - amin] = count[i_ - amin] - 1
end count
end i_
return array

View file

@ -0,0 +1,50 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method countingSort(icounts) public constant
parse getMinMax(icounts) amin amax
array = 0
loop ix = 1 to icounts.words
iw = icounts.word(ix) + 0
array[iw] = array[iw] + 1
end ix
ocounts = ''
loop ix = amin to amax
if array[ix] = 0 then iterate ix
loop for array[ix]
ocounts = ocounts ix
end
end ix
return ocounts.space
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getMinMax(icounts) public constant
amin = Long.MAX_VALUE
amax = Long.MIN_VALUE
loop x_ = 1 to icounts.words
amin = icounts.word(x_).min(amin)
amax = icounts.word(x_).max(amax)
end x_
return amin amax
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
parse arg icounts
if icounts = '' then -
icounts = -
' 1 3 6 2 7 13 20 12 21 11 22 10 23 9 24 8 25 43 62 42' -
'63 41 18 42 17 43 16 44 15 45 14 46 79 113 78 114 77 39 78 38' -
'0 -200 -6 -10 -0' -
''
say icounts.space
say countingSort(icounts)
return

View file

@ -0,0 +1,12 @@
#lang racket
(define (counting-sort xs min max)
(define ns (make-vector (+ max (- min) 1) 0))
(for ([x xs]) (vector-set! ns (- x min) (+ (vector-ref ns (- x min)) 1)))
(for/fold ([i 0]) ([n ns] [x (in-naturals)])
(for ([j (in-range i (+ i n ))])
(vector-set! xs j (+ x min)))
(+ i n))
xs)
(counting-sort (vector 0 9 3 8 1 -1 1 2 3 7 4) -1 10)

View file

@ -0,0 +1 @@
'#(-1 0 1 1 2 3 3 4 7 8 9)

View file

@ -1,10 +1,10 @@
class Array
def countingsort!
do_countingsort!(min, max)
def counting_sort
dup.counting_sort!
end
protected
def do_countingsort!(min, max)
def counting_sort!
min, max = minmax
count = Array.new(max - min + 1, 0)
each {|number| count[number - min] += 1}
z = 0
@ -20,5 +20,6 @@ class Array
end
ary = [9,7,10,2,9,7,4,3,10,2,7,10,2,1,3,8,7,3,9,5,8,5,1,6,3,7,5,4,6,9,9,6,6,10,2,4,5,2,8,2,2,5,2,9,3,3,5,7,8,4]
ary.countingsort!.join(",")
p ary.counting_sort.join(",")
# => "1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,6,6,7,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10"