This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,66 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. quicksort RECURSIVE.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 temp PIC S9(8).
01 pivot PIC S9(8).
01 left-most-idx PIC 9(5).
01 right-most-idx PIC 9(5).
01 left-idx PIC 9(5).
01 right-idx PIC 9(5).
LINKAGE SECTION.
78 Arr-Length VALUE 50.
01 arr-area.
03 arr PIC S9(8) OCCURS Arr-Length TIMES.
01 left-val PIC 9(5).
01 right-val PIC 9(5).
PROCEDURE DIVISION USING REFERENCE arr-area, OPTIONAL left-val,
OPTIONAL right-val.
IF left-val IS OMITTED OR right-val IS OMITTED
MOVE 1 TO left-most-idx, left-idx
MOVE Arr-Length TO right-most-idx, right-idx
ELSE
MOVE left-val TO left-most-idx, left-idx
MOVE right-val TO right-most-idx, right-idx
END-IF
IF right-most-idx - left-most-idx < 1
GOBACK
END-IF
COMPUTE pivot = arr ((left-most-idx + right-most-idx) / 2)
PERFORM UNTIL left-idx > right-idx
PERFORM VARYING left-idx FROM left-idx BY 1
UNTIL arr (left-idx) >= pivot
END-PERFORM
PERFORM VARYING right-idx FROM right-idx BY -1
UNTIL arr (right-idx) <= pivot
END-PERFORM
IF left-idx <= right-idx
MOVE arr (left-idx) TO temp
MOVE arr (right-idx) TO arr (left-idx)
MOVE temp TO arr (right-idx)
ADD 1 TO left-idx
SUBTRACT 1 FROM right-idx
END-IF
END-PERFORM
CALL "quicksort" USING REFERENCE arr-area,
CONTENT left-most-idx, right-idx
CALL "quicksort" USING REFERENCE arr-area, CONTENT left-idx,
right-most-idx
GOBACK
.

View file

@ -1,14 +1,14 @@
import std.stdio;
import std.stdio, std.algorithm, std.range, std.array;
T[] quickSort(T)(T[] items) {
if (items.length <= 1)
auto quickSort(T)(T[] items) /*pure*/ nothrow {
if (items.length < 2)
return items;
T[] less, more;
foreach (x; items[1 .. $])
(x < items[0] ? less : more) ~= x;
return quickSort(less) ~ items[0] ~ quickSort(more);
auto pivot = items[0];
return items[1 .. $].filter!(x => x < pivot).array.quickSort ~
pivot ~
items[1 .. $].filter!(x => x >= pivot).array.quickSort;
}
void main() {
writeln(quickSort([4, 65, 2, -31, 0, 99, 2, 83, 782, 1]));
[4, 65, 2, -31, 0, 99, 2, 83, 782, 1].quickSort.writeln;
}

View file

@ -1,18 +1,14 @@
import std.stdio;
import std.algorithm;
import std.stdio, std.array;
void quickSort(T)(T[] items)
{
if (items.length >= 2) {
auto parts = partition3(items, items[$ / 2]);
quickSort(parts[0]);
quickSort(parts[2]);
}
T[] quickSort(T)(T[] items) pure nothrow {
if (items.empty)
return items;
T[] less, notLess;
foreach (x; items[1 .. $])
(x < items[0] ? less : notLess) ~= x;
return less.quickSort ~ items[0] ~ notLess.quickSort;
}
void main()
{
auto items = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
quickSort(items);
writeln(items);
void main() {
[4, 65, 2, -31, 0, 99, 2, 83, 782, 1].quickSort.writeln;
}

View file

@ -0,0 +1,15 @@
import std.stdio, std.algorithm;
void quickSort(T)(T[] items) pure nothrow {
if (items.length >= 2) {
auto parts = partition3(items, items[$ / 2]);
parts[0].quickSort;
parts[2].quickSort;
}
}
void main() {
auto items = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
items.quickSort;
items.writeln;
}

View file

@ -0,0 +1 @@
QUICKSORT

View file

@ -0,0 +1,94 @@
class
QUICKSORT [G -> COMPARABLE]
create
make
feature {NONE} --Implementation
is_sorted (list: ARRAY [G]): BOOLEAN
require
not_void: list /= Void
local
i: INTEGER
do
Result := True
from
i := list.lower + 1
invariant
i >= list.lower + 1 and i <= list.upper + 1
until
i > list.upper
loop
Result := Result and list [i - 1] <= list [i]
i := i + 1
variant
list.upper + 1 - i
end
end
concatenate_array (a: ARRAY [G] b: ARRAY [G]): ARRAY [G]
require
not_void: a /= Void and b /= Void
do
create Result.make_from_array (a)
across
b as t
loop
Result.force (t.item, Result.upper + 1)
end
ensure
same_size: a.count + b.count = Result.count
end
quicksort_array (list: ARRAY [G]): ARRAY [G]
require
not_void: list /= Void
local
less_a: ARRAY [G]
equal_a: ARRAY [G]
more_a: ARRAY [G]
pivot: G
do
create less_a.make_empty
create more_a.make_empty
create equal_a.make_empty
create Result.make_empty
if list.count <= 1 then
Result := list
else
pivot := list [list.lower]
across
list as li
invariant
less_a.count + equal_a.count + more_a.count <= list.count
loop
if li.item < pivot then
less_a.force (li.item, less_a.upper + 1)
elseif li.item = pivot then
equal_a.force (li.item, equal_a.upper + 1)
elseif li.item > pivot then
more_a.force (li.item, more_a.upper + 1)
end
end
Result := concatenate_array (Result, quicksort_array (less_a))
Result := concatenate_array (Result, equal_a)
Result := concatenate_array (Result, quicksort_array (more_a))
end
ensure
same_size: list.count = Result.count
sorted: is_sorted (Result)
end
feature -- Initialization
make
do
end
quicksort (a: ARRAY [G]): ARRAY [G]
do
Result := quicksort_array (a)
end
end

View file

@ -0,0 +1,28 @@
class
APPLICATION
create
make
feature {NONE} -- Initialization
make
-- Run application.
local
test: ARRAY [INTEGER]
sorted: ARRAY [INTEGER]
sorter: QUICKSORT [INTEGER]
do
create sorter.make
test := <<1, 3, 2, 4, 5, 5, 7, -1>>
sorted := sorter.quicksort (test)
across
sorted as s
loop
print (s.item)
print (" ")
end
print ("%N")
end
end

View file

@ -25,7 +25,7 @@ integer :: marker
if (nA > 1) then
call random_number(random)
pivot = A(int(random*real(nA-1))+1)%value ! random pivot (not best performance, but avoids worst-case)
pivot = A(int(random*real(nA-1))+1)%value ! random pivor (not best performance, but avoids worst-case)
left = 0
right = nA + 1
@ -67,7 +67,7 @@ implicit none
integer, parameter :: l = 8
type (group), dimension(l) :: A
integer, dimension(3) :: seed = [1, 2, 3]
integer, dimension(12) :: seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
integer :: i
real :: random

View file

@ -10,7 +10,7 @@ Begin
Repeat
While pivot > X[i] Do i:=i+1;
While pivot < X[j] Do j:=j-1;
If i<j Then Begin
If i<=j Then Begin
tmp:=X[i];
X[i]:=X[j];
X[j]:=tmp;