Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,7 +1,7 @@
|
|||
# insertion_sort(var [value1 value2...]) sorts a list of integers.
|
||||
function(insertion_sort var)
|
||||
math(EXPR last "${ARGC} - 1") # Sort ARGV[1..last].
|
||||
foreach(i RANGE 2 ${last})
|
||||
foreach(i RANGE 1 ${last})
|
||||
# Extend the sorted area to ARGV[1..i].
|
||||
set(b ${i})
|
||||
set(v ${ARGV${b}})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
defmodule Sort do
|
||||
def insert_sort(list) when is_list(list), do: insert_sort(list, [])
|
||||
|
||||
def insert_sort([], sorted), do: sorted
|
||||
def insert_sort([h | t], sorted), do: insert_sort(t, insert(h, sorted))
|
||||
|
||||
defp insert(x, []), do: [x]
|
||||
defp insert(x, sorted) when x < hd(sorted), do: [x | sorted]
|
||||
defp insert(x, [h | t]), do: [h | insert(x, t)]
|
||||
end
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
let rec insert x = function
|
||||
[] -> [x]
|
||||
| y :: ys ->
|
||||
if x <= y then x :: y :: ys
|
||||
else y :: insert x ys
|
||||
let rec insert lst x =
|
||||
match lst with
|
||||
[] -> [x]
|
||||
| y :: ys when x <= y -> x :: y :: ys
|
||||
| y :: ys -> y :: insert ys x
|
||||
|
||||
;;
|
||||
let insertion_sort lst = List.fold_right insert lst [];;
|
||||
let insertion_sort = List.fold_left insert [];;
|
||||
|
||||
insertion_sort [6;8;5;9;3;2;1;4;7];;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
function insertionSort($arr){
|
||||
for($i=0;$i -lt $arr.length;$i++){
|
||||
$val = $arr[$i]
|
||||
$j = $i-1
|
||||
while($j -ge 0 -and $arr[$j] -gt $val){
|
||||
$arr[$j+1] = $arr[$j]
|
||||
$j--
|
||||
}
|
||||
$arr[$j+1] = $val
|
||||
}
|
||||
}
|
||||
|
||||
$arr = @(4,2,1,6,9,3,8,7)
|
||||
insertionSort($arr)
|
||||
$arr -join ","
|
||||
|
|
@ -1,38 +1,30 @@
|
|||
/*REXX program sorts a stemmed array using the insertion-sort algoritm.*/
|
||||
call gen@ /*generate the array's elements. */
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call insertionSort # /*invoke the insertion sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @. = /*assign default value to array. */
|
||||
@.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
|
||||
@.2 = "Monday's child is fair of face;"
|
||||
@.3 = "Tuesday's child is full of grace;"
|
||||
@.4 = "Wednesday's child is full of woe;"
|
||||
@.5 = "Thursday's child has far to go;"
|
||||
@.6 = "Friday's child is loving and giving;"
|
||||
@.7 = "Saturday's child works hard for a living;"
|
||||
@.8 = "But the child that is born on the Sabbath day"
|
||||
@.9 = "Is blithe and bonny, good and gay."
|
||||
|
||||
do #=1 while @.#\=='' /*find how many entries in array.*/
|
||||
end /*#*/ /*short and sweet DO loop, eh? */
|
||||
#=#-1 /*because of DO, adjust # entries*/
|
||||
/*REXX program sorts a stemmed array using the insertion sort algorithm. */
|
||||
call gen /*generate the array's elements. */
|
||||
call show 'before sort' /*display the before array elements. */
|
||||
say copies('▒',79) /*display a separator line (a fence). */
|
||||
call insertionSort # /*invoke the insertion sort. */
|
||||
call show ' after sort' /*display the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────GEN subroutine────────────────────────────*/
|
||||
gen: @.=; @.1 = "---Monday's Child Is Fair of Face (by Mother Goose)---"
|
||||
@.2 = "Monday's child is fair of face;"
|
||||
@.3 = "Tuesday's child is full of grace;"
|
||||
@.4 = "Wednesday's child is full of woe;"
|
||||
@.5 = "Thursday's child has far to go;"
|
||||
@.6 = "Friday's child is loving and giving;"
|
||||
@.7 = "Saturday's child works hard for a living;"
|
||||
@.8 = "But the child that is born on the Sabbath day"
|
||||
@.9 = "Is blithe and bonny, good and gay."
|
||||
do #=1 while @.#\==''; end; #=#-1 /*determine how many entries in @ array*/
|
||||
return
|
||||
/*──────────────────────────────────INSERTIONSORT subroutine────────────*/
|
||||
insertionSort: procedure expose @. #
|
||||
do i=2 to #
|
||||
value=@.i; do j=i-1 by -1 while j\==0 & @.j>value
|
||||
jp=j+1; @.jp=@.j
|
||||
end /*j*/
|
||||
jp=j+1
|
||||
@.jp=value
|
||||
end /*i*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: do j=1 for #
|
||||
say 'element' right(j,length(#)) arg(1)': ' @.j
|
||||
end /*j*/
|
||||
say copies('─',79) /*show a separator line that fits*/
|
||||
/*──────────────────────────────────INSERTIONSORT subroutine──────────────────*/
|
||||
insertionSort: procedure expose @.; parse arg #
|
||||
do i=2 to #; $=@.i
|
||||
do j=i-1 by -1 while j\==0 & @.j>$
|
||||
_=j+1; @._=@.j
|
||||
end /*j*/
|
||||
_=j+1; @._=$
|
||||
end /*i*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW subroutine───────────────────────────*/
|
||||
show: do j=1 for #; say 'element' right(j,length(#)) arg(1)': ' @.j; end; return
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
fn insertion_sort<T: std::cmp::Ord>(arr: &mut [T]) {
|
||||
for i in range(1, arr.len()) {
|
||||
let mut j = i;
|
||||
while (j > 0 && arr[j] < arr[j-1]) {
|
||||
arr.swap(j, j-1);
|
||||
j = j-1;
|
||||
for i in 1..arr.len() {
|
||||
let mut j = i;
|
||||
while j > 0 && arr[j] < arr[j-1] {
|
||||
arr.swap(j, j-1);
|
||||
j = j-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue