task_url
stringlengths
30
116
task_name
stringlengths
2
86
task_description
stringlengths
0
14.4k
language_url
stringlengths
2
53
language_name
stringlengths
1
52
code
stringlengths
0
61.9k
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Pascal
Pascal
sub selection_sort {my @a = @_; foreach my $i (0 .. $#a - 1) {my $min = $i + 1; $a[$_] < $a[$min] and $min = $_ foreach $min .. $#a; $a[$i] > $a[$min] and @a[$i, $min] = @a[$min, $i];} return @a;}
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Prolog
Prolog
%____________________________________________________________________ % Implements the American soundex algorithm % as described at https://en.wikipedia.org/wiki/Soundex % In SWI Prolog, a 'string' is specified in 'single quotes', % while a "list of codes" may be specified in "double quotes". % So, "abc" is equi...
http://rosettacode.org/wiki/Sorting_algorithms/Shell_sort
Sorting algorithms/Shell sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#zkl
zkl
// Shell sort a sequence of objects in place // Requires mutiable list fcn shellSort(sequence){ n := sequence.len(); gap := n / 2; while (0 < gap){ i := gap; while (i < n){ j := i - gap; while ((j >= 0) and (sequence[j] > sequence[j + gap])){ sequence.swap(j,j + gap); j -= gap...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#PicoLisp
PicoLisp
(push 'Stack 3) (push 'Stack 2) (push 'Stack 1)
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#Sidef
Sidef
func spiral(n) { var (x, y, dx, dy, a) = (0, 0, 1, 0, []) { |i| a[y][x] = i var (nx, ny) = (x+dx, y+dy) ( if (dx == 1 && (nx == n || a[ny][nx]!=nil)) { [ 0, 1] } elsif (dy == 1 && (ny == n || a[ny][nx]!=nil)) { [-1, 0] } elsif (dx == -1 && (nx < 0 || a[ny][nx]!=nil)...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#ERRE
ERRE
  PROGRAM QUICKSORT_DEMO   DIM ARRAY[21]   !$DYNAMIC DIM QSTACK[0]   !$INCLUDE="PC.LIB"   PROCEDURE QSORT(ARRAY[],START,NUM) FIRST=START  ! initialize work variables LAST=START+NUM-1 LOOP REPEAT TEMP=ARRAY[(LAST+FIRST) DIV 2]  ! seek midpoint I=FIRST J=LAST REPEAT  ! re...
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#ERRE
ERRE
  PROGRAM INSERTION_SORT   DIM A[9]   PROCEDURE INSERTION_SORT(A[]) LOCAL I,J FOR I=0 TO UBOUND(A,1) DO V=A[I] J=I-1 WHILE J>=0 DO IF A[J]>V THEN A[J+1]=A[J] J=J-1 ELSE EXIT END IF END WHILE A[J+1]=V E...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#F.23
F#
let inline swap (a: _ []) i j = let temp = a.[i] a.[i] <- a.[j] a.[j] <- temp   let inline sift cmp (a: _ []) start count = let rec loop root child = if root * 2 + 1 < count then let p = child < count - 1 && cmp a.[child] a.[child + 1] < 0 let child = if p then child + 1 else child if cmp ...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Delphi
Delphi
def merge(var xs :List, var ys :List) { var result := [] while (xs =~ [x] + xr && ys =~ [y] + yr) { if (x <= y) { result with= x xs := xr } else { result with= y ys := yr } } return result + xs + ys }   def sort(list :List) { if...
http://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort
Sorting algorithms/Pancake sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Tailspin
Tailspin
  templates pancakeSort @: {stack: $, flips: 0"1"}; sink flip when <2..> do @pancakeSort.stack(1..$): $@pancakeSort.stack($..1:-1)...; '$@pancakeSort.stack;$#10;' -> !OUT::write @pancakeSort.flips: $@pancakeSort.flips + 1; end flip sink fixTop @: 1; 2..$ -> # $ -> \(when <~=$@f...
http://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort
Sorting algorithms/Pancake sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Tcl
Tcl
package require Tcl 8.5 # Some simple helper procedures proc flip {nlist n} { concat [lreverse [lrange $nlist 0 $n]] [lrange $nlist $n+1 end] } proc findmax {nlist limit} { lsearch -exact $nlist [tcl::mathfunc::max {*}[lrange $nlist 0 $limit]] }   # Simple-minded pancake sort algorithm proc pancakeSort {nlist {...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Perl
Perl
sub selection_sort {my @a = @_; foreach my $i (0 .. $#a - 1) {my $min = $i + 1; $a[$_] < $a[$min] and $min = $_ foreach $min .. $#a; $a[$i] > $a[$min] and @a[$i, $min] = @a[$min, $i];} return @a;}
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#PureBasic
PureBasic
Procedure.s getCode(c.s) Protected getCode.s = ""   If FindString("BFPV", c ,1)  : getCode = "1" : EndIf If FindString("CGJKQSXZ", c ,1) : getCode = "2" : EndIf If FindString("DT", c ,1)  : getCode = "3" : EndIf If "L" = c  : getCode = "4" : EndIf If FindString("MN"...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#Pike
Pike
  object s = ADT.Stack(); s->push("a"); s->push("b"); write("top: %O, pop1: %O, pop2: %O\n", s->top(), s->pop(), s->pop()); s->reset(); // Empty the stack  
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#Stata
Stata
function spiral_mat(n) { a = J(n*n, 1, 1) u = J(n, 1, -n) v = J(n, 1, 1) for (k=(i=n)-1; k>=1; i=i+2*k--) { j = 1..k a[j:+i] = u[j] = -u[j] a[j:+(i+k)] = v[j] = -v[j] } return(rowshape(invorder(runningsum(a)),n):-1) }   spiral_mat(5) 1 2 3 4 5 +--------------------------+ 1 | 0 ...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#F.23
F#
  let rec qsort = function hd :: tl -> let less, greater = List.partition ((>=) hd) tl List.concat [qsort less; [hd]; qsort greater] | _ -> []  
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Euphoria
Euphoria
function insertion_sort(sequence s) object temp integer j for i = 2 to length(s) do temp = s[i] j = i-1 while j >= 1 and compare(s[j],temp) > 0 do s[j+1] = s[j] j -= 1 end while s[j+1] = temp end for return s end function   include misc...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Forth
Forth
create example 70 , 61 , 63 , 37 , 63 , 25 , 46 , 92 , 38 , 87 ,   [UNDEFINED] r'@ [IF] : r'@ r> r> r@ swap >r swap >r ; [THEN]   defer precedes ( n1 n2 a -- f) defer exchange ( n1 n2 a --)   : siftDown ( a e s -- a e s) swap >r swap >r dup...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#E
E
def merge(var xs :List, var ys :List) { var result := [] while (xs =~ [x] + xr && ys =~ [y] + yr) { if (x <= y) { result with= x xs := xr } else { result with= y ys := yr } } return result + xs + ys }   def sort(list :List) { if...
http://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort
Sorting algorithms/Pancake sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Transd
Transd
  #lang transd   MainModule: { vint: [ 9, 0, 5, 10, 3, -3, -1, 8, -7, -4, -2, -6, 2, 4, 6, -10, 7, -8, -5, 1, -9],   _start: (λ (with n (- (size vint) 1) m 0 (textout vint "\n") (while n (= m (max-element-idx vint Range(0 (+ n 1)))) (if (neq m n) (if m (re...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Phix
Phix
with javascript_semantics function selection_sort(sequence s) for i=1 to length(s) do integer m = i object si = s[i], sm = s[m] for j=i+1 to length(s) do object sj = s[j] if sj<sm then {sm,m} = {sj,j} end if end for ...
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Python
Python
from itertools import groupby   def soundex(word): codes = ("bfpv","cgjkqsxz", "dt", "l", "mn", "r") soundDict = dict((ch, str(ix+1)) for ix,cod in enumerate(codes) for ch in cod) cmap2 = lambda kar: soundDict.get(kar, '9') sdx = ''.join(cmap2(kar) for kar in word.lower()) sdx2 = word[0].upper() + ''.jo...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#PL.2FI
PL/I
/* Any controlled variable may behave as a stack. */   declare s float controlled;   /* to push a value on the stack. */ allocate s; s = 10;   /* To pop a value from the stack. */ put (s); free s;   /* to peek at the top of stack> */ put (s);   /* To see whether the stack is empty */ if allocation(s) = 0 then ...   /* ...
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#Tcl
Tcl
package require Tcl 8.5 namespace path {::tcl::mathop} proc spiral size { set m [lrepeat $size [lrepeat $size .]] set x 0; set dx 0 set y -1; set dy 1 set i -1 while {$i < $size ** 2 - 1} { if {$dy == 0} { incr x $dx if {0 <= $x && $x < $size && [lindex $m $x $y] eq "...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Factor
Factor
: qsort ( seq -- seq ) dup empty? [ unclip [ [ < ] curry partition [ qsort ] bi@ ] keep prefix append ] unless ;
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#F.23
F#
  // This function performs an insertion sort with an array. // The input parameter is a generic array (any type that can perform comparison). // As is typical of functional programming style the input array is not modified; // a copy of the input array is made and modified and returned. let insertionSort (A: _ array) ...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Fortran
Fortran
program Heapsort_Demo implicit none   integer, parameter :: num = 20 real :: array(num)   call random_seed call random_number(array) write(*,*) "Unsorted array:-" write(*,*) array write(*,*) call heapsort(array) write(*,*) "Sorted array:-" write(*,*) array   contains   subroutine heapsort(a)   ...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#EasyLang
EasyLang
subr merge mid = left + sz if mid > sz_data mid = sz_data . right = mid + sz if right > sz_data right = sz_data . l = left r = mid for i = left to right - 1 if r = right or l < mid and tmp[l] < tmp[r] data[i] = tmp[l] l += 1 else data[i] = tmp[r] r += 1 . ...
http://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort
Sorting algorithms/Pancake sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#uBasic.2F4tH
uBasic/4tH
PRINT "Pancake sort:" n = FUNC (_InitArray) PROC _ShowArray (n) PROC _Pancakesort (n) PROC _ShowArray (n) PRINT   END     _Flip PARAM(1) LOCAL(1)   b@ = 0   DO WHILE b@ < a@ PROC _Swap (b@, a@) b@ = b@ + 1 a@ = a@ - 1 LOOP RETURN     _Pancakesort PARAM (1) ' Pancakesort LOC...
http://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort
Sorting algorithms/Pancake sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#UNIX_Shell
UNIX Shell
#!/usr/bin/env bash main() { local stack local -i n m i if (( $# )); then stack=("$@") else stack=($(printf '%s\n' {0..9} | shuf)) fi print_stack 0 "${stack[@]}"   # start by looking at whole stack (( n = ${#stack[@]} ))   # keep going until we're all sorted while (( n > 0 )); do   # shr...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PHP
PHP
function selection_sort(&$arr) { $n = count($arr); for($i = 0; $i < count($arr); $i++) { $min = $i; for($j = $i + 1; $j < $n; $j++){ if($arr[$j] < $arr[$min]){ $min = $j; } } list($arr[$i],$arr[$min]) = array($arr[$min],$arr[$i]); } }
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Racket
Racket
sub soundex ($name --> Str) { my $first = substr($name,0,1).uc; gather { take $first; my $fakefirst = ''; $fakefirst = "de " if $first ~~ /^ <[AEIOUWH]> /; "$fakefirst$name".lc.trans('wh' => '') ~~ / ^ [ [ | <[ bfpv ]>+ ...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#PostScript
PostScript
% empty? is already defined. /push {exch cons}. /pop {uncons exch pop}. [2 3 4 5 6] 1 push = [1 2 3 4 5 6] [1 2 3 4 5 6] pop =[2 3 4 5 6] [2 3 4 5 6] empty? =false [] empty? =true
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#TI-83_BASIC
TI-83 BASIC
5->N DelVar [F] {N,N}→dim([F]) 1→A: N→B 1→C: N→D 0→E: E→G 1→I: 1→J For(K,1,N*N) K-1→[F](I,J) If E=0: Then If J<D: Then J+1→J Else: 1→G I+1→I: A+1→A End End If E=1: Then If I<B: Then I+1→I Else: 2→G J-1→J: D-1→D End End If E=2: Then If J>C: Then J-1→J Else: 3→G I-1→I: B-1→B End End If E=3: Then If I>A: Then I-1→I E...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Fexl
Fexl
# (sort xs) is the ordered list of all elements in list xs. # This version preserves duplicates. \sort== (\xs xs [] \x\xs append (sort; filter (gt x) xs); # all the items less than x cons x; append (filter (eq x) xs); # all the items equal to x sort; filter (lt x) xs # all the items g...
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Factor
Factor
USING: kernel prettyprint sorting.extras sequences ;   : insertion-sort ( seq -- sorted-seq ) <reversed> V{ } clone [ swap insort-left! ] reduce ;   { 6 8 5 9 3 2 1 4 7 } insertion-sort .
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#FreeBASIC
FreeBASIC
' version 22-10-2016 ' compile with: fbc -s console ' for boundary checks on array's compile with: fbc -s console -exx   ' sort from lower bound to the higher bound ' array's can have subscript range from -2147483648 to +2147483647   Sub siftdown(hs() As Long, start As ULong, end_ As ULong) Dim As ULong root = star...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Eiffel
Eiffel
  class MERGE_SORT [G -> COMPARABLE]   create sort   feature   sort (ar: ARRAY [G]) -- Sorted array in ascending order. require ar_not_empty: not ar.is_empty do create sorted_array.make_empty mergesort (ar, 1, ar.count) sorted_array := ar ensure sorted_array_not_empty: not sorted_array.is_emp...
http://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort
Sorting algorithms/Pancake sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#VBA
VBA
    'pancake sort 'uses two auxiliary routines "printarray" and "flip" Public Sub printarray(A) For i = LBound(A) To UBound(A) Debug.Print A(i), Next Debug.Print End Sub   Public Sub Flip(ByRef A, p1, p2, trace) 'flip first elements of A (p1 to p2) If trace Then Debug.Print "we'll flip the first "; p2 - p1 ...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PicoLisp
PicoLisp
(de selectionSort (Lst) (map '((L) (and (cdr L) (xchg L (member (apply min @) L)))) Lst ) Lst )
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PL.2FI
PL/I
  Selection: procedure options (main); /* 2 November 2013 */   declare a(10) fixed binary initial ( 5, 7, 3, 98, 4, -3, 25, 20, 60, 17);   put edit (trim(a)) (a, x(1));   call Selection_Sort (a);   put skip edit (trim(a)) (a, x(1));   Selection_sort: procedure (a); declare a(*) fixed binary...
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Raku
Raku
sub soundex ($name --> Str) { my $first = substr($name,0,1).uc; gather { take $first; my $fakefirst = ''; $fakefirst = "de " if $first ~~ /^ <[AEIOUWH]> /; "$fakefirst$name".lc.trans('wh' => '') ~~ / ^ [ [ | <[ bfpv ]>+ ...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#PowerShell
PowerShell
  $stack = New-Object -TypeName System.Collections.Stack # or $stack = [System.Collections.Stack] @()  
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#TSE_SAL
TSE SAL
    // library: math: create: array: spiral: inwards <description></description> <version control></version control> <version>1.0.0.0.15</version> (filenamemacro=creamasi.s) [<Program>] [<Research>] [kn, ri, mo, 31-12-2012 01:15:43] PROC PROCMathCreateArraySpiralInwards( INTEGER nI ) // e.g. PROC Main() // e.g. STRIN...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Forth
Forth
: mid ( l r -- mid ) over - 2/ -cell and + ;   : exch ( addr1 addr2 -- ) dup @ >r over @ swap ! r> swap ! ;   : partition ( l r -- l r r2 l2 ) 2dup mid @ >r ( r: pivot ) 2dup begin swap begin dup @ r@ < while cell+ repeat swap begin r@ over @ < while cell- repeat 2dup <= if 2dup exch >r cell+ r> cell- ...
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Forth
Forth
: insert ( start end -- start ) dup @ >r ( r: v ) \ v = a[i] begin 2dup < \ j>0 while r@ over cell- @ < \ a[j-1] > v while cell- \ j-- dup @ over cell+ ! \ a[j] = a[j-1] repeat then r> swap ! ; \ a[j] = v   : sort ( array len -- ) 1 ?do dup i cells + insert loop drop ;   create test...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#FunL
FunL
def heapSort( a ) = heapify( a ) end = a.length() - 1   while end > 0 a(end), a(0) = a(0), a(end) siftDown( a, 0, --end )   def heapify( a ) = for i <- (a.length() - 2)\2..0 by -1 siftDown( a, i, a.length() - 1 )   def siftDown( a, start, end ) = root = start   while root*2 + 1 <= end child ...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Go
Go
package main   import ( "sort" "container/heap" "fmt" )   type HeapHelper struct { container sort.Interface length int }   func (self HeapHelper) Len() int { return self.length } // We want a max-heap, hence reverse the comparison func (self HeapHelper) Less(i, j int) bool { return self.container.Less(...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Elixir
Elixir
defmodule Sort do def merge_sort(list) when length(list) <= 1, do: list def merge_sort(list) do {left, right} = Enum.split(list, div(length(list), 2))  :lists.merge( merge_sort(left), merge_sort(right)) end end
http://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort
Sorting algorithms/Pancake sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Wren
Wren
import "/sort" for Find   class Pancake { construct new(a) { _a = a.toList }   flip(r) { for (l in 0...r) { _a.swap(r, l) r = r - 1 } }   sort() { for (uns in _a.count-1..1) { var h = Find.highest(_a[0..uns]) var lx = h[...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PowerShell
PowerShell
Function SelectionSort( [Array] $data ) { $datal=$data.length-1 0..( $datal - 1 ) | ForEach-Object { $min = $data[ $_ ] $mini = $_ ( $_ + 1 )..$datal | ForEach-Object { if( $data[ $_ ] -lt $min ) { $min = $data[ $_ ] $mini = $_ } } $temp = $data[ $_ ] $data[ $_ ] = $min $data[ $mini ] = $t...
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#REXX
REXX
/*REXX program demonstrates Soundex codes from some words or from the command line.*/ _=; @.= /*set a couple of vars to "null".*/ parse arg @.0 . /*allow input from command line. */ @.1 = "12346"  ; ...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#Prolog
Prolog
% push( ELEMENT, STACK, NEW ) % True if NEW is [ELEMENT|STACK] push(ELEMENT,STACK,[ELEMENT|STACK]).   % pop( STACK, TOP, NEW ) % True if TOP and NEW are head and tail, respectively, of STACK pop([TOP|STACK],TOP,STACK).   % empty( STACK ) % True if STACK is empty empty([]).
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#uBasic.2F4tH
uBasic/4tH
Input "Width: ";w Input "Height: ";h Print   For i = 0 To h-1 For j = 0 To w-1 Print Using "__#"; FUNC(_Spiral(w,h,j,i)); Next Print Next End     _Spiral Param(4) If d@ Then Return (a@ + FUNC(_Spiral(b@-1, a@, d@ - 1, a@ - c@ - 1))) Else Return (c@) EndIf
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Fortran
Fortran
MODULE qsort_mod   IMPLICIT NONE   TYPE group INTEGER :: order ! original order of unsorted data REAL :: VALUE ! values to be sorted by END TYPE group   CONTAINS   RECURSIVE SUBROUTINE QSort(a,na)   ! DUMMY ARGUMENTS INTEGER, INTENT(in) :: nA TYPE (group), DIMENSION(nA), INTENT(in...
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Fortran
Fortran
subroutine sort(n, a) implicit none integer :: n, i, j real :: a(n), x   do i = 2, n x = a(i) j = i - 1 do while (j >= 1) if (a(j) <= x) exit a(j + 1) = a(j) j = j - 1 end do a(j + 1) = x end do end subroutine
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Groovy
Groovy
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] }   def checkSwap = { list, i, j = i+1 -> [(list[i] > list[j])].find{ it }.each { makeSwap(list, i, j) } }   def siftDown = { a, start, end -> def p = start while (p*2 < end) { def c = p*2 + ((p*2 + 1 < end && a[p*2 + 2] > a[p*2 + 1]) ? ...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Erlang
Erlang
mergeSort(L) when length(L) == 1 -> L; mergeSort(L) when length(L) > 1 -> {L1, L2} = lists:split(length(L) div 2, L), lists:merge(mergeSort(L1), mergeSort(L2)).
http://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort
Sorting algorithms/Pancake sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#zkl
zkl
fcn pancakeSort(a){ foreach i in ([a.len()-1..1,-1]){ j := a.index((0).max(a[0,i+1])); // min for decending sort if(i != j){ a.swap(0,j); a.swap(0,i); } } a }
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Prolog
Prolog
  selection_sort([], []). selection_sort([H | L], [H1 | L2]) :- exchange(H, L, H1, L1), selection_sort(L1, L2).     exchange(H, [], H, []).   exchange(H, L, H1, L1) :- min_list(L, H2), ( H < H2 -> H1 = H, L1 = L ; H1 = H2, % does the exchange of the number H % and the min of the list nth0(Ind...
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Ring
Ring
  # Project: Soundex   name = ["Ashcraf", "Ashcroft", "Gauss", "Ghosh", "Hilbert", "Heilbronn", "Lee", "Lloyd", "Moses", "Pfister", "Robert", "Rupert", "Rubin","Tymczak", "Soundex", "Example"] for i = 1 to 16 sp = 10 - len(name[i]) see '"' + name[i] + '"' + copy(" ", sp) + " " + soundex(name[i])...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#PureBasic
PureBasic
Global NewList MyStack()   Procedure Push_LIFO(n) FirstElement(MyStack()) InsertElement(MyStack()) MyStack() = n EndProcedure   Procedure Pop_LIFO() If FirstElement(MyStack()) Topmost = MyStack() DeleteElement(MyStack()) EndIf ProcedureReturn Topmost EndProcedure   Procedure Empty_LIFO() Protected...
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#Ursala
Ursala
#import std #import nat #import int   spiral =   ^H/block nleq-<lS&r+ -+ num@NiC+ sum:-0*yK33x+ (|\LL negation**)+ rlc ~&lh==1, ~&rNNXNXSPlrDlSPK32^lrtxiiNCCSLhiC5D/~& iota*+ iota+-   #cast %nLLL   examples = spiral* <5,6,7>
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#FreeBASIC
FreeBASIC
' version 23-10-2016 ' compile with: fbc -s console   ' sort from lower bound to the highter bound ' array's can have subscript range from -2147483648 to +2147483647   Sub quicksort(qs() As Long, l As Long, r As Long)   Dim As ULong size = r - l +1 If size < 2 Then Exit Sub   Dim As Long i = l, j = r Di...
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#FreeBASIC
FreeBASIC
' version 20-10-2016 ' compile with: fbc -s console ' for boundry checks on array's compile with: fbc -s console -exx   Sub insertionSort( arr() As Long )   ' sort from lower bound to the highter bound ' array's can have subscript range from -2147483648 to +2147483647   Dim As Long lb = LBound(arr) Dim As Long ...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Haskell
Haskell
import Data.Graph.Inductive.Internal.Heap( Heap(..),insert,findMin,deleteMin)   -- heapsort is added in this module as an example application   build :: Ord a => [(a,b)] -> Heap a b build = foldr insert Empty   toList :: Ord a => Heap a b -> [(a,b)] toList Empty = [] toList h = x:toList r where (x,r) = (fi...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#ERRE
ERRE
  PROGRAM MERGESORT_DEMO   ! Example of merge sort usage.   CONST SIZE%=100,S1%=50   DIM DTA%[SIZE%],FH%[S1%],STACK%[20,2]     PROCEDURE MERGE(START%,MIDDLE%,ENDS%)   LOCAL FHSIZE%   FHSIZE%=MIDDLE%-START%+1   FOR I%=0 TO FHSIZE%-1 DO FH%[I%]=DTA%[START%+I%] END FOR   I%=0 J%=MIDDLE%+1 K%=START%   RE...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PureBasic
PureBasic
Procedure selectionSort(Array a(1)) Protected i, j, lastIndex, minIndex   lastIndex = ArraySize(a()) For i = 0 To lastIndex - 1 minIndex = i For j = i + 1 To lastIndex If a(minIndex) > a(j) minIndex = j EndIf Next Swap a(minIndex), a(i) Next EndProcedure
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Ruby
Ruby
class String   SoundexChars = 'BFPVCGJKQSXZDTLMNR' SoundexNums = '111122222222334556' SoundexCharsEx = '^' + SoundexChars SoundexCharsDel = '^A-Z'   # desc: http://en.wikipedia.org/wiki/Soundex def soundex(census = true) str = self.upcase.delete(SoundexCharsDel) str[0,1] + str[1..-1].delete(Soundex...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#Python
Python
from collections import deque stack = deque() stack.append(value) # pushing value = stack.pop() not stack # is empty?
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#VBScript
VBScript
  Function build_spiral(n) botcol = 0 : topcol = n - 1 botrow = 0 : toprow = n - 1 'declare a two dimensional array Dim matrix() ReDim matrix(topcol,toprow) dir = 0 : col = 0 : row = 0 'populate the array For i = 0 To n*n-1 matrix(col,row) = i Select Case dir Case 0 If col < topcol Then col = co...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#FunL
FunL
def qsort( [] ) = [] qsort( p:xs ) = qsort( xs.filter((< p)) ) + [p] + qsort( xs.filter((>= p)) )
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#GAP
GAP
InsertionSort := function(L) local n, i, j, x; n := Length(L); for i in [ 2 .. n ] do x := L[i]; j := i - 1; while j >= 1 and L[j] > x do L[j + 1] := L[j]; j := j - 1; od; L[j + 1] := x; od; end;   s := "BFKRIMPOQACNESWUTXDGLVZHYJ"; InsertionSort(s); s; # "ABCDEFGHIJKLMNOPQRSTUV...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Haxe
Haxe
class HeapSort { @:generic private static function siftDown<T>(arr: Array<T>, start:Int, end:Int) { var root = start; while (root * 2 + 1 <= end) { var child = root * 2 + 1; if (child + 1 <= end && Reflect.compare(arr[child], arr[child + 1]) < 0) child++; if (Reflect.compare(arr[ro...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Euphoria
Euphoria
function merge(sequence left, sequence right) sequence result result = {} while length(left) > 0 and length(right) > 0 do if compare(left[1], right[1]) <= 0 then result = append(result, left[1]) left = left[2..$] else result = append(result, right[1]) ...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Python
Python
def selection_sort(lst): for i, e in enumerate(lst): mn = min(range(i,len(lst)), key=lst.__getitem__) lst[i], lst[mn] = lst[mn], e return lst
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Qi
Qi
(define select-r Small [] Output -> [Small | (selection-sort Output)] Small [X|Xs] Output -> (select-r X Xs [Small|Output]) where (< X Small) Small [X|Xs] Output -> (select-r Small Xs [X|Output]))   (define selection-sort [] -> [] [First|Lst] -> (select-r First Lst []))   (selection-sort [8 7 4 3...
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Run_BASIC
Run BASIC
global val$ val$(1) = "BPFV" val$(2) = "CSGJKQXZ" val$(3) = "DT" val$(4) = "L" val$(5) = "MN" val$(6) = "R"   ' --------------------------------- ' show soundex on these words ' --------------------------------- w$(1) = "Robert" 'R163 w$(2) = "Rupert" 'R163 w$(3) = "Rubin" 'R150 w$(4) = "moses" 'M...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#Quackery
Quackery
[ size 1 = ] is isempty ( s --> b )   [ stack ] is mystack ( --> s )   mystack isempty if [ say "mystack is empty" cr cr ] 23 mystack put mystack share echo say " is on the top of mystack" cr cr   mystack mystack put ( you can put anything on an ancillary stack, even itself! )   mystack share echo say " is on ...
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#Visual_Basic
Visual Basic
Option Explicit   Sub Main() print2dArray getSpiralArray(5) End Sub   Function getSpiralArray(dimension As Integer) As Integer() ReDim spiralArray(dimension - 1, dimension - 1) As Integer   Dim numConcentricSquares As Integer numConcentricSquares = dimension \ 2 If (dimension Mod 2) Then numConcentr...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Go
Go
package main   import "fmt"   func main() { list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84} fmt.Println("unsorted:", list)   quicksort(list) fmt.Println("sorted! ", list) }   func quicksort(a []int) { var pex func(int, int) pex = func(lower, upper int) { for { switch u...
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Go
Go
package main   import "fmt"   func insertionSort(a []int) { for i := 1; i < len(a); i++ { value := a[i] j := i - 1 for j >= 0 && a[j] > value { a[j+1] = a[j] j = j - 1 } a[j+1] = value } }   func main() { list := []int{31, 41, 59, 26, 53, 58, 9...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Icon_and_Unicon
Icon and Unicon
procedure main() #: demonstrate various ways to sort a list and string demosort(heapsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end   procedure heapsort(X,op) #: return sorted list ascending(or descending) local head,tail   op := sortop(op,X) ...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#F.23
F#
let split list = let rec aux l acc1 acc2 = match l with | [] -> (acc1,acc2) | [x] -> (x::acc1,acc2) | x::y::tail -> aux tail (x::acc1) (y::acc2) in aux list [] []   let rec merge l1 l2 = match (l1,l2) with | (x,[]) -> x | ([],y) -> ...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Quackery
Quackery
[ 0 swap behead swap witheach [ 2dup > iff [ nip nip i^ 1+ swap ] else drop ] drop ] is least ( [ --> n )   [ [] swap dup size times [ dup least pluck swap dip join ] drop ] is ssort ( [ --> [ )   [] 20 times [ 10...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#R
R
selectionsort.loop <- function(x) { lenx <- length(x) for(i in seq_along(x)) { mini <- (i - 1) + which.min(x[i:lenx]) start_ <- seq_len(i-1) x <- c(x[start_], x[mini], x[-c(start_, mini)]) } x }
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Scala
Scala
def soundex(s:String)={ var code=s.head.toUpper.toString var previous=getCode(code.head) for(ch <- s.drop(1); current=getCode(ch.toUpper)){ if (!current.isEmpty && current!=previous) code+=current previous=current } code+="0000" code.slice(0,4) }   def getCode(c:Char)={ val cod...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#R
R
library(proto)   stack <- proto(expr = { l <- list() empty <- function(.) length(.$l) == 0 push <- function(., x) { .$l <- c(list(x), .$l) print(.$l) invisible() } pop <- function(.) { if(.$empty()) stop("can't pop from an empty list") .$l[[1]] <- NULL print(.$...
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#Wren
Wren
import "/fmt" for Conv, Fmt   var n = 5 var top = 0 var left = 0 var bottom = n - 1 var right = n - 1 var sz = n * n var a = List.filled(sz, 0) var i = 0 while (left < right) { // work right, along top var c = left while (c <= right) { a[top*n+c] = i i = i + 1 c = c + 1 } top...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Haskell
Haskell
qsort [] = [] qsort (x:xs) = qsort [y | y <- xs, y < x] ++ [x] ++ qsort [y | y <- xs, y >= x]
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Groovy
Groovy
def insertionSort = { list ->   def size = list.size() (1..<size).each { i -> def value = list[i] def j = i - 1 for (; j >= 0 && list[j] > value; j--) { print "."; list[j+1] = list[j] } print "."; list[j+1] = value } list }
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#J
J
swap=: C.~ <   siftDown=: 4 : 0 'c e'=. x while. e > c=.1+2*s=.c do. before=. <&({&y) if. e > 1+c do. c=.c+ c before c+1 end. if. s before c do. y=. y swap c,s else. break. end. end. y )   heapSort=: 3 : 0 if. 1>: c=. # y do. y return. end. z=. siftDown&.>/ (c,~each i.<.c%2),<y NB. heapif...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Factor
Factor
: mergestep ( accum seq1 seq2 -- accum seq1 seq2 ) 2dup [ first ] bi@ < [ [ [ first ] [ rest-slice ] bi [ suffix ] dip ] dip ] [ [ first ] [ rest-slice ] bi [ swap [ suffix ] dip ] dip ] if ;   : merge ( seq1 seq2 -- merged ) [ { } ] 2dip [ 2dup [ length 0 > ] bi@ and ] [ mergestep ] while append append ;   : mergesort...
http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort
Sorting algorithms/Selection sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Ra
Ra
  class SelectionSort **Sort a list with the Selection Sort algorithm**   on start   args := program arguments .sort(args) print args   define sort(list) is shared **Sort the list**   test list := [4, 2, 7, 3] .sort(list) assert list = [2, 3, 4, 7]   body count := list.count last := count ...
http://rosettacode.org/wiki/Soundex
Soundex
Soundex is an algorithm for creating indices for words based on their pronunciation. Task The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). Caution There is a major issue in many of the ...
#Scheme
Scheme
;; The American Soundex System ;; ;; The soundex code consist of the first letter of the name followed ;; by three digits. These three digits are determined by dropping the ;; letters a, e, i, o, u, h, w and y and adding three digits from the ;; remaining letters of the name according to the table below. There ;; are o...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#Racket
Racket
  #lang racket (define stack '()) (define (push x stack) (cons x stack)) (define (pop stack) (values (car stack) (cdr stack))) (define (empty? stack) (null? stack))  
http://rosettacode.org/wiki/Spiral_matrix
Spiral matrix
Task Produce a spiral array. A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards. For example, given   5,   produce this array: 0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 ...
#XPL0
XPL0
def N=5; int A(N,N); int I, J, X, Y, Steps, Dir; include c:\cxpl\codes; [Clear; I:= 0; X:= -1; Y:= 0; Steps:= N; Dir:= 0; repeat for J:= 1 to Steps do [case Dir&3 of 0: X:= X+1; 1: Y:= Y+1; 2: X:= X-1; 3: Y:= Y-1 ...
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort
Sorting algorithms/Quicksort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Icon_and_Unicon
Icon and Unicon
procedure main() #: demonstrate various ways to sort a list and string demosort(quicksort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end   procedure quicksort(X,op,lower,upper) #: return sorted list local pivot,x   if /lower := 1 then { # top l...
http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
Sorting algorithms/Insertion sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Haskell
Haskell
import Data.List (insert)   insertionSort :: Ord a => [a] -> [a] insertionSort = foldr insert []   -- Example use: -- *Main> insertionSort [6,8,5,9,3,2,1,4,7] -- [1,2,3,4,5,6,7,8,9]
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Janet
Janet
  (defn swap [l a b] (let [aval (get l a) bval (get l b)] (put l a bval) (put l b aval)))   (defn heap-sort [l] (def len (length l)) # Invariant: heap[parent] <= heap[*children] (def heap (array/new (+ len 1))) (array/push heap nil) (def ROOT 1)   # Returns the parent index of index, or nil if no...