September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,17 @@
|
|||
# v0.6
|
||||
|
||||
function insertionsort!(A::Array{T}) where T <: Number
|
||||
for i in 1:length(A)-1
|
||||
value = A[i+1]
|
||||
j = i
|
||||
while j > 0 && A[j] > value
|
||||
A[j+1] = A[j]
|
||||
j -= 1
|
||||
end
|
||||
A[j+1] = value
|
||||
end
|
||||
return A
|
||||
end
|
||||
|
||||
x = randn(5)
|
||||
@show x insertionsort!(x)
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
/* REXX program sorts a stemmed array (has characters) */
|
||||
/* using the insertion sort algorithm */
|
||||
Call gen /* fill the array with test data */
|
||||
Call show 'before sort' /* display the elements */
|
||||
Say copies('-',79) /* display a separator line */
|
||||
Call insertionSort x.0 /* invoke the insertion sort. */
|
||||
Call show ' after sort' /* display the elements after sort*/
|
||||
Exit
|
||||
/*--------------------------------------------------------------------*/
|
||||
gen: Procedure Expose x.
|
||||
x.1="---Monday's Child Is Fair of Face (by Mother Goose)---"
|
||||
x.2="======================================================="
|
||||
x.3="Monday's child is fair of face;"
|
||||
x.4="Tuesday's child is full of grace;"
|
||||
x.5="Wednesday's child is full of woe;"
|
||||
x.6="Thursday's child has far to go;"
|
||||
x.7="Friday's child is loving and giving;"
|
||||
x.8="Saturday's child works hard for a living;"
|
||||
x.9="But the child that is born on the Sabbath day"
|
||||
x.10="Is blithe and bonny, good and gay."
|
||||
x.0=10 /* number of elements */
|
||||
Return
|
||||
/*--------------------------------------------------------------------*/
|
||||
insertionsort: Procedure Expose x.
|
||||
Parse Arg n
|
||||
Do i=2 To n
|
||||
y=x.i
|
||||
Do j=i-1 By -1 To 1 While x.j>y
|
||||
z=j+1
|
||||
x.z=x.j
|
||||
/* Say 'set x.'z 'to x.'j '('||x.j||')' */
|
||||
End
|
||||
z=j+1
|
||||
x.z=y
|
||||
/* Say 'set x.'z 'to' y */
|
||||
End
|
||||
Return
|
||||
/*--------------------------------------------------------------------*/
|
||||
show:
|
||||
Do j=1 To x.0
|
||||
Say 'Element' right(j,length(x.0)) arg(1)":" x.j
|
||||
End
|
||||
Return
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
class Array {
|
||||
method insertion_sort {
|
||||
{ |i|
|
||||
var j = i;
|
||||
var k = self[i];
|
||||
while ((j > 0) && (k < self[j - 1])) {
|
||||
self[j] = self[j - 1];
|
||||
j--;
|
||||
};
|
||||
self[j] = k;
|
||||
} * self.end;
|
||||
return self;
|
||||
var j = i-1
|
||||
var k = self[i]
|
||||
while ((j >= 0) && (k < self[j])) {
|
||||
self[j+1] = self[j]
|
||||
j--
|
||||
}
|
||||
self[j+1] = k
|
||||
} << 1..self.end
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
var a = 10.of {100.rand.int};
|
||||
say a.insertion_sort;
|
||||
var a = 10.of { 100.irand }
|
||||
say a.insertion_sort
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
fun insertion_sort cmp = let
|
||||
fun insert (x, []) = [x]
|
||||
| insert (x, y::ys) =
|
||||
case cmp (x, y) of GREATER => y :: insert (x, ys)
|
||||
| _ => x :: y :: ys
|
||||
in
|
||||
foldl insert []
|
||||
end;
|
||||
|
||||
insertion_sort Int.compare [6,8,5,9,3,2,1,4,7];
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fcn insertionSort(list){
|
||||
sink:=List();
|
||||
foreach x in (list){
|
||||
if(False==(n:=sink.filter1n('>(x)))) sink.append(x); // x>all items in sink
|
||||
else sink.insert(n,x);
|
||||
}
|
||||
sink.close();
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
insertionSort(T(4,65,2,-31,0,99,2,83,782,1)).println();
|
||||
insertionSort("big fjords vex quick waltz nymph".split()).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue