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/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...
#ooRexx
ooRexx
  stack = .queue~of(123, 234) -- creates a stack with a couple of items stack~push("Abc") -- pushing value = stack~pull -- popping value = stack~peek -- peeking -- the is empty test if stack~isEmpty then say "The 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 ...
#Raku
Raku
class Turtle { my @dv = [0,-1], [1,-1], [1,0], [1,1], [0,1], [-1,1], [-1,0], [-1,-1]; my $points = 8; # 'compass' points of neighbors on grid: north=0, northeast=1, east=2, etc.   has @.loc = 0,0; has $.dir = 0; has %.world; has $.maxegg; has $.range-x; has $.range-y;   method turn-...
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 ...
#EasyLang
EasyLang
data[] = [ 29 4 72 44 55 26 27 77 92 5 ] # func qsort left right . . while left < right swap data[(left + right) / 2] data[left] mid = left for i = left + 1 to right if data[i] < data[left] mid += 1 swap data[i] data[mid] . . swap data[left] data[mid] call qsort le...
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 ...
#Ruby
Ruby
class Array def patience_sort piles = [] each do |i| if (idx = piles.index{|pile| pile.last <= i}) piles[idx] << i else piles << [i] #create a new pile end end # merge piles result = [] until piles.empty? first = piles.map(&:first) idx = first.i...
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 ...
#Delphi
Delphi
program TestInsertionSort;   {$APPTYPE CONSOLE}   {.$DEFINE DYNARRAY} // remove '.' to compile with dynamic array   type TItem = Integer; // declare ordinal type for array item {$IFDEF DYNARRAY} TArray = array of TItem; // dynamic array {$ELSE} TArray = array[0..15] of TItem; // static array {$ENDIF...
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 ...
#D
D
import std.stdio, std.container;   void heapSort(T)(T[] data) /*pure nothrow @safe @nogc*/ { for (auto h = data.heapify; !h.empty; h.removeFront) {} }   void main() { auto items = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]; items.heapSort; items.writeln; }
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 ...
#Clojure
Clojure
  (defn merge [left right] (cond (nil? left) right (nil? right) left :else (let [[l & *left] left [r & *right] right] (if (<= l r) (cons l (merge *left right)) (cons r (merge left *right))))))   (defn merge-sort [list] (if (< (count li...
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 ...
#PureBasic
PureBasic
If OpenConsole() Define i, j, k, Loops Dim Pile(9) ;-------------------------------------------------------------- ;- Create a Random Pile() For i=1 To 9 ;- Initiate the Pile Pile(i)=i Next For i=9 To 1 Step -1 ;- Do a Fisher-Yates shuffle Swap Pile(...
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 ...
#XPL0
XPL0
code ChOut=8, IntOut=11; \intrinsic routines   proc StoogeSort(L, I, J); \Sort array L int L, I, J; int T; [if L(J) < L(I) then [T:= L(I); L(I):= L(J); L(J):= T]; \swap if J-I > 1 then [T:= (J-I+1)/3; StoogeSort(L, I, J-T); StoogeSort(L, I+T, J); StoogeSort(L, I, J-T); ]; ];   in...
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 ...
#Yorick
Yorick
func stoogesort(&L, i, j) { if(is_void(i)) i = 1; if(is_void(j)) j = numberof(L); if(L(j) < L(i)) L([i,j]) = L([j,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; } }
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 ...
#MAXScript
MAXScript
fn selectionSort arr = ( local min = undefined for i in 1 to arr.count do ( min = i for j in i+1 to arr.count do ( if arr[j] < arr[min] then ( min = j ) ) swap arr[i] arr[min] ) arr )   data = selectionSort #...
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 ...
#N.2Ft.2Froff
N/t/roff
.de end .. .de array . nr \\$1.c 0 1 . de \\$1.push end . nr \\$1..\\\\n+[\\$1.c] \\\\$1 . end . de \\$1.pushln end . if \\\\n(.$>0 .\\$1.push \\\\$1 . if \\\\n(.$>1 \{ \ . shift . \\$1.pushln \\\\$@ . \} . end . de \\$1.dump end . nr i 0 1 . while \\\\n+i<=\\\\n[\\$1.c] .tm \\\\n[\\$1..\\\\ni] . rr i . end ...
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 ...
#Perl
Perl
use Text::Soundex; print soundex("Soundex"), "\n"; # S532 print soundex("Example"), "\n"; # E251 print soundex("Sownteks"), "\n"; # S532 print soundex("Ekzampul"), "\n"; # E251
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 ...
#Seed7
Seed7
const proc: shellSort (inout array elemType: arr) is func local var integer: i is 0; var integer: j is 0; var integer: increment is 0; var elemType: help is elemType.value; begin increment := length(arr) div 2; while increment > 0 do for i range 1 to length(arr) do j := i; ...
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 ...
#Sidef
Sidef
func shell_sort(a) { var h = a.len; while (h >>= 1) { for i in (h .. a.end) { var k = a[i]; for (var j = i; (j >= h) && (k < a[j - h]); j -= h) { a[j] = a[j - h]; } a[j] = k; } } return a; }   var a = 10.of {100.irand}; say ...
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...
#OxygenBasic
OxygenBasic
  function f() sys a=1,b=2,c=3,d=4 push a push b push c push d print a "," b "," c "," d 'result 1,2,3,4 a=10 b=20 c=30 d=40 print a "," b "," c "," d 'result 10,20,30,40 pop a pop b pop c pop d print a "," b "," c "," d 'result 4,3,2,1 end function   f  
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 ...
#REXX
REXX
/*REXX program displays a spiral in a square array (of any size) starting at START. */ parse arg size start . /*obtain optional arguments from the CL*/ if size =='' | size =="," then size =5 /*Not specified? Then use the default.*/ if start=='' | start=="," then start=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 ...
#EchoLisp
EchoLisp
  (lib 'list) ;; list-partition   (define compare 0) ;; counter   (define (quicksort L compare-predicate: proc aux: (part null)) (if (<= (length L) 1) L (begin ;; counting the number of comparisons (set! compare (+ compare (length (rest L)))) ;; pivot = first element of list (set! part (list...
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 ...
#Scala
Scala
import scala.collection.mutable   object PatienceSort extends App { def sort[A](source: Iterable[A])(implicit bound: A => Ordered[A]): Iterable[A] = { val piles = mutable.ListBuffer[mutable.Stack[A]]()   def PileOrdering: Ordering[mutable.Stack[A]] = (a: mutable.Stack[A], b: mutable.Stack[A]) => b.head...
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 ...
#E
E
def insertionSort(array) { for i in 1..!(array.size()) { def value := array[i] var j := i-1 while (j >= 0 && array[j] > value) { array[j + 1] := array[j] j -= 1 } array[j+1] := value } }
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 ...
#Dart
Dart
  void heapSort(List a) { int count = a.length;   // first place 'a' in max-heap order heapify(a, count);   int end = count - 1; while (end > 0) { // swap the root (maximum value) of the heap with the // last element of the heap int tmp = a[end]; a[end] = a[0]; a[0] = tmp;   // put th...
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 ...
#Delphi
Delphi
proc nonrec siftDown([*] int a; word start, end) void: word root, child; int temp; bool stop;   root := start; stop := false; while not stop and root*2 + 1 <= end do child := root*2 + 1; if child+1 <= end and a[child] < a[child + 1] then child := child + 1 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 ...
#COBOL
COBOL
IDENTIFICATION DIVISION. PROGRAM-ID. MERGESORT. AUTHOR. DAVE STRATFORD. DATE-WRITTEN. APRIL 2010. INSTALLATION. HEXAGON SYSTEMS LIMITED. **********************************************************...
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 ...
#Python
Python
tutor = False   def pancakesort(data): if len(data) <= 1: return data if tutor: print() for size in range(len(data), 1, -1): maxindex = max(range(size), key=data.__getitem__) if maxindex+1 != size: # This indexed max needs moving if maxindex != 0: ...
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 ...
#zkl
zkl
fcn stoogeSort(list,i=0,j=Void){ if(Void==j) j=list.len() - 1; // default parameters set before call if(list[j]<list[i]) list.swap(i,j); if(j - i >1){ t:=(j - i + 1)/3; stoogeSort(list,i , j-t); stoogeSort(list,i+t, j ); stoogeSort(list,i , j-t); } list }
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 ...
#Nanoquery
Nanoquery
import math   def sort(nums) global math for currentPlace in range(0, len(nums) - 2) smallest = math.maxint smallestAt = currentPlace + 1 for check in range(currentPlace, len(nums) - 1) if nums[check] < smallest smallestAt = check smallest = nums[check] end end temp = nums[currentPlace] nu...
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 ...
#Nemerle
Nemerle
using System; using System.Console;   module Selection { public static Sort[T](this a : array[T]) : void where T : IComparable { mutable k = 0; def lastindex = a.Length - 1;   foreach (i in [0 .. lastindex]) { k = i; foreach (j in [i .. lastindex]) ...
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 ...
#Phix
Phix
with javascript_semantics constant soundex_alphabet = "0123012#02245501262301#202" -- ABCDEFGHIJKLMNOPQRSTUVWXYZ function soundex(string name) string res = "0000" integer rdx = 1, ch, curr, prev for i=1 to length(name) do ch = upper(name[i]) if ch>='A' and ch<='Z'...
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 ...
#Swift
Swift
func shellsort<T where T : Comparable>(inout seq: [T]) { var inc = seq.count / 2 while inc > 0 { for (var i, el) in EnumerateSequence(seq) { while i >= inc && seq[i - inc] > el { seq[i] = seq[i - inc] i -= inc } seq[i] = el } ...
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 ...
#Tcl
Tcl
package require Tcl 8.5   proc shellsort {m} { set len [llength $m] set inc [expr {$len / 2}] while {$inc > 0} { for {set i $inc} {$i < $len} {incr i} { set j $i set temp [lindex $m $i] while {$j >= $inc && [set val [lindex $m [expr {$j - $inc}]]] > $temp} { ...
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...
#Oz
Oz
functor export New Push Pop Empty define fun {New} {NewCell nil} end   proc {Push Stack Element} NewStack %% Use atomic swap for thread safety OldStack = Stack := NewStack in NewStack = Element|OldStack end   proc {Pop Stack ?Result} NewStack %%...
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 ...
#Ring
Ring
  # Project : Spiral matrix   load "guilib.ring" load "stdlib.ring" new qapp { win1 = new qwidget() { setwindowtitle("Spiral matrix") setgeometry(100,100,600,400) n = 5 result = newlist(n,n) spiral = newlist(...
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 ...
#Eero
Eero
#import <Foundation/Foundation.h>   void quicksortInPlace(MutableArray array, const long first, const long last) if first >= last return Value pivot = array[(first + last) / 2] left := first right := last while left <= right while array[left] < pivot left++ while array[right] > pivot r...
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 ...
#Scheme
Scheme
(define-library (rosetta-code k-way-merge)   (export k-way-merge)   (import (scheme base)) (import (scheme case-lambda)) (import (only (srfi 1) car+cdr)) (import (only (srfi 1) reverse!)) (import (only (srfi 132) list-merge)) (import (only (srfi 151) bitwise-xor))   (begin   ;; ;; The algorithm ...
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 ...
#EasyLang
EasyLang
subr sort for i = 1 to len data[] - 1 h = data[i] j = i - 1 while j >= 0 and h < data[j] data[j + 1] = data[j] j -= 1 . data[j + 1] = h . . data[] = [ 29 4 72 44 55 26 27 77 92 5 ] call sort print data[]
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 ...
#Draco
Draco
proc nonrec siftDown([*] int a; word start, end) void: word root, child; int temp; bool stop;   root := start; stop := false; while not stop and root*2 + 1 <= end do child := root*2 + 1; if child+1 <= end and a[child] < a[child + 1] then child := child + 1 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 ...
#CoffeeScript
CoffeeScript
# This is a simple version of mergesort that returns brand-new arrays. # A more sophisticated version would do more in-place optimizations. merge_sort = (arr) -> if arr.length <= 1 return (elem for elem in arr) m = Math.floor(arr.length / 2) arr1 = merge_sort(arr.slice 0, m) arr2 = merge_sort(arr.slice m) ...
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 ...
#Quackery
Quackery
[ split reverse join ] is flip ( [ n --> [ )   [ 0 swap behead swap witheach [ 2dup > iff [ nip nip i^ 1+ swap ] else drop ] drop ] is smallest ( [ --> n )   [ dup size times [ dup i^ split nip smallest i^ + flip i^ flip ] ] i...
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 ...
#Racket
Racket
  #lang racket   (define (pancake-sort l) (define (flip l n) (append (reverse (take l n)) (drop l n))) (for/fold ([l l]) ([i (in-range (length l) 1 -1)]) (let* ([i2 (cdr (for/fold ([m #f]) ([x l] [j i]) (if (and m (<= x (car m))) m (cons x j))))] [l (if (zero? i2) l (flip l (add...
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 ...
#NetRexx
NetRexx
/* NetRexx */ options replace format comments java crossref savelog symbols binary   import java.util.List   placesList = [String - "UK London", "US New York", "US Boston", "US Washington" - , "UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" - ]   lists = [ - placesList - ...
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 ...
#PHP
PHP
<?php echo soundex("Soundex"), "\n"; // S532 echo soundex("Example"), "\n"; // E251 echo soundex("Sownteks"), "\n"; // S532 echo soundex("Ekzampul"), "\n"; // E251 ?>
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 ...
#uBasic.2F4tH
uBasic/4tH
PRINT "Shell sort:" n = FUNC (_InitArray) PROC _ShowArray (n) PROC _Shellsort (n) PROC _ShowArray (n) PRINT   END     _Shellsort PARAM (1) ' Shellsort subroutine LOCAL (4) b@ = a@   DO WHILE b@ b@ = b@ / 2 FOR c@ = b@ TO a@ - 1 e@ = @(c@) d@ = c@ DO WHILE (d@ > ...
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...
#PARI.2FGP
PARI/GP
push(x)=v=concat(v,[x]);; pop()={ if(#v, my(x=v[#v]); v=vecextract(v,1<<(#v-1)-1); x , error("Stack underflow") ) }; empty()=v==[]; peek()={ if(#v, v[#v] , error("Stack underflow") ) };
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 ...
#Ruby
Ruby
def spiral(n) spiral = Array.new(n) {Array.new(n, nil)} # n x n array of nils runs = n.downto(0).each_cons(2).to_a.flatten # n==5; [5,4,4,3,3,2,2,1,1,0] delta = [[1,0], [0,1], [-1,0], [0,-1]].cycle x, y, value = -1, 0, -1 for run in runs dx, dy = delta.next run.times { spiral[y+=dy][x+=dx] = (val...
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 ...
#Eiffel
Eiffel
QUICKSORT
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 ...
#Sidef
Sidef
func patience(deck) { var stacks = []; deck.each { |card| given (stacks.first { card < .last }) { |stack| case (defined stack) { stack << card } default { stacks << [card] } } }   gather { while (stacks) { take stacks.min_by { .last }.pop stacks.gr...
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 ...
#Eiffel
Eiffel
class MY_SORTED_SET [G -> COMPARABLE] inherit TWO_WAY_SORTED_SET [G] redefine sort end create make   feature sort -- Insertion sort local l_j: INTEGER l_value: like item do across 2 |..| count as ii loop ...
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 ...
#E
E
def heapsort := { def cswap(c, a, b) { def t := c[a] c[a] := c[b] c[b] := t # println(c) }   def siftDown(array, start, finish) { var root := start while (var child := root * 2 + 1 child <= finish) { if (child + 1 <= finish && array[child] < array[child + 1]) { c...
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 ...
#Common_Lisp
Common Lisp
(defun merge-sort (result-type sequence predicate) (let ((split (floor (length sequence) 2))) (if (zerop split) (copy-seq sequence) (merge result-type (merge-sort result-type (subseq sequence 0 split) predicate) (merge-sort result-type (subseq sequence split) predicate)...
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 ...
#Raku
Raku
sub pancake_sort ( @a is copy ) { my $endpoint = @a.end; while $endpoint > 0 and not [<] @a { my $max_i = [0..$endpoint].max: { @a[$_] }; my $max = @a[$max_i]; if @a[$endpoint] == $max { $endpoint-- while @a[$endpoint] == $max; next; } # @a[$endp...
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 ...
#Nim
Nim
proc selectionSort[T](a: var openarray[T]) = let n = a.len for i in 0 ..< n: var m = i for j in i ..< n: if a[j] < a[m]: m = j swap a[i], a[m]   var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] selectionSort a echo 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 ...
#OCaml
OCaml
let rec selection_sort = function [] -> [] | first::lst -> let rec select_r small output = function [] -> small :: selection_sort output | x::xs when x < small -> select_r x (small::output) xs | x::xs -> select_r small (x::output) xs in select_r first [] ...
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 ...
#Picat
Picat
go => Names = split("Lloyd Woolcock Donnell Baragwanath Williams Ashcroft Ashcraft Euler Ellery Gauss Ghosh Hilbert Heilbronn Knuth Kant Ladd Lukasiewicz Lissajous O'Hara"), SoundexNames = split("L300 W422 D540 B625 W452 A261 A261 E460 E460 G200 G200 H416 H416 K530 K530 L300 L222 L222 O600"),   foreach({Name,...
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 ...
#Visual_Basic
Visual Basic
Sub arrShellSort(ByVal arrData As Variant) Dim lngHold, lngGap As Long Dim lngCount, lngMin, lngMax As Long Dim varItem As Variant ' lngMin = LBound(arrData) lngMax = UBound(arrData) lngGap = lngMin Do While (lngGap < lngMax) lngGap = 3 * lngGap + 1 Loop Do While (lngGap > 1) lngGap = lngGap...
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...
#Pascal
Pascal
{ tStack is the actual stack type, tStackNode a helper type } type pStackNode = ^tStackNode; tStackNode = record next: pStackNode; data: integer; end; tStack = record top: pStackNode; end;   { Always call InitStack before using a stack } proced...
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 ...
#Rust
Rust
const VECTORS: [(isize, isize); 4] = [(1, 0), (0, 1), (-1, 0), (0, -1)];   pub fn spiral_matrix(size: usize) -> Vec<Vec<u32>> { let mut matrix = vec![vec![0; size]; size]; let mut movement = VECTORS.iter().cycle(); let (mut x, mut y, mut n) = (-1, 0, 1..);   for (move_x, move_y) in std::iter::once(size)...
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 ...
#Elena
Elena
import extensions; import system'routines; import system'collections;   extension op { quickSort() { if (self.isEmpty()) { ^ self };   var pivot := self[0];   auto less := new ArrayList(); auto pivotList := new ArrayList(); auto more := new ArrayList();   self.for...
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 ...
#Standard_ML
Standard ML
structure PilePriority = struct type priority = int fun compare (x, y) = Int.compare (y, x) (* we want min-heap *) type item = int list val priority = hd end   structure PQ = LeftPriorityQFn (PilePriority)   fun sort_into_piles n = let val piles = DynamicArray.array (length n, []) fun bsearch_piles 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 ...
#Elena
Elena
import extensions;   extension op { insertionSort() = self.clone().insertionSort(0, self.Length - 1);   insertionSort(int first, int last) { for(int i := first + 1, i <= last, i += 1) { var entry := self[i]; int j := i;   while (j > first && self[j...
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 ...
#EasyLang
EasyLang
subr make_heap for i = 1 to n - 1 if data[i] > data[(i - 1) / 2] j = i while data[j] > data[(j - 1) / 2] swap data[j] data[(j - 1) / 2] j = (j - 1) / 2 . . . . subr sort n = len data[] call make_heap for i = n - 1 downto 1 swap data[0] data[i] j = 0 ind = ...
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 ...
#Crystal
Crystal
def merge_sort(a : Array(Int32)) : Array(Int32) return a if a.size <= 1 m = a.size // 2 lt = merge_sort(a[0 ... m]) rt = merge_sort(a[m .. -1]) return merge(lt, rt) end   def merge(lt : Array(Int32), rt : Array(Int32)) : Array(Int32) result = Array(Int32).new until lt.empty? || rt.empty? result << (lt...
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 ...
#REXX
REXX
/*REXX program sorts and displays an array using the pancake sort algorithm. */ call gen /*generate elements in the @. array.*/ call show 'before sort' /*display the BEFORE array elements.*/ say copies('▒', 60) ...
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 ...
#Oforth
Oforth
: selectSort(l) | b j i k s | l size ->s l asListBuffer ->b   s loop: i [ i dup ->k b at i 1 + s for: j [ b at(j) 2dup <= ifTrue: [ drop ] else: [ nip j ->k ] ] k i b at b put i swap b put ] b dup freeze ;
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 ...
#ooRexx
ooRexx
/*REXX **************************************************************** * program sorts an array using the selection-sort method. * derived from REXX solution * Note that ooRexx can process Elements of the stem argument (Use Arg) * 06.10.2010 Walter Pachl ***********************************************************...
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 ...
#PicoLisp
PicoLisp
(de soundex (Str) (pack (pad -4 (cons (uppc (char (char Str))) (head 3 (let Last NIL (extract '((C) (and (setq C (case (uppc C) ...
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 ...
#Vlang
Vlang
fn shell(mut arr []int, n int) { mut j := 0 for h := n; h /= 2; { for i := h; i < n; i++ { t := arr[i] for j = i; j >= h && t < arr[j - h]; j -= h { arr[j] = arr[j - h] } arr[j] = t } } }   fn main() { mut arr := [4, 65, 2, -31, 0, 99, 2, 83, 782, 1] n := arr.len println('Input: ' + arr.str()...
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 ...
#Whitespace
Whitespace
var shellSort = Fn.new { |a| var n = a.count var gaps = [701, 301, 132, 57, 23, 10, 4, 1] for (gap in gaps) { if (gap < n) { for (i in gap...n) { var t = a[i] var j = i while (j >= gap && a[j-gap] > t) { a[j] = a[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...
#Perl
Perl
sub empty{ not @_ }
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 ...
#Scala
Scala
class Folder(){ var dir = (1,0) var pos = (-1,0) def apply(l:List[Int], a:Array[Array[Int]]) = { var (x,y) = pos //start position var (dx,dy) = dir //direction l.foreach {e => x = x + dx; y = y + dy; a(y)(x) = e } //copy l elements to array using current direction pos = (x,y) dir = (-dy, dx)...
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 ...
#Elixir
Elixir
defmodule Sort do def qsort([]), do: [] def qsort([h | t]) do {lesser, greater} = Enum.split_with(t, &(&1 < h)) qsort(lesser) ++ [h] ++ qsort(greater) end end
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 ...
#Tcl
Tcl
package require Tcl 8.6   proc patienceSort {items} { # Make the piles set piles {} foreach item $items { set p [lsearch -bisect -index end $piles $item] if {$p == -1} { lappend piles [list $item] } else { lset piles $p end+1 $item } } # Merge the piles; no suitable builtin, alas s...
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 ...
#Elixir
Elixir
defmodule Sort do def insert_sort(list) when is_list(list), do: insert_sort(list, [])   def insert_sort([], sorted), do: sorted def insert_sort([h | t], sorted), do: insert_sort(t, insert(h, sorted))   defp insert(x, []), do: [x] defp insert(x, sorted) when x < hd(sorted), do: [x | sorted] defp insert(x, [h...
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 ...
#EchoLisp
EchoLisp
  (lib 'heap)   (define (heap-sort list) (define heap (make-heap < )) ;; make a min heap (list->heap list heap) (while (not (heap-empty? heap)) (push 'stack (heap-pop heap))) (stack->list 'stack))   (define L (shuffle (iota 15))) → (9 4 0 12 8 3 10 7 11 2 5 6 14 13 1) (heap-sort L) → ...
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 ...
#Curry
Curry
-- merge sort: sorting two lists by merging the sorted first -- and second half of the list   sort :: ([a] -> [a] -> [a] -> Success) -> [a] -> [a] -> Success   sort merge xs ys = if length xs < 2 then ys =:= xs else sort merge (firsthalf xs) us & sort merge (secondhalf xs) vs & merge us vs ys w...
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 ...
#Ring
Ring
  pancakeList = [6, 7, 8, 9, 2, 5, 3, 4, 1] flag = 0 see "Before :" + nl for n = 1 to len(pancakeList) see pancakeList[n] + " " next see nl   pancakeSort(pancakeList)   see "After :" + nl for n = 1 to len(pancakeList) see pancakeList[n] + " " next see nl   func pancakeSort A n = len(A) while flag = 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 ...
#Ruby
Ruby
class Array def pancake_sort! num_flips = 0 (self.size-1).downto(1) do |end_idx| max = self[0..end_idx].max max_idx = self[0..end_idx].index(max) next if max_idx == end_idx   if max_idx > 0 self[0..max_idx] = self[0..max_idx].reverse p [num_flips += 1, self] if $D...
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 ...
#Oz
Oz
declare proc {SelectionSort Arr} proc {Swap K L} Arr.K := (Arr.L := Arr.K) end Low = {Array.low Arr} High = {Array.high Arr} in %% for every index I of the array for I in Low..High do %% find the index of the minimum element %% with an index >= I Min = {NewCell 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 ...
#PL.2FI
PL/I
Soundex: procedure (pword) returns (character(4)); declare pword character (*) varying, value character (length(pword)) varying; declare word character (length(pword)); declare (prevCode, currCode) character (1); declare alphabet CHARACTER (26) STATIC INITIAL ('AEIOUHWYBFPVCGJKQSXZDTLMNR'); declare repla...
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 ...
#Wren
Wren
var shellSort = Fn.new { |a| var n = a.count var gaps = [701, 301, 132, 57, 23, 10, 4, 1] for (gap in gaps) { if (gap < n) { for (i in gap...n) { var t = a[i] var j = i while (j >= gap && a[j-gap] > t) { a[j] = a[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...
#Phix
Phix
with javascript_semantics -- comparing a simple implementation against using the builtins: sequence stack = {} procedure push_(object what) stack = append(stack,what) end procedure function pop_() object what = stack[$] stack = stack[1..$-1] return what end function function empty_() return le...
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 ...
#Scilab
Scilab
function a = spiral(n) a = ones(n*n, 1) v = ones(n, 1) u = -n*v; i = n for k = n-1:-1:1 j = 1:k u(j) = -u(j) a(j+i) = u(j) v(j) = -v(j) a(j+(i+k)) = v(j) i = i+2*k end a(cumsum(a)) = (1:n*n)' a = matrix(a, n, n)'-1 endfunction   -->spiral(5) ans =   0. 1. 2. 3. ...
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 ...
#Erlang
Erlang
  -module( quicksort ).   -export( [qsort/1] ).   qsort([]) -> []; qsort([X|Xs]) -> qsort([ Y || Y <- Xs, Y < X]) ++ [X] ++ qsort([ Y || Y <- Xs, Y >= X]).  
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 ...
#Wren
Wren
import "/sort" for Cmp   var patienceSort = Fn.new { |a| var size = a.count if (size < 2) return var cmp = Cmp.default(a[0]) var piles = [] for (e in a) { var outer = false for (pile in piles) { if (cmp.call(pile[-1], e) > 0) { pile.add(e) ...
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 ...
#Emacs_Lisp
Emacs Lisp
(defun min-or-max-of-a-list (numbers comparator) "Return minimum or maximum of NUMBERS using COMPARATOR." (let ((extremum (car numbers))) (dolist (n (cdr numbers)) (when (funcall comparator n extremum) (setq extremum n))) extremum))   (defun remove-number-from-list (numbers n) "Return NUMBER...
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 ...
#Eiffel
Eiffel
  class HEAPSORT   feature   sort_array (ar: ARRAY [INTEGER]) -- Sorts array 'ar' in ascending order. require not_empty: ar.count > 0 local i, j, r, l, m, n: INTEGER sorted: BOOLEAN do n := ar.count j := 0 i := 0 m := 0 r := n l := (n // 2)+1 from until sorted loop ...
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 ...
#D
D
import std.stdio, std.algorithm, std.array, std.range;   T[] mergeSorted(T)(in T[] D) /*pure nothrow @safe*/ { if (D.length < 2) return D.dup; return [D[0 .. $ / 2].mergeSorted, D[$ / 2 .. $].mergeSorted] .nWayUnion.array; }   void main() { [3, 4, 2, 5, 1, 6].mergeSorted.writeln; }
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 ...
#Rust
Rust
fn pancake_sort<T: Ord>(v: &mut [T]) { let len = v.len(); // trivial case -- no flips if len < 2 { return; } for i in (0..len).rev() { // find index of the maximum element within `v[0..i]` (inclusive) let max_index = v.iter() .take(i + 1) .enumerate() ...
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 ...
#Sidef
Sidef
func pancake(a) { for idx in ^(a.end) { var min = idx for i in (idx+1 .. a.end) { min = i if (a[min] > a[i]) } next if (a[min] == a[idx]) a[min..a.end] = [a[min..a.end]].reverse... a[idx..a.end] = [a[idx..a.end]].reverse... } return a }   var arr = 10.of{ 100.irand } ...
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 ...
#PARI.2FGP
PARI/GP
selectionSort(v)={ for(i=1,#v-1, my(mn=i,t); for(j=i+1,#v, if(v[j]<v[mn],mn=j) ); t=v[mn]; v[mn]=v[i]; v[i]=t ); v };
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 ...
#PowerShell
PowerShell
  function Get-Soundex { [CmdletBinding()] [OutputType([PSCustomObject])] Param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]] $InputObject )   ...
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 ...
#XPL0
XPL0
  include c:\cxpl\codes; \intrinsic 'code' declarations string 0; \use zero-terminated strings     proc SSort(A, N); \Shell sort array in ascending order char A; \address of array int N; \number of elements in array (size) int...
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 ...
#Yabasic
Yabasic
export sub shell_sort(x()) // Shell sort based on insertion sort   local gap, i, j, first, last, tempi, tempj   last = arraysize(x(),1) gap = int(last / 10) + 1 while(TRUE) first = gap + 1 for i = first to last tempi = x(i) j = i - gap while(TRUE) tempj = x(j) if tempi >= tempj then...
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...
#PHP
PHP
$stack = array();   empty( $stack ); // true   array_push( $stack, 1 ); // or $stack[] = 1; array_push( $stack, 2 ); // or $stack[] = 2;   empty( $stack ); // false   echo array_pop( $stack ); // outputs "2" echo array_pop( $stack ); // outputs "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 ...
#Seed7
Seed7
$ include "seed7_05.s7i";   const type: matrix is array array integer;   const func matrix: spiral (in integer: n) is func result var matrix: myArray is matrix.value; local var integer: i is 0; var integer: dx is 1; var integer: dy is 0; var integer: x is 1; var integer: y is 1; var inte...
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 ...
#Emacs_Lisp
Emacs Lisp
(require 'seq)   (defun quicksort (xs) (if (null xs) () (let* ((head (car xs)) (tail (cdr xs)) (lower-part (quicksort (seq-filter (lambda (x) (<= x head)) tail))) (higher-part (quicksort (seq-filter (lambda (x) (> x head)) tail)))) (append lower-part (list head) higher...
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 ...
#zkl
zkl
fcn patienceSort(ns){ piles:=L(); foreach n in (ns){ newPile:=True; // create list of sorted lists foreach p in (piles){ if(n>=p[-1]) { p.append(n); newPile=False; break; } } if(newPile)piles.append(L(n)); } // merge sort the piles r:=Sink(List); while(piles){ mins:=piles.appl...
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 ...
#Erlang
Erlang
-module(sort). -export([insertion/1]).   insertion(L) -> lists:foldl(fun insert/2, [], L).   insert(X,[]) -> [X]; insert(X,L=[H|_]) when X =< H -> [X|L]; insert(X,[H|T]) -> [H|insert(X, T)].
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 ...
#Elixir
Elixir
defmodule Sort do def heapSort(list) do len = length(list) heapify(List.to_tuple(list), div(len - 2, 2)) |> heapSort(len-1) |> Tuple.to_list end   defp heapSort(a, finish) when finish > 0 do swap(a, 0, finish) |> siftDown(0, finish-1) |> heapSort(finish-1) end defp 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 ...
#Dart
Dart
void main() { MergeSortInDart sampleSort = MergeSortInDart();   List<int> theResultingList = sampleSort.sortTheList([54, 89, 125, 47899, 32, 61, 42, 895647, 215, 345, 6, 21, 2, 78]);   print('Here\'s the sorted list: ${theResultingList}'); }   /////////////////////////////////////   class MergeSortInDart {   Li...
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 ...
#Swift
Swift
import Foundation   struct PancakeSort { var arr:[Int]   mutating func flip(n:Int) { for i in 0 ..< (n + 1) / 2 { swap(&arr[n - i], &arr[i]) } println("flip(0.. \(n)): \(arr)") }   func minmax(n:Int) -> [Int] { var xm = arr[0] var xM = arr[0] v...