June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,22 +1,24 @@
|
|||
import std.stdio, std.algorithm;
|
||||
import std.stdio, std.algorithm : swap;
|
||||
|
||||
void bubbleSort(T)(T[] data) pure nothrow {
|
||||
auto itemCount = data.length;
|
||||
bool hasChanged = false;
|
||||
|
||||
do {
|
||||
hasChanged = false;
|
||||
itemCount--;
|
||||
foreach (immutable i; 0 .. itemCount)
|
||||
T[] bubbleSort(T)(T[] data) pure nothrow
|
||||
{
|
||||
foreach_reverse (n; 0 .. data.length)
|
||||
{
|
||||
bool swapped;
|
||||
foreach (i; 0 .. n)
|
||||
if (data[i] > data[i + 1]) {
|
||||
swap(data[i], data[i + 1]);
|
||||
hasChanged = true;
|
||||
swapped = true;
|
||||
}
|
||||
} while (hasChanged);
|
||||
if (!swapped)
|
||||
break;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
||||
void main()
|
||||
{
|
||||
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
|
||||
array.bubbleSort();
|
||||
writeln(array);
|
||||
writeln(array.bubbleSort());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue