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,85 @@
#include <iostream>
#include <windows.h>
//--------------------------------------------------------------------------------------------------
const int EL_COUNT = 77, LLEN = 11;
//--------------------------------------------------------------------------------------------------
class cocktailSort
{
public:
void sort( int* arr, int len )
{
bool notSorted = true;
while( notSorted )
{
notSorted = false;
for( int a = 0; a < len - 1; a++ )
{
if( arr[a] > arr[a + 1] )
{
sSwap( arr[a], arr[a + 1] );
notSorted = true;
}
}
if( !notSorted ) break;
notSorted = false;
for( int a = len - 1; a > 0; a-- )
{
if( arr[a - 1] > arr[a] )
{
sSwap( arr[a], arr[a - 1] );
notSorted = true;
}
}
}
}
private:
void sSwap( int& a, int& b )
{
int t = a;
a = b; b = t;
}
};
//--------------------------------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
srand( GetTickCount() );
cocktailSort cs;
int arr[EL_COUNT];
for( int x = 0; x < EL_COUNT; x++ )
arr[x] = rand() % EL_COUNT + 1;
std::cout << "Original: " << std::endl << "==========" << std::endl;
for( int x = 0; x < EL_COUNT; x += LLEN )
{
for( int s = x; s < x + LLEN; s++ )
std::cout << arr[s] << ", ";
std::cout << std::endl;
}
//DWORD now = GetTickCount();
cs.sort( arr, EL_COUNT );
//now = GetTickCount() - now;
std::cout << std::endl << std::endl << "Sorted: " << std::endl << "========" << std::endl;
for( int x = 0; x < EL_COUNT; x += LLEN )
{
for( int s = x; s < x + LLEN; s++ )
std::cout << arr[s] << ", ";
std::cout << std::endl;
}
std::cout << std::endl << std::endl << std::endl << std::endl;
//std::cout << now << std::endl << std::endl;
system( "pause" );
return 0;
}
//--------------------------------------------------------------------------------------------------

View file

@ -0,0 +1,18 @@
ctail(_, [], Rev, Rev, sorted) :- write(Rev), nl.
ctail(fwrd, [A,B|T], In, Rev, unsorted) :- A > B, !,
ctail(fwrd, [B,A|T], In, Rev, _).
ctail(bkwd, [A,B|T], In, Rev, unsorted) :- A < B, !,
ctail(bkwd, [B,A|T], In, Rev, _).
ctail(D,[A|T], In, Rev, Ch) :- !, ctail(D, T, [A|In], Rev, Ch).
cocktail([], []).
cocktail(In, [Min|Out]) :-
ctail(fwrd, In, [], [Max|Rev], SFlag),
( SFlag=sorted->reverse([Max|Rev], [Min|Out]);
(ctail(bkwd, Rev, [Max], [Min|Tmp], SortFlag),
(SortFlag=sorted->Out=Tmp; !, cocktail(Tmp, Out)))).
test :- In = [8,9,1,3,4,2,6,5,4],
writef(' input=%w\n', [In]),
cocktail(In, R),
writef('-> %w\n', [R]).

View file

@ -0,0 +1,16 @@
#lang racket
(require (only-in srfi/43 vector-swap!))
(define (cocktail-sort! xs)
(define (ref i) (vector-ref xs i))
(define (swap i j) (vector-swap! xs i j))
(define len (vector-length xs))
(define (bubble from to delta)
(for/fold ([swaps 0]) ([i (in-range from to delta)])
(cond [(> (ref i) (ref (+ i 1)))
(swap i (+ i 1)) (+ swaps 1)]
[swaps])))
(let loop ()
(cond [(zero? (bubble 0 (- len 2) 1)) xs]
[(zero? (bubble (- len 2) 0 -1)) xs]
[(loop)])))