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/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 ...
#Ada
Ada
generic type Element_Type is private; type Index_Type is (<>); type Collection_Type is array(Index_Type range <>) of Element_Type; with function "<"(Left, Right : Element_Type) return Boolean is <>;   package Mergesort is function Sort(Item : Collection_Type) return Collection_Type; end MergeSort;
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 ...
#JavaScript
JavaScript
Array.prototype.pancake_sort = function () { for (var i = this.length - 1; i >= 1; i--) { // find the index of the largest element not yet sorted var max_idx = 0; var max = this[0]; for (var j = 1; j <= i; j++) { if (this[j] > max) { max = this[j]; ...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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 stoogeSort(&$arr, $i, $j) { if($arr[$j] < $arr[$i]) { list($arr[$j],$arr[$i]) = array($arr[$i], $arr[$j]); } if(($j - $i) > 1) { $t = ($j - $i + 1) / 3; stoogesort($arr, $i, $j - $t); stoogesort($arr, $i + $t, $j); stoogesort($arr, $i, $j - $t); } }  
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 ...
#GAP
GAP
SelectionSort := function(v) local i, j, k, n, m; n := Size(v); for i in [1 .. n] do k := i; m := v[i]; for j in [i + 1 .. n] do if v[j] < m then k := j; m := v[j]; fi; od; v[k] := v[i]; v[i] := m; od; end;   v := List([1 .. 100],...
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 ...
#Go
Go
package main   import "fmt"   var a = []int{170, 45, 75, -90, -802, 24, 2, 66}   func main() { fmt.Println("before:", a) selectionSort(a) fmt.Println("after: ", a) }   func selectionSort(a []int) { last := len(a) - 1 for i := 0; i < last; i++ { aMin := a[i] iMin := i for j :=...
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 ...
#J
J
removeDups =: {.;.1~ (1 , }. ~: }: ) codes =: ;: 'BFPV CGJKQSXZ DT L MN R HW'   soundex =: 3 : 0 if. 0=# k=.toupper y do. '0' return. end. ({.k), ,": ,. 3 {. 0-.~ }. removeDups 7 0:`(I.@:=)`]} , k >:@I.@:(e. &>)"0 _ codes )
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 ...
#ooRexx
ooRexx
/* Rexx */ -- --- Main -------------------------------------------------------------------- call demo return exit   -- ----------------------------------------------------------------------------- -- Shell sort implementation -- ----------------------------------------------------------------------------- ::routine sh...
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...
#Mercury
Mercury
:- module sstack.   :- interface.   % We're going to call the type sstack (simple stack) because we don't want to get it % accidentally confused with the official stack module in the standard library. :- type sstack(T).   :- func sstack.new = sstack(T). :- pred sstack.is_empty(sstack(T)::in) is semidet. :- func sstack....
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 ...
#Pascal
Pascal
program Spiralmat; type tDir = (left,down,right,up); tdxy = record dx,dy: longint; end; tdeltaDir = array[tDir] of tdxy; const Nextdir : array[tDir] of tDir = (down,right,up,left); cDir : tDeltaDir = ((dx:1;dy:0),(dx:0;dy:1),(dx:-1;dy:0),(dx:0;dy:-1)); cMaxN = 32; type tSpiral = array...
http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort
Sorting algorithms/Radix 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 ...
#REXX
REXX
/*REXX program performs a radix sort on an integer array (can be negative/zero/positive)*/ call gen /*call subroutine to generate numbers. */ call radSort n, w /*invoke the radix sort subroutine. */ call show ...
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 ...
#C.23
C#
// // The Tripartite conditional enables Bentley-McIlroy 3-way Partitioning. // This performs additional compares to isolate islands of keys equal to // the pivot value. Use unless key-equivalent classes are of small size. // #define Tripartite   namespace RosettaCode { using System; using System.Diagnostics;   ...
http://rosettacode.org/wiki/Sorting_algorithms/Patience_sort
Sorting algorithms/Patience 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 ...
#OCaml
OCaml
module PatienceSortFn (Ord : Set.OrderedType) : sig val patience_sort : Ord.t list -> Ord.t list end = struct   module PilesSet = Set.Make (struct type t = Ord.t list let compare x y = Ord.compare (List.hd x) (List.hd y) end);;   let sort_into_piles list = let piles = Array.make (Li...
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 ...
#BASIC_2
BASIC
DECLARE SUB InsertionSort (theList() AS INTEGER)   DIM n(10) AS INTEGER, L AS INTEGER, o AS STRING FOR L = 0 TO 10 n(L) = INT(RND * 32768) NEXT InsertionSort n() FOR L = 1 TO 10 PRINT n(L); ";"; NEXT   SUB InsertionSort (theList() AS INTEGER) DIM insertionElementIndex AS INTEGER FOR insertionElementInde...
http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
Sorting algorithms/Permutation 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 ...
#Ring
Ring
  # Project : Sorting algorithms/Permutation sort   a = [4, 65, 2, 31, 0, 99, 2, 83, 782] result = [] permute(a,1)   for n = 1 to len(result) num = 0 for m = 1 to len(result[n]) - 1 if result[n][m] <= result[n][m+1] num = num + 1 ok next if num = len(result[n]) ...
http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
Sorting algorithms/Permutation 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 ...
#Ruby
Ruby
class Array def permutationsort permutation.each{|perm| return perm if perm.sorted?} end   def sorted? each_cons(2).all? {|a, b| a <= b} end end
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 ...
#AppleScript
AppleScript
-- In-place binary heap sort. -- Heap sort algorithm: J.W.J. Williams. on heapSort(theList, l, r) -- Sort items l thru r of theList. set listLen to (count theList) if (listLen < 2) then return -- Convert negative and/or transposed range indices. if (l < 0) then set l to listLen + l + 1 if (r < 0) th...
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 ...
#ALGOL_68
ALGOL 68
MODE DATA = CHAR;   PROC merge sort = ([]DATA m)[]DATA: ( IF LWB m >= UPB m THEN m ELSE INT middle = ( UPB m + LWB m ) OVER 2; []DATA left = merge sort(m[:middle]); []DATA right = merge sort(m[middle+1:]); flex merge(left, right)[AT LWB m] FI );   # FLEX version: A de...
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 ...
#jq
jq
def pancakeSort:   def flip(i): . as $in | ($in[0:i+1]|reverse) + $in[i+1:] ;   # If input is [] then return null def index_of_max: . as $in | reduce range(1; length) as $i # state: [ix, max] ( [ 0, $in[0] ]; if $in[$i] > .[1] then [ $i, $in[$i] ] else . end ) | .[0] ;   ...
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 ...
#Julia
Julia
function pancakesort!(arr::Vector{<:Real}) len = length(arr) if len < 2 return arr end for i in len:-1:2 j = indmax(arr[1:i]) if i == j continue end arr[1:j] = reverse(arr[1:j]) arr[1:i] = reverse(arr[1:i]) end return arr end   v = rand(-10:10, 10) println("# unordere...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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 stoogeSort (L N) (default N (length L)) (let P (nth L N) (when (> (car L) (car P)) (xchg L P) ) ) (when (> N 2) (let D (/ N 3) (stoogeSort L (- N D)) (stoogeSort (nth L (inc D)) (- N D)) (stoogeSort L (- N D)) ) ) L )
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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
stoogesort: procedure (L) recursive; /* 16 August 2010 */ declare L(*) fixed binary; declare (i, j, t, temp) fixed binary;   j = hbound(L,1); do i = lbound(L, 1) to j; if L(j) < L(i) then do; temp = L(i); L(i) = L(j); L(j) = temp; end; if j - i > 1 then do; t = (j - i...
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 ...
#Haskell
Haskell
import Data.List (delete)   selSort :: (Ord a) => [a] -> [a] selSort [] = [] selSort xs = selSort (delete x xs) ++ [x] where x = maximum xs
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 ...
#Java
Java
public static void main(String[] args){ System.out.println(soundex("Soundex")); System.out.println(soundex("Example")); System.out.println(soundex("Sownteks")); System.out.println(soundex("Ekzampul")); }   private static String getCode(char c){ switch(c){ case 'B': case 'F': case 'P': case 'V': ...
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 ...
#PARI.2FGP
PARI/GP
shellSort(v)={ my(inc=#v\2); while(inc, for(i=inc+1,#v, my(t=v[i],j=i); while(j>inc && v[j-inc]>t, v[j]=v[j-=inc] ); v[j]=t ); inc \= 2.2 ); v };
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 ...
#Pascal
Pascal
Const MaxN = 100; { number of elements (my example is 100) } Type TArray = Array [0..MaxN] of Integer;   Procedure ShellSort ( var A : TArray; N : Integer ); Var i, j, step, tmp : Integer; Begin step:=N div 2; // step:=step shr 1 While step>0 Do Begin For i:=step to N Do Begin tmp:=A[i]; j:=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...
#MiniScript
MiniScript
// Note in Miniscript, a value of zero is false, // and any other number is true. // therefore the .len function works as the inverse of a .empty function stack = [2, 4, 6] stack.push 8 print "Stack is " + stack print "Adding '9' to stack " + stack.push(9) print "Top of stack is " + stack.pop print "Stack is " + 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 ...
#Perl
Perl
sub spiral {my ($n, $x, $y, $dx, $dy, @a) = (shift, 0, 0, 1, 0); foreach (0 .. $n**2 - 1) {$a[$y][$x] = $_; my ($nx, $ny) = ($x + $dx, $y + $dy); ($dx, $dy) = $dx == 1 && ($nx == $n || defined $a[$ny][$nx]) ? ( 0, 1) : $dy == 1 && ($ny == $n || defined $a[$ny][$nx]) ...
http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort
Sorting algorithms/Radix 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 ...
#Ruby
Ruby
class Array def radix_sort(base=10) ary = dup rounds = (Math.log(ary.minmax.map(&:abs).max)/Math.log(base)).floor + 1 rounds.times do |i| buckets = Array.new(2*base){[]} base_i = base**i ary.each do |n| digit = (n/base_i) % base digit += base if 0<=n buckets[digit...
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 ...
#C.2B.2B
C++
#include <iterator> #include <algorithm> // for std::partition #include <functional> // for std::less   // helper function for median of three template<typename T> T median(T t1, T t2, T t3) { if (t1 < t2) { if (t2 < t3) return t2; else if (t1 < t3) return t3; else return t1; } el...
http://rosettacode.org/wiki/Sorting_algorithms/Patience_sort
Sorting algorithms/Patience 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
PatienceSortTask (Output);   CONST MaxSortSize = 1024; { A power of two. } MaxWinnersSize = (2 * MaxSortSize) - 1;   TYPE PilesArrayType = ARRAY [1 .. MaxSortSize] OF INTEGER; WinnersArrayType = ARRAY [1 .. MaxWinnersSize, 1 .. 2] OF INTEGER;   VAR ExampleNumbers : ARRAY ...
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 ...
#BCPL
BCPL
get "libhdr"   let insertionSort(A, len) be for i = 1 to len-1 do $( let value = A!i let j = i-1 while j >= 0 & A!j > value do $( A!(j+1) := A!j j := j-1 $) A!(j+1) := value $)   let write(s, A, len) be $( writes(s) for i=0 to len-1 do writed(A!i, 4...
http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
Sorting algorithms/Permutation 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 ...
#Scheme
Scheme
(define (insertions e list) (if (null? list) (cons (cons e list) list) (cons (cons e list) (map (lambda (tail) (cons (car list) tail)) (insertions e (cdr list))))))   (define (permutations list) (if (null? list) (cons list list) (apply append (map (lambda (permut...
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 ...
#ARM_Assembly
ARM Assembly
    /* ARM assembly Raspberry PI */ /* program heapSort.s */ /* look Pseudocode begin this task */   /************************************/ /* Constantes */ /************************************/ .equ STDOUT, 1 @ Linux output console .equ EXIT, 1 @ Linux syscall .equ WRITE, 4 @...
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 ...
#AppleScript
AppleScript
(* In-place, iterative binary merge sort Merge sort algorithm: John von Neumann, 1945.   Convenience terminology used here: run: one of two adjacent source-list ranges containing ordered items for merging. block: range in the destination list to which two runs are merged. *) on mergeSort(the...
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 ...
#Kotlin
Kotlin
fun pancakeSort(a: IntArray) { /** Returns the index of the highest number in the range 0 until n. */ fun indexOfMax(n: Int): Int = (0 until n).maxByOrNull{ a[it] }!!   /** Flips the elements in the range 0 .. n. */ fun flip(index: Int) { a.reverse(0, index + 1) }   for (n in a.size down...
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 ...
#Lua
Lua
-- Initialisation math.randomseed(os.time()) numList = {step = 0, sorted = 0}   -- Create list of n random values function numList:build (n) self.values = {} for i = 1, n do self.values[i] = math.random(-100, 100) end end   -- Return boolean indicating whether the list is in order function numList:isSorted () ...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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 ...
#PowerBASIC
PowerBASIC
%arraysize = 10   SUB stoogesort (L() AS LONG, i AS LONG, j AS LONG) IF L(j) < L(i) THEN SWAP L(i), L(j) IF (j - i) > 1 THEN DIM t AS LONG t = (j - i + 1) / 3 stoogesort L(), i, j - t stoogesort L(), i + t, j stoogesort L(), i, j - t END IF END SUB   FUNCTION PBMAIN (...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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 StoogeSort( [Int32[]] $L ) { $i = 0 $j = $L.length-1 if( $L[$j] -lt $L[$i] ) { $L[$i] = $L[$i] -bxor $L[$j] $L[$j] = $L[$i] -bxor $L[$j] $L[$i] = $L[$i] -bxor $L[$j] } if( $j -gt 1 ) { $t = [int] ( ( $j + 1 ) / 3 ) $k = $j - $t + 1 [Array]::Copy( [Int32[]] ( StoogeSort( $L[0..( $j - $t ) ] )...
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 ...
#Haxe
Haxe
class SelectionSort { @:generic public static function sort<T>(arr:Array<T>) { var len = arr.length; for (index in 0...len) { var minIndex = index; for (remainingIndex in (index+1)...len) { if (Reflect.compare(arr[minIndex], arr[remainingIndex]) > 0) minIndex = remainingIndex; ...
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 ...
#Icon_and_Unicon
Icon and Unicon
procedure main() #: demonstrate various ways to sort a list and string demosort(selectionsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") end     procedure selectionsort(X,op) #: return sorted list ascending(or descending) local i,m   op := sortop(op,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 ...
#JavaScript
JavaScript
var soundex = function (s) { var a = s.toLowerCase().split('') f = a.shift(), r = '', codes = { a: '', e: '', i: '', o: '', u: '', b: 1, f: 1, p: 1, v: 1, c: 2, g: 2, j: 2, k: 2, q: 2, s: 2, x: 2, z: 2, d: 3, t: 3, l: 4, ...
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 ...
#Perl
Perl
sub shell_sort { my (@a, $h, $i, $j, $k) = @_; for ($h = @a; $h = int $h / 2;) { for $i ($h .. $#a) { $k = $a[$i]; for ($j = $i; $j >= $h && $k < $a[$j - $h]; $j -= $h) { $a[$j] = $a[$j - $h]; } $a[$j] = $k; } } @a; }   my @...
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...
#Nanoquery
Nanoquery
class Stack declare internalList   // constructor def Stack() internalList = list() end   def push(val) internalList.append(val) end   def pop() val = internalList[int(len($internalList) - 1)] interna...
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 ...
#Phix
Phix
with javascript_semantics integer n = 6, x = 1, y = 0, counter = 0, len = n, dx = 0, dy = 1 string fmt = sprintf("%%%dd",length(sprintf("%d",n*n))) sequence m = repeat(repeat("??",n),n) for i=1 to 2*n do -- 2n runs.. for j=1 to len do -- of a length... x += d...
http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort
Sorting algorithms/Radix 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 ...
#Rust
Rust
  fn merge(in1: &[i32], in2: &[i32], out: &mut [i32]) { let (left, right) = out.split_at_mut(in1.len()); left.clone_from_slice(in1); right.clone_from_slice(in2); }   // least significant digit radix sort fn radix_sort(data: &mut [i32]) { for bit in 0..31 { // types of small and big is Vec<i32>. ...
http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort
Sorting algorithms/Radix 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 ...
#Scala
Scala
object RadixSort extends App { def sort(toBeSort: Array[Int]): Array[Int] = { // Loop for every bit in the integers var arr = toBeSort for (shift <- Integer.SIZE - 1 until -1 by -1) { // The array to put the partially sorted array into val tmp = new Array[Int](arr.length) // The number of 0s ...
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 ...
#Clojure
Clojure
(defn qsort [L] (if (empty? L) '() (let [[pivot & L2] L] (lazy-cat (qsort (for [y L2 :when (< y pivot)] y)) (list pivot) (qsort (for [y L2 :when (>= y pivot)] y))))))
http://rosettacode.org/wiki/Sorting_algorithms/Patience_sort
Sorting algorithms/Patience 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 patience_sort { my @s = [shift]; for my $card (@_) { my @t = grep { $_->[-1] > $card } @s; if (@t) { push @{shift(@t)}, $card } else { push @s, [$card] } } my @u; while (my @v = grep @$_, @s) { my $value = (my $min = shift @v)->[-1]; for (@v) { ($min, $value) = ($_, $_->[-1]) if $...
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 ...
#C
C
#include <stdio.h>   void insertion_sort(int*, const size_t);   void insertion_sort(int *a, const size_t n) { for(size_t i = 1; i < n; ++i) { int key = a[i]; size_t j = i; while( (j > 0) && (key < a[j - 1]) ) { a[j] = a[j - 1]; --j; } a[j] = key; } }   int main (int argc, char** argv) {   int a[]...
http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
Sorting algorithms/Permutation 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 ...
#Sidef
Sidef
func psort(x, d=x.end) {   if (d.is_zero) { for i in (1 .. x.end) { (x[i] < x[i-1]) && return false; } return true; }   (d+1).times { x.prepend(x.splice(d, 1)...); x[d] < x[d-1] && next; psort(x, d-1) && return true; }   return false; }   v...
http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
Sorting algorithms/Permutation 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 package require struct::list   proc inorder {list} {::tcl::mathop::<= {*}$list}   proc permutationsort {list} { while { ! [inorder $list]} { set list [struct::list nextperm $list] } return $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 ...
#Arturo
Arturo
siftDown: function [items, start, ending][ root: start a: new items while [ending > 1 + 2 * root][ child: 1 + 2 * root if and? ending > child + 1 a\[child+1] > a\[child] -> child: child + 1   if? a\[root] < a\[child][ tmp: a\[child] a\[child]: ...
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 ...
#ARM_Assembly
ARM Assembly
  /* ARM assembly Raspberry PI */ /* program mergeSort.s */   /* REMARK 1 : this program use routines in a include file see task Include a file language arm assembly for the routine affichageMess conversion10 see at end of this program the instruction include */ /* for constantes see task include a file...
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 ...
#Maple
Maple
flip := proc(arr, i) local start, temp, icopy; temp, start, icopy := 0,1,i: while (start < icopy) do arr[start], arr[icopy] := arr[icopy], arr[start]: start:=start+1: icopy:=icopy-1: end do: end proc: findMax := proc(arr, i) local Max, j: Max := 1: for j from 1 to i do if arr[j] > arr[Max] then Max := j:...
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 ...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
ClearAll[LMaxPosition, Flip, pancakeSort] LMaxPosition[a_, n_] := With[{b = Take[a, n]}, First[Ordering[b, -1]]] SetAttributes[Flip, HoldAll]; Flip[a_] := Set[a, Reverse[a]] pancakeSort[in_] := Module[{n, lm, a = in, flips = 0}, Do[ lm = LMaxPosition[a, n]; If[lm < n, Flip[a[[;; lm]]]; Flip[a[[;; n]]]; ...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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 Stooge_Sort(Array L.i(1), i=0 , j=0) If j=0 j=ArraySize(L()) EndIf If L(i)>L(j) Swap L(i), L(j) EndIf If j-i>1 Protected t=(j-i+1)/3 Stooge_Sort(L(), i, j-t) Stooge_Sort(L(), i+t, j ) Stooge_Sort(L(), i, j-t) EndIf EndProcedure
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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
>>> data = [1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7] >>> def stoogesort(L, i=0, j=None): if j is None: j = len(L) - 1 if L[j] < L[i]: L[i], L[j] = L[j], L[i] if j - i > 1: t = (j - i + 1) // 3 stoogesort(L, i , j-t) stoogesort(L, i+t, j ) stoogesort(L, i , j-t) return L   >>> stoogesort(data...
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 ...
#Io
Io
List do ( selectionSortInPlace := method( size repeat(idx, swapIndices(idx, indexOf(slice(idx, size) min)) ) ) )   l := list(-1, 4, 2, -9) l selectionSortInPlace println # ==> list(-9, -1, 2, 4)
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 ...
#IS-BASIC
IS-BASIC
100 PROGRAM "SelecSrt.bas" 110 RANDOMIZE 120 NUMERIC ARRAY(-5 TO 14) 130 CALL INIT(ARRAY) 140 CALL WRITE(ARRAY) 150 CALL SELECTIONSORT(ARRAY) 160 CALL WRITE(ARRAY) 170 DEF INIT(REF A) 180 FOR I=LBOUND(A) TO UBOUND(A) 190 LET A(I)=RND(98)+1 200 NEXT 210 END DEF 220 DEF WRITE(REF A) 230 FOR I=LBOUND(A) TO UB...
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 ...
#Julia
Julia
  using Soundex @assert soundex("Ashcroft") == "A261" # true   # Too trivial? OK. Here is an example not using a package: function soundex(s) char2num = Dict('B'=>1,'F'=>1,'P'=>1,'V'=>1,'C'=>2,'G'=>2,'J'=>2,'K'=>2, 'Q'=>2,'S'=>2,'X'=>2,'Z'=>2,'D'=>3,'T'=>3,'L'=>4,'M'=>5,'N'=>5,'R'=>6) s = replace(s, r"...
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 ...
#Phix
Phix
with javascript_semantics function shell_sort(sequence s) integer gap = floor(length(s)/2), j while gap>0 do for i=gap to length(s) do object si = s[i] j = i-gap while j>=1 and si<=s[j] do s[j+gap] = s[j] j -= gap end while...
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...
#Nemerle
Nemerle
public class Stack[T] { private stack : list[T];   public this() { stack = []; }   public this(init : list[T]) { stack = init; }   public Push(item : T) : Stack[T] { Stack(item::stack) }   public Pop() : T * Stack[T] { (stack.Head, Stack(st...
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 ...
#PicoLisp
PicoLisp
(load "@lib/simul.l")   (de spiral (N) (prog1 (grid N N) (let (Dir '(north east south west .) This 'a1) (for Val (* N N) (=: val Val) (setq This (or (with ((car Dir) This) (unless (: val) This) ) (with ((c...
http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort
Sorting algorithms/Radix 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 ...
#Scheme
Scheme
;;; An illustrative implementation of the radix-10 example at ;;; https://en.wikipedia.org/w/index.php?title=Radix_sort&oldid=1070890278#Least_significant_digit   (cond-expand (r7rs) (chicken (import (r7rs))))   (import (scheme base)) (import (scheme write))   (define (sort-by-decimal-digit data power-of-10) (def...
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 ...
#COBOL
COBOL
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 ...
http://rosettacode.org/wiki/Sorting_algorithms/Patience_sort
Sorting algorithms/Patience 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 patience_sort(sequence s) -- create list of sorted lists sequence piles = {} for i=1 to length(s) do object n = s[i] for p=1 to length(piles)+1 do if p>length(piles) then piles = append(piles,{n}) elsif n>=piles[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 ...
#C.23
C#
namespace Sort { using System;   static class InsertionSort<T> where T : IComparable { public static void Sort(T[] entries) { Sort(entries, 0, entries.Length - 1); }   public static void Sort(T[] entries, Int32 first, Int32 last) { for (var i = first + 1; i <= last; i++) { var entry ...
http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
Sorting algorithms/Permutation 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 ...
#Ursala
Ursala
#import std   permsort "p" = ~&ihB+ ordered"p"*~+ permutations   #cast %sL   example = permsort(lleq) <'pmf','oao','ejw','hhp','oqh','ock','dwj'>
http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
Sorting algorithms/Permutation 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 Sort   var a = [170, 45, 75, -90, -802, 24, 2, 66]   // recursive permutation generator var recurse recurse = Fn.new { |last| if (last <= 0) return Sort.isSorted(a) for (i in 0..last) { var t = a[i] a[i] = a[last] a[last] = t if (recurse.call(last - 1)) return...
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 ...
#AutoHotkey
AutoHotkey
heapSort(a) { Local end end := %a%0 heapify(a,end) While end > 1 %a%%end% := (%a%1 "", %a%1 := %a%%end%) ,siftDown(a, 1, --end) }   heapify(a, count) { Local start start := count // 2 While start siftDown(a, start--, count) }   siftDown(a, start, end) { Local child,...
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 ...
#Astro
Astro
fun mergesort(m): if m.lenght <= 1: return m let middle = floor m.lenght / 2 let left = merge(m[:middle]) let right = merge(m[middle-1:]);   fun merge(left, right): let result = [] while not (left.isempty or right.isempty): if left[1] <= right[1]: result.push! left.shift!() ...
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 ...
#MATLAB_.2F_Octave
MATLAB / Octave
function list = pancakeSort(list)   for i = (numel(list):-1:2)   minElem = list(i); minIndex = i;   %Find the min element in the current subset of the list for j = (i:-1:1) if list(j) <= minElem minElem = list(j); minIndex = j; ...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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
[ 2 * 3 /mod 0 > + ] is twothirds ( n --> n )   [ dup 0 peek over -1 peek 2dup > iff [ rot 0 poke -1 poke ] else 2drop ] is swapends ( [ --> [ )   [ swapends dup size 3 < if done dup size twothirds split swap recurse swap join dup size 3 / split recurse join ...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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
stoogesort = function(vect) { i = 1 j = length(vect) if(vect[j] < vect[i]) vect[c(j, i)] = vect[c(i, j)] if(j - i > 1) { t = (j - i + 1) %/% 3 vect[i:(j - t)] = stoogesort(vect[i:(j - t)]) vect[(i + t):j] = stoogesort(vect[(i + t):j]) vect[i:(j - t)] = stoogesort(vect[i:(j - t)]) } vect }   v = sample(21...
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 ...
#J
J
selectionSort=: verb define data=. y for_xyz. y do. temp=. xyz_index }. data nvidx=. xyz_index + temp i. <./ temp data=. ((xyz_index, nvidx) { data) (nvidx, xyz_index) } data end. data )
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 ...
#Java
Java
public static void sort(int[] nums){ for(int currentPlace = 0;currentPlace<nums.length-1;currentPlace++){ int smallest = Integer.MAX_VALUE; int smallestAt = currentPlace+1; for(int check = currentPlace; check<nums.length;check++){ if(nums[check]<smallest){ smallestAt = check; smallest = nums[check]; ...
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 ...
#Kotlin
Kotlin
// version 1.1.2   fun getCode(c: Char) = when (c) { 'B', 'F', 'P', 'V' -> "1" 'C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z' -> "2" 'D', 'T' -> "3" 'L' -> "4" 'M', 'N' -> "5" 'R' -> "6" 'H', 'W' -> "-" else -> "" }   fun soundex(s: String): String { if (s == "") return "" val sb = Strin...
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 ...
#PHP
PHP
  function shellSort($arr) { $inc = round(count($arr)/2); while($inc > 0) { for($i = $inc; $i < count($arr);$i++){ $temp = $arr[$i]; $j = $i; while($j >= $inc && $arr[$j-$inc] > $temp) { $arr[$j] = $arr[$j - $inc]; $j -= $inc; } $arr[$j] = $temp; } $inc = round($inc/2.2); } return $...
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...
#NetRexx
NetRexx
/* NetRexx ************************************************************ * 13.08.2013 Walter Pachl translated from REXX version 2 **********************************************************************/ options replace format comments java crossref savelog symbols nobinary   stk = create_stk   say push(stk,123) 'from pu...
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 ...
#PL.2FI
PL/I
/* Generates a square matrix containing the integers from 0 to N**2-1, */ /* where N is the length of one side of the square. */ /* Written 22 February 2010. */ declare n fixed binary;   put skip list ('Please type the size of the square:'); get list (n); ...
http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort
Sorting algorithms/Radix 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 ...
#Sidef
Sidef
class Array { method radix_sort(base=10) { var arr = self.clone var rounds = ([arr.minmax].map{.abs}.max.ilog(base) + 1) for i in (0..rounds) { var buckets = (2*base -> of {[]}) var base_i = base**i for n in arr { var digit = (n/base_i % ba...
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 ...
#CoffeeScript
CoffeeScript
  quicksort = ([x, xs...]) -> return [] unless x? smallerOrEqual = (a for a in xs when a <= x) larger = (a for a in xs when a > x) (quicksort smallerOrEqual).concat(x).concat(quicksort larger)  
http://rosettacode.org/wiki/Sorting_algorithms/Patience_sort
Sorting algorithms/Patience 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
<?php class PilesHeap extends SplMinHeap { public function compare($pile1, $pile2) { return parent::compare($pile1->top(), $pile2->top()); } }   function patience_sort(&$n) { $piles = array(); // sort into piles foreach ($n as $x) { // binary search $low = 0; $high = count($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 ...
#C.2B.2B
C++
g++ -std=c++11 insertion.cpp
http://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
Sorting algorithms/Permutation 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
rns:=T(4, 65, 2, 31, 0, 99, 2, 83, 782, 1); fcn psort(list){ len:=list.len(); cnt:=Ref(0); foreach ns in (Utils.Helpers.permuteW(list)){ // lasy permutations cnt.set(1); ns.reduce('wrap(p,n){ if(p>n)return(Void.Stop); cnt.inc(); n }); if(cnt.value==len) return(ns); } }(rns).println();
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 ...
#BBC_BASIC
BBC BASIC
DIM test(9) test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 PROCheapsort(test()) FOR i% = 0 TO 9 PRINT test(i%) ; NEXT PRINT END   DEF PROCheapsort(a()) LOCAL e% PROCheapify(a()) FOR e% = DIM(a(),1) TO 1 STEP -1 SWAP a(e%), a(0) PROC...
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 ...
#BCPL
BCPL
// This can be run using Cintcode BCPL freely available from www.cl.cam.ac.uk/users/mr10.   GET "libhdr.h"   LET heapify(v, k, i, last) BE { LET j = i+i // If there is a son (or two), j = subscript of first. AND x = k // x will hold the larger of the sons if any.   IF j<=last DO x := v!j // j, x = subscrip...
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 ...
#ATS
ATS
(*------------------------------------------------------------------*) (* Mergesort in ATS2, for linear lists. *) (*------------------------------------------------------------------*)   #include "share/atspre_staload.hats"   staload UN = "prelude/SATS/unsafe.sats"   #define NIL list_vt_nil ...
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 ...
#MAXScript
MAXScript
fn flipArr arr index = ( local new = #() for i = index to 1 by -1 do ( append new arr[i] ) join new (for i in (index+1) to arr.count collect arr[i]) return new )   fn pancakeSort arr = ( if arr.count < 2 then return arr else ( for i = arr.count to 1 by -1 do ( local newArr = for n in 1 to i collect a...
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 ...
#NetRexx
NetRexx
/* NetRexx */ options replace format comments java crossref symbols nobinary   import java.util.List   runSample(arg) return   -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ method pancakeSort(tlist = List, debug = (1 == 0)) private static returns List   if tlist.size() > 1 then do ...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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 ...
#Racket
Racket
  #lang racket (define (stooge-sort xs [i 0] [j (- (vector-length xs) 1)]) (define (x i) (vector-ref xs i)) (define (x! i v) (vector-set! xs i v)) (define (swap! i j) (define t (x i)) (x! i (x j)) (x! j t)) (when (> (x i) (x j)) (swap! i j)) (when (> (- j i) 1) (define t (quotient (+ j (- i) 1) 3)) (s...
http://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort
Sorting algorithms/Stooge 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 ...
#Raku
Raku
sub stoogesort( @L, $i = 0, $j = @L.end ) { @L[$j,$i] = @L[$i,$j] if @L[$i] > @L[$j];   my $interval = $j - $i;   if $interval > 1 { my $t = ( $interval + 1 ) div 3; stoogesort( @L, $i , $j-$t ); stoogesort( @L, $i+$t, $j ); stoogesort( @L, $i , $j-$t ); } ...
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 ...
#JavaScript
JavaScript
function selectionSort(nums) { var len = nums.length; for(var i = 0; i < len; i++) { var minAt = i; for(var j = i + 1; j < len; j++) { if(nums[j] < nums[minAt]) minAt = j; }   if(minAt != i) { var temp = nums[i]; nums[i] = nums[minAt]; nums[minAt] = temp; } } ...
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 ...
#Lua
Lua
local d, digits, alpha = '01230120022455012623010202', {}, ('A'):byte() d:gsub(".",function(c) digits[string.char(alpha)] = c alpha = alpha + 1 end)   function soundex(w) local res = {} for c in w:upper():gmatch'.'do local d = digits[c] if d then if #res==0 then res[1] = c elseif #...
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 ...
#Picat
Picat
go => A = [23, 76, 99, 58, 97, 57, 35, 89, 51, 38, 95, 92, 24, 46, 31, 24, 14, 12, 57, 78], println(A), shell_sort(A), println(A), nl.   % Inline sort shell_sort(A) => Inc = round(A.length/2), while (Inc > 0) foreach(I in Inc+1..A.length) Temp = A[I], J := I, while (J > Inc, ...
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 ...
#PicoLisp
PicoLisp
(de shellSort (A) (for (Inc (*/ (length A) 2) (gt0 Inc) (*/ Inc 10 22)) (for (I Inc (get A I) (inc I)) (let (Tmp @ J I) (while (and (>= J Inc) (> (get A (- J Inc)) Tmp)) (set (nth A J) (get A (- J Inc))) (dec 'J Inc) ) (set (nth A J) Tmp) ) ) ...
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...
#Nim
Nim
type Stack[T] = distinct seq[T]   func initStack[T](initialSize = 32): Stack[T] = Stack[T](newSeq[T](initialSize))   func isEmpty[T](stack: Stack[T]): bool = seq[T](stack).len == 0   func push[T](stack: var Stack[T]; item: sink T) = seq[T](stack).add(item)   func pop[T](stack: var Stack[T]): T = if stack.isEmpt...
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 ...
#PowerShell
PowerShell
function Spiral-Matrix ( [int]$N ) { # Initialize variables $X = 0 $Y = -1 $i = 0 $Sign = 1   # Intialize array $A = New-Object 'int[,]' $N, $N   # Set top row 1..$N | ForEach { $Y += $Sign; $A[$X,$Y] = ++$i }   # For each remaining half spiral... ForEach ( $M in ($N-...
http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort
Sorting algorithms/Radix 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 radixsort&{base:} sink bucketize def value: $; $ ~/ $@radixsort.digit -> # when <=0 ?($value <0..>)> do ..|@radixsort.positives: $value; when <=0> do ..|@radixsort.negatives(last): $value; otherwise def bucket: $ mod $base -> \(<?($value<0..>)> $ + 1 ! <=0> $base ! <>...
http://rosettacode.org/wiki/Sorting_algorithms/Radix_sort
Sorting algorithms/Radix 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 proc splitByRadix {lst base power} { # create a list of empty lists to hold the split by digit set out [lrepeat [expr {$base*2}] {}] foreach item $lst { # pulls the selected digit set digit [expr {($item / $base ** $power) % $base + $base * ($item >= 0)}] # append the number to th...