Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,5 +1,6 @@
{{Sorting Algorithm}}{{wikipedia|Cocktail sort}}
The cocktail shaker sort is an improvement on the [[Bubble Sort]]. The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from [[wp:Cocktail sort|wikipedia]]):
The cocktail shaker sort is an improvement on the [[Bubble Sort]].
The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from [[wp:Cocktail sort|wikipedia]]):
'''function''' ''cocktailSort''( A : list of sortable items )
'''do'''
swapped := false

View file

@ -1,10 +1,8 @@
#include <iostream>
#include <windows.h>
//--------------------------------------------------------------------------------------------------
const int EL_COUNT = 77, LLEN = 11;
//--------------------------------------------------------------------------------------------------
class cocktailSort
{
public:
@ -44,13 +42,13 @@ private:
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;
@ -66,7 +64,7 @@ int main( int argc, char* argv[] )
//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 )
{
@ -78,8 +76,5 @@ int main( int argc, char* argv[] )
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,35 @@
#include <algorithm>
#include <iostream>
#include <iterator>
template <typename RandomAccessIterator>
void cocktail_sort(RandomAccessIterator begin, RandomAccessIterator end) {
bool swapped = true;
while (begin != end-- && swapped) {
swapped = false;
for (auto i = begin; i != end; ++i) {
if (*(i + 1) < *i) {
std::iter_swap(i, i + 1);
swapped = true;
}
}
if (!swapped) {
break;
}
swapped = false;
for (auto i = end - 1; i != begin; --i) {
if (*i < *(i - 1)) {
std::iter_swap(i, i - 1);
swapped = true;
}
}
++begin;
}
}
int main() {
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
cocktail_sort(std::begin(a), std::end(a));
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}

View file

@ -0,0 +1,37 @@
// Written in the D programming language.
module rosettaCode.sortingAlgorithms.cocktailSort;
import std.range;
Range cocktailSort(Range)(Range data)
if (isRandomAccessRange!Range && hasLvalueElements!Range) {
import std.algorithm : swap;
bool swapped = void;
void trySwap(E)(ref E lhs, ref E rhs) {
if (lhs > rhs) {
swap(lhs, rhs);
swapped = true;
}
}
if (data.length > 0) do {
swapped = false;
foreach (i; 0 .. data.length - 1)
trySwap(data[i], data[i + 1]);
if (!swapped)
break;
swapped = false;
foreach_reverse (i; 0 .. data.length - 1)
trySwap(data[i], data[i + 1]);
} while(swapped);
return data;
}
unittest {
assert (cocktailSort([3, 1, 5, 2, 4]) == [1, 2, 3, 4, 5]);
assert (cocktailSort([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5]);
assert (cocktailSort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5]);
assert (cocktailSort((int[]).init) == []);
assert (cocktailSort(["John", "Kate", "Zerg", "Alice", "Joe", "Jane"]) ==
["Alice", "Jane", "Joe", "John", "Kate", "Zerg"]);
}

View file

@ -0,0 +1,7 @@
import rosettaCode.sortingAlgorithms.cocktailSort;
void main() {
import std.stdio, std.algorithm, std.range, std.random;
//generate 10 sorted random numbers in [0 .. 10)
rndGen.take(10).map!(a=>a%10).array.cocktailSort.writeln();
}

View file

@ -1,26 +0,0 @@
import std.stdio, std.algorithm;
void cocktailSort(Range)(Range data) {
bool swapped = false;
do {
foreach (i; 0 .. data.length - 1)
if (data[i] > data[i + 1]) {
swap(data[i], data[i + 1]);
swapped = true;
}
if (!swapped)
break;
swapped = false;
foreach_reverse (i; 0 .. data.length - 1)
if (data[i] > data[i + 1]) {
swap(data[i], data[i + 1]);
swapped = true;
}
} while(swapped);
}
void main() {
auto array = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"];
cocktailSort(array);
writeln(array);
}

View file

@ -0,0 +1,50 @@
class
COCKTAIL_SORT[G -> COMPARABLE]
feature
cocktail_sort(ar: ARRAY[G]): ARRAY[G]
require
ar_not_empty: ar.count>=1
local
swapped, finished: BOOLEAN
sol: ARRAY[G]
i,j : INTEGER
t: G
do
create sol.make_from_array (ar)
from
until finished= TRUE
loop
swapped := FALSE
from
i:= 1
until
i= ar.count-1
loop
if ar[i]> ar[i+1] then
t:= ar[i]
ar[i]:= ar[i+1]
ar[i+1]:= t
swapped:= true
end
i:= i+1
end
from j:= ar.count-1
until j= 1
loop
if ar[j]> ar[j+1] then
t:= ar[j]
ar[j]:= ar[j+1]
ar[j+1]:= t
swapped:= TRUE
end
j:= j-1
end
if swapped= FALSE then
finished:= TRUE
sol:= ar
end
end
Result:= sol
end
end

View file

@ -0,0 +1,18 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
test:= <<5,1,99,3,2>>
across test as t loop io.put_string (t.item.out + "%T") end
create cs
test:= cs.cocktail_sort(test)
across test as ar loop io.put_string (ar.item.out+"%T") end
end
cs: COCKTAIL_SORT[INTEGER]
test: ARRAY[INTEGER]
end