Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,60 @@
|
|||
begin
|
||||
% As algol W does not allow overloading, we have to have type-specific %
|
||||
% sorting procedures - this bubble sorts an integer array %
|
||||
% as there is no way for the procedure to determine the array bounds, we %
|
||||
% pass the lower and upper bounds in lb and ub %
|
||||
procedure bubbleSortIntegers( integer array item( * )
|
||||
; integer value lb
|
||||
; integer value ub
|
||||
) ;
|
||||
begin
|
||||
integer lower, upper;
|
||||
|
||||
lower := lb;
|
||||
upper := ub;
|
||||
|
||||
while
|
||||
begin
|
||||
logical swapped;
|
||||
upper := upper - 1;
|
||||
swapped := false;
|
||||
for i := lower until upper
|
||||
do begin
|
||||
if item( i ) > item( i + 1 )
|
||||
then begin
|
||||
integer val;
|
||||
val := item( i );
|
||||
item( i ) := item( i + 1 );
|
||||
item( i + 1 ) := val;
|
||||
swapped := true;
|
||||
end if_must_swap ;
|
||||
end for_i ;
|
||||
swapped
|
||||
end
|
||||
do begin end;
|
||||
end bubbleSortIntegers ;
|
||||
|
||||
begin % test the bubble sort %
|
||||
integer array data( 1 :: 10 );
|
||||
|
||||
procedure writeData ;
|
||||
begin
|
||||
write( data( 1 ) );
|
||||
for i := 2 until 10 do writeon( data( i ) );
|
||||
end writeData ;
|
||||
|
||||
% initialise data to unsorted values %
|
||||
integer dPos;
|
||||
dPos := 1;
|
||||
for i := 16, 2, -6, 9, 90, 14, 0, 23, 8, 9
|
||||
do begin
|
||||
data( dPos ) := i;
|
||||
dPos := dPos + 1;
|
||||
end for_i ;
|
||||
|
||||
i_w := 3; s_w := 1; % set output format %
|
||||
writeData;
|
||||
bubbleSortIntegers( data, 1, 10 );
|
||||
writeData;
|
||||
end test
|
||||
end.
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
defmodule Sort do
|
||||
def bubble_sort(list) when length(list)<=1, do: list
|
||||
def bubble_sort(list) when is_list(list), do: bubble_sort(list, [])
|
||||
|
||||
def bubble_sort([x], sorted), do: [x | sorted]
|
||||
def bubble_sort(list, sorted) do
|
||||
{rest, [max]} = Enum.split(bubble_move(list), -1)
|
||||
bubble_sort(rest, [max | sorted])
|
||||
end
|
||||
|
||||
def bubble_move([x]), do: [x]
|
||||
def bubble_move([x, y | t]) when x > y, do: [y | bubble_move([x | t])]
|
||||
def bubble_move([x, y | t]) , do: [x | bubble_move([y | t])]
|
||||
|
||||
end
|
||||
|
||||
IO.inspect Sort.bubble_sort([3,2,1,4,5,2])
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
function bubblesort{T}(a::AbstractArray{T,1})
|
||||
b = copy(a)
|
||||
isordered = false
|
||||
span = length(b)
|
||||
while !isordered && span > 1
|
||||
isordered = true
|
||||
for i in 2:span
|
||||
if b[i] < b[i-1]
|
||||
t = b[i]
|
||||
b[i] = b[i-1]
|
||||
b[i-1] = t
|
||||
isordered = false
|
||||
end
|
||||
end
|
||||
span -= 1
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
a = [rand(-100:100) for i in 1:20]
|
||||
println("Before bubblesort:")
|
||||
println(a)
|
||||
a = bubblesort(a)
|
||||
println("\nAfter bubblesort:")
|
||||
println(a)
|
||||
|
|
@ -1,48 +1,39 @@
|
|||
/*REXX program sorts an array using the bubble-sort algorithm. */
|
||||
call gen@ /*generate the array elements. */
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call bubbleSort # /*invoke the bubble sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────BUBBLESORT subroutine───────────────*/
|
||||
bubbleSort: procedure expose @.; parse arg n /*N: number of items.*/
|
||||
/*diminish # items each time. */
|
||||
do until done /*sort until it's done. */
|
||||
done=1 /*assume it's done (1 ≡ true). */
|
||||
do j=1 for n-1 /*sort M items this time around. */
|
||||
k=j+1 /*point to the next item. */
|
||||
if @.j>@.k then do /*is it out of order? */
|
||||
_=@.j /*assign to a temp variable. */
|
||||
@.j=@.k /*swap current item with next ···*/
|
||||
@.k=_ /* ··· and the next with _ */
|
||||
done=0 /*indicate it's not done, whereas*/
|
||||
end /* [↑] 1≡true 0≡false */
|
||||
/*REXX program sorts an array (of any items) using the bubble-sort algorithm.*/
|
||||
call gen /*generate the array elements (items).*/
|
||||
call show 'before sort' /*show the before array elements. */
|
||||
say copies('─',79) /*show a separator line (before/after).*/
|
||||
call bubbleSort # /*invoke the bubble sort with # items.*/
|
||||
call show ' after sort' /*show the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
bubbleSort: procedure expose @.; parse arg n /*N: number of array elements.*/
|
||||
m=n-1 /*use this as a handy variable for sort*/
|
||||
do until done; done=1 /*keep sorting the array until done. */
|
||||
do j=1 for m; k=j+1 /*search for an element out─of─order. */
|
||||
if @.j>@.k then do; _=@.j /*Out of order? Then swap two elements*/
|
||||
@.j=@.k /*swap current element with the next···*/
|
||||
@.k=_ /* ··· and the next with _ */
|
||||
done=0 /*indicate that the sorting isn't done,*/
|
||||
end /* (1≡true, 0≡false). */
|
||||
end /*j*/
|
||||
end /*until done*/
|
||||
return
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @.= /*assign default value to all @. */
|
||||
@.1 = '---letters of the Hebrew alphabet---' ; @.13 = 'kaph [kaf]'
|
||||
@.2 = '====================================' ; @.14 = 'lamed'
|
||||
@.3 = 'aleph [alef]' ; @.15 = 'mem'
|
||||
@.4 = 'beth [bet]' ; @.16 = 'nun'
|
||||
@.5 = 'gimel' ; @.17 = 'samekh'
|
||||
@.6 = 'daleth [dalet]' ; @.18 = 'ayin'
|
||||
@.7 = 'he' ; @.19 = 'pe'
|
||||
@.8 = 'waw [vav]' ; @.20 = 'sadhe [tsadi]'
|
||||
@.9 = 'zayin' ; @.21 = 'qoph [qof]'
|
||||
@.10 = 'heth [het]' ; @.22 = 'resh'
|
||||
@.11 = 'teth [tet]' ; @.23 = 'shin'
|
||||
@.12 = 'yod' ; @.24 = 'taw [tav]'
|
||||
|
||||
do #=1 while @.# \=='' /*find how many entries in list. */
|
||||
end /*#*/
|
||||
#=#-1 /*adjust because of DO increment.*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: widthH=length(#) /*maximum width of any line. */
|
||||
do j=1 for #
|
||||
say 'element' right(j,widthH) arg(1)':' @.j
|
||||
end /*j*/
|
||||
say copies('─',80) /*show a separator line. */
|
||||
end /*until ··· */
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: @. = /*assign a default value to all of @. */
|
||||
@.1 = '---letters of the Hebrew alphabet---' ; @.13 = 'kaph [kaf]'
|
||||
@.2 = '====================================' ; @.14 = 'lamed'
|
||||
@.3 = 'aleph [alef]' ; @.15 = 'mem'
|
||||
@.4 = 'beth [bet]' ; @.16 = 'nun'
|
||||
@.5 = 'gimel' ; @.17 = 'samekh'
|
||||
@.6 = 'daleth [dalet]' ; @.18 = 'ayin'
|
||||
@.7 = 'he' ; @.19 = 'pe'
|
||||
@.8 = 'waw [vav]' ; @.20 = 'sadhe [tsadi]'
|
||||
@.9 = 'zayin' ; @.21 = 'qoph [qof]'
|
||||
@.10 = 'heth [het]' ; @.22 = 'resh'
|
||||
@.11 = 'teth [tet]' ; @.23 = 'shin'
|
||||
@.12 = 'yod' ; @.24 = 'taw [tav]'
|
||||
do #=1 while @.#\==''; end; #=#-1 /*find how many elements in list.*/
|
||||
w=length(#) /*the maximum width of any index.*/
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do j=1 for #; say 'element' right(j,w) arg(1)":" @.j; end; return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
fn bubble_sort<T: Ord>(values: &mut[T]) {
|
||||
let mut n = values.len();
|
||||
let mut swapped = true;
|
||||
|
||||
while swapped {
|
||||
swapped = false;
|
||||
|
||||
for i in 1..n {
|
||||
if values[i - 1] > values[i] {
|
||||
values.swap(i - 1, i);
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
n = n - 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Sort numbers.
|
||||
let mut numbers = [8, 7, 1, 2, 9, 3, 4, 5, 0, 6];
|
||||
println!("Before: {:?}", numbers);
|
||||
|
||||
bubble_sort(&mut numbers);
|
||||
println!("After: {:?}", numbers);
|
||||
|
||||
// Sort strings.
|
||||
let mut strings = ["empty", "beach", "art", "car", "deal"];
|
||||
println!("Before: {:?}", strings);
|
||||
|
||||
bubble_sort(&mut strings);
|
||||
println!("After: {:?}", strings);
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
import scala.annotation.tailrec
|
||||
|
||||
def bubbleSort(xt: List[Int]) = {
|
||||
@tailrec
|
||||
def bubble(xs: List[Int], rest: List[Int], sorted: List[Int]): List[Int] = xs match {
|
||||
case x :: Nil =>
|
||||
if (rest.isEmpty) x :: sorted
|
||||
else bubble(rest, Nil, x :: sorted)
|
||||
case a :: b :: xs =>
|
||||
if (a > b) bubble(a :: xs, b :: rest, sorted)
|
||||
else bubble(b :: xs, a :: rest, sorted)
|
||||
}
|
||||
bubble(xt, Nil, Nil)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue