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/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 ...
#Koka
Koka
fun qsort( xs : list<int> ) : div list<int> { match(xs) { Cons(x,xx) -> { val ys = xx.filter fn(el) { el < x } val zs = xx.filter fn(el) { el >= x } qsort(ys) + [x] + qsort(zs) } Nil -> Nil } }
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 ...
#Ksh
Ksh
#!/bin/ksh   # An insertion sort in ksh   # # Variables: # typeset -a arr=( 4 65 2 -31 0 99 2 83 782 1 )   # # Functions: #   # # Function _insertionSort(array) - Insersion sort of array of integers # function _insertionSort { typeset _arr ; nameref _arr="$1" typeset _i _j _val ; integer _i _j _val   for (( _i=1;...
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 ...
#Oz
Oz
declare proc {HeapSort A} Low = {Array.low A} High = {Array.high A} Count = High-Low+1   %% heapify LastParent = Low + (Count-2) div 2 in for Start in LastParent..Low;~1 do {Siftdown A Start High} end   %% repeatedly put the maximum element to the end %% and re-h...
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 ...
#JavaScript
JavaScript
function merge(left, right, arr) { var a = 0;   while (left.length && right.length) { arr[a++] = (right[0] < left[0]) ? right.shift() : left.shift(); } while (left.length) { arr[a++] = left.shift(); } while (right.length) { arr[a++] = right.shift(); } }   function mergeSort(arr) { var len = ...
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 ...
#VBScript
VBScript
' Soundex tt=array( _ "Ashcraft","Ashcroft","Gauss","Ghosh","Hilbert","Heilbronn","Lee","Lloyd", _ "Moses","Pfister","Robert","Rupert","Rubin","Tymczak","Soundex","Example") tv=array( _ "A261","A261","G200","G200","H416","H416","L000","L300", _ "M220","P236","R163","R163","R150","T522",...
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...
#Seed7
Seed7
$ include "seed7_05.s7i";   const func type: stack (in type: baseType) is func result var type: stackType is void; begin stackType := array baseType;   const proc: push (inout stackType: aStack, in baseType: top) is func begin aStack := [] (top) & aStack; end func;   const func ...
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 ...
#Kotlin
Kotlin
fun <E : Comparable<E>> List<E>.qsort(): List<E> = if (size < 2) this else filter { it < first() }.qsort() + filter { it == first() } + filter { it > first() }.qsort()  
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 ...
#Lambdatalk
Lambdatalk
  {def sort   {def sort.i {lambda {:x :a} {if {A.empty? :a} then {A.new :x} else {if {<= :x {A.first :a}} then {A.addfirst! :x :a} else {A.addfirst! {A.first :a} {sort.i :x {A.rest :a}}} }}}}   {def sort.r {lambda {:a1 :a2} {if {A.empty? :a1} then :a2 else {sort.r {A.rest :a1} {sort...
http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
Sorting algorithms/Heapsort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Pascal
Pascal
program HeapSortDemo;   type TIntArray = array[4..15] of integer;   var data: TIntArray; i: integer;   procedure siftDown(var a: TIntArray; start, ende: integer); var root, child, swap: integer; begin root := start; while root * 2 - start + 1 <= ende do begin child := root * 2 - start + ...
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 ...
#jq
jq
# Input: [x,y] -- the two arrays to be merged # If x and y are sorted as by "sort", then the result will also be sorted: def merge: def m: # state: [x, y, array] (array being the answer) .[0] as $x | .[1] as $y | if 0 == ($x|length) then .[2] + $y elif 0 == ($y|length) then .[2] + $x else ...
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 ...
#Wren
Wren
import "/str" for Char import "/fmt" for Fmt   var getCode = Fn.new { |c| return "BFPV".contains(c) ? "1" : "CGJKQSXZ".contains(c) ? "2" : c == "D" || c == "T" ? "3" : c == "L" ? "4" : c == "M" || c == "N" ? "5" : c == "R" ? ...
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...
#SenseTalk
SenseTalk
put () into stack repeat with each item of 1 .. 10 push it into stack end repeat   repeat while stack is not empty pop stack put it end repeat
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 ...
#Lambdatalk
Lambdatalk
  We create a binary tree from a random array, then we walk the canopy.   1) three functions for readability:   {def BT.data {lambda {:t} {A.get 0 :t}}} -> BT.data {def BT.left {lambda {:t} {A.get 1 :t}}} -> BT.left {def BT.right {lambda {:t} {A.get 2 :t}}} -> BT.right   2) adding a leaf to the tree:   {def...
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 ...
#Liberty_BASIC
Liberty BASIC
itemCount = 20 dim A(itemCount) for i = 1 to itemCount A(i) = int(rnd(1) * 100) next i   print "Before Sort" gosub [printArray]   '--- Insertion sort algorithm for i = 2 to itemCount value = A(i) j = i-1 while j >= 0 and A(j) > value A(j+1) = A(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 ...
#Perl
Perl
#!/usr/bin/perl   my @a = (4, 65, 2, -31, 0, 99, 2, 83, 782, 1); print "@a\n"; heap_sort(\@a); print "@a\n";   sub heap_sort { my ($a) = @_; my $n = @$a; for (my $i = ($n - 2) / 2; $i >= 0; $i--) { down_heap($a, $n, $i); } for (my $i = 0; $i < $n; $i++) { my $t = $a->[$n - $i - 1]; ...
http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort
Sorting algorithms/Merge sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Julia
Julia
function mergesort(arr::Vector) if length(arr) ≤ 1 return arr end mid = length(arr) ÷ 2 lpart = mergesort(arr[1:mid]) rpart = mergesort(arr[mid+1:end]) rst = similar(arr) i = ri = li = 1 @inbounds while li ≤ length(lpart) && ri ≤ length(rpart) if lpart[li] ≤ rpart[ri] rst...
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 ...
#XPL0
XPL0
code CrLf=9, Text=12; string 0; \use zero-terminated strings   func Soundex(S1); \Convert name to Soundex string (e.g: Rubin = R150) char S1; char S2(80), Tbl; int I, J, Char, Dig, Dig0; [ \abcdefghijklmnopqrstuvwxyz Tbl:= "01230120022455012623010202"; I:= 0; J:= 0; ...
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...
#Sidef
Sidef
var stack = []; stack.push(42); # pushing say stack.pop; # popping say stack.is_empty; # is_emtpy?
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 ...
#Lobster
Lobster
include "std.lobster"   def quicksort(xs, lt): if xs.length <= 1: xs else: pivot := xs[0] tail := xs.slice(1, -1) f1 := filter tail: lt(_, pivot) f2 := filter tail: !lt(_, pivot) append(append(quicksort(f1, lt), [ pivot ]), quicksort(f2, lt)...
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 ...
#Lua
Lua
do local function lower_bound(container, container_begin, container_end, value, comparator) local count = container_end - container_begin + 1   while count > 0 do local half = bit.rshift(count, 1) -- or math.floor(count / 2) local middle = container_begin + half   if comparator(container[middle], value) 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 ...
#Phix
Phix
with javascript_semantics function siftDown(sequence arr, integer s, integer last) integer root = s while root*2<=last do integer child = root*2 if child<last and arr[child]<arr[child+1] then child += 1 end if if arr[root]>=arr[child] then exit end if object tmp...
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 ...
#Kotlin
Kotlin
fun mergeSort(list: List<Int>): List<Int> { if (list.size <= 1) { return list }   val left = mutableListOf<Int>() val right = mutableListOf<Int>()   val middle = list.size / 2 list.forEachIndexed { index, number -> if (index < middle) { left.add(number) } else...
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...
#Slate
Slate
collections define: #Stack &parents: {ExtensibleArray}. "An abstraction over ExtensibleArray implementations to follow the stack protocol. The convention is that the Sequence indices run least-to-greatest from bottom to top."   s@(Stack traits) push: obj [s addLast: obj].   s@(Stack traits) pop [s removeLast].   s@(Sta...
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 ...
#Logo
Logo
; quicksort (lists, functional)   to small? :list output or [empty? :list] [empty? butfirst :list] end to quicksort :list if small? :list [output :list] localmake "pivot first :list output (sentence quicksort filter [? < :pivot] butfirst :list filter [? = :pivot]  :list quicksort f...
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 ...
#Maple
Maple
arr := Array([17,3,72,0,36,2,3,8,40,0]): len := numelems(arr): for i from 2 to len do val := arr[i]: j := i-1: while(j > 0 and arr[j] > val) do arr[j+1] := arr[j]: j--: end do: arr[j+1] := val: end do: arr;
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 ...
#Picat
Picat
main => _ = random2(), A = [random(-10,10) : _ in 1..30], println(A), heapSort(A), println(A).   heapSort(A) => heapify(A), End = A.len, while (End > 1) swap(A, End, 1), End := End - 1, siftDown(A, 1, End) end.   heapify(A) => Count = A.len, Start = Count // 2, while (Start >= 1) ...
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 ...
#PicoLisp
PicoLisp
(de heapSort (A Cnt) (let Cnt (length A) (for (Start (/ Cnt 2) (gt0 Start) (dec Start)) (siftDown A Start (inc Cnt)) ) (for (End Cnt (> End 1) (dec End)) (xchg (nth A End) A) (siftDown A 1 End) ) ) A )   (de siftDown (A Start End) (use Child (for (Root Start (> End...
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 ...
#Lambdatalk
Lambdatalk
  {def alt {lambda {:list} {if {A.empty? :list} then {A.new} else {A.addfirst! {A.first :list} {alt {A.rest {A.rest :list}}}} }}} -> alt   {def merge {lambda {:l1 :l2} {if {A.empty? :l2} then :l1 else {if {< {A.first :l1} {A.first :l2}} then {A.addfirst! {A.first :l1} {merge :l...
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...
#Smalltalk
Smalltalk
  s := Stack new. s push: 1. s push: 2. s push: 3. s pop. s top. "2"  
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 ...
#Logtalk
Logtalk
quicksort(List, Sorted) :- quicksort(List, [], Sorted).   quicksort([], Sorted, Sorted). quicksort([Pivot| Rest], Acc, Sorted) :- partition(Rest, Pivot, Smaller0, Bigger0), quicksort(Smaller0, [Pivot| Bigger], Sorted), quicksort(Bigger0, Acc, Bigger).   partition([], _, [], []). partition([X| Xs], Pivo...
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 ...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
insertionSort[a_List] := Module[{A = a}, For[i = 2, i <= Length[A], i++, value = A[[i]]; j = i - 1; While[j >= 1 && A[[j]] > value, A[[j + 1]] = A[[j]]; j--;]; A[[j + 1]] = value;]; A ]
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 ...
#PL.2FI
PL/I
*process source xref attributes or(!); /********************************************************************* * Pseudocode found here: * http://en.wikipedia.org/wiki/Heapsort#Pseudocode * Sample data from REXX * 27.07.2013 Walter Pachl *********************************************************************/ heap...
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 ...
#Liberty_BASIC
Liberty BASIC
itemCount = 20 dim A(itemCount) dim tmp(itemCount) 'merge sort needs additionally same amount of storage   for i = 1 to itemCount A(i) = int(rnd(1) * 100) next i   print "Before Sort" call printArray itemCount   call mergeSort 1,itemCount   print "After Sort" call prin...
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...
#Standard_ML
Standard ML
signature STACK = sig type 'a stack exception EmptyStack   val empty : 'a stack val isEmpty : 'a stack -> bool   val push : ('a * 'a stack) -> 'a stack val pop  : 'a stack -> 'a stack val top  : 'a stack -> 'a val popTop : 'a stack -> 'a stack * 'a   val map : ('a -> 'b) -> 'a stack ...
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 ...
#Lua
Lua
table.sort(tableName)
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 ...
#MATLAB_.2F_Octave
MATLAB / Octave
function list = insertionSort(list)   for i = (2:numel(list))   value = list(i); j = i - 1;   while (j >= 1) && (list(j) > value) list(j+1) = list(j); j = j-1; end   list(j+1) = value;   end %for end %insertionSort
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 ...
#PL.2FM
PL/M
100H:   /* HEAP SORT AN ARRAY OF 16-BIT INTEGERS */ HEAP$SORT: PROCEDURE (AP, COUNT); SIFT$DOWN: PROCEDURE (AP, START, ENDV); DECLARE (AP, A BASED AP) ADDRESS; DECLARE (START, ENDV, ROOT, CHILD, TEMP) ADDRESS; ROOT = START;   DO WHILE (CHILD := SHL(ROOT,1) + 1) <= ENDV; I...
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 ...
#Logo
Logo
to split :size :front :list if :size < 1 [output list :front :list] output split :size-1 (lput first :list :front) (butfirst :list) end   to merge :small :large if empty? :small [output :large] ifelse lessequal? first :small first :large ~ [output fput first :small merge butfirst :small :large] ~ [outpu...
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...
#Stata
Stata
struct Stack<T> { var items = [T]() var empty:Bool { return items.count == 0 }   func peek() -> T { return items[items.count - 1] }   mutating func pop() -> T { return items.removeLast() }   mutating func push(obj:T) { items.append(obj) } }   var stack...
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 ...
#Lucid
Lucid
qsort(a) = if eof(first a) then a else follow(qsort(b0),qsort(b1)) fi where p = first a < a; b0 = a whenever p; b1 = a whenever not p; follow(x,y) = if xdone then y upon xdone else x fi where xdone = iseod x fby xdone or iseod x; end; end
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 ...
#Maxima
Maxima
insertion_sort(u) := block( [n: length(u), x, j], for i from 2 thru n do ( x: u[i], j: i - 1, while j >= 1 and u[j] > x do ( u[j + 1]: u[j], j: j - 1 ), u[j + 1]: x ) )$
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 ...
#PowerShell
PowerShell
  function heapsort($a, $count) { $a = heapify $a $count $end = $count - 1 while( $end -gt 0) { $a[$end], $a[0] = $a[0], $a[$end] $end-- $a = siftDown $a 0 $end } $a } function heapify($a, $count) { $start = [Math]::Floor(($count - 2) / 2) while($start -ge 0) { $a = sift...
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 ...
#Logtalk
Logtalk
msort([], []) :- !. msort([X], [X]) :- !. msort([X, Y| Xs], Ys) :- split([X, Y| Xs], X1s, X2s), msort(X1s, Y1s), msort(X2s, Y2s), merge(Y1s, Y2s, Ys).   split([], [], []). split([X| Xs], [X| Ys], Zs) :- split(Xs, Zs, Ys).   merge([X| Xs], [Y| Ys], [X| Zs]) :- X @=< Y, !, merge(Xs, [Y| Ys], Z...
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...
#Swift
Swift
struct Stack<T> { var items = [T]() var empty:Bool { return items.count == 0 }   func peek() -> T { return items[items.count - 1] }   mutating func pop() -> T { return items.removeLast() }   mutating func push(obj:T) { items.append(obj) } }   var stack...
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 ...
#M2000_Interpreter
M2000 Interpreter
  Module Checkit1 { Group Quick { Private: Function partition { Read &A(), p, r x = A(r) i = p-1 For j=p to r-1 { If .LE(A(j), x) Then { 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 ...
#MAXScript
MAXScript
  fn inSort arr = ( arr = deepcopy arr for i = 1 to arr.count do ( j = i while j > 1 and arr[j-1] > arr[j] do ( swap arr[j] arr[j-1] j -= 1 ) ) return arr )  
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 ...
#PureBasic
PureBasic
Declare heapify(Array a(1), count) Declare siftDown(Array a(1), start, ending)   Procedure heapSort(Array a(1), count) Protected ending=count-1 heapify(a(), count) While ending>0 Swap a(ending),a(0) siftDown(a(), 0, ending-1) ending-1 Wend EndProcedure   Procedure heapify(Array a(1), count) Protec...
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 ...
#Lua
Lua
local function merge(left_container, left_container_begin, left_container_end, right_container, right_container_begin, right_container_end, result_container, result_container_begin, comparator) while left_container_begin <= left_container_end do if right_container_begin > right_container_end then for i = left_con...
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...
#Tailspin
Tailspin
  processor Stack @: $;   sink push ..|@Stack: $; end push   source peek $@Stack(last) ! end peek   source pop ^@Stack(last) ! end pop   source empty $@Stack::length -> # <=0> 1 ! <> 0 ! end empty end Stack   def myStack: [1] -> Stack;   2 -> !myStack::push   '$myStack::empty; ...
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 ...
#M4
M4
dnl return the first element of a list when called in the funny way seen below define(`arg1', `$1')dnl dnl dnl append lists 1 and 2 define(`append', `ifelse(`$1',`()', `$2', `ifelse(`$2',`()', `$1', `substr($1,0,decr(len($1))),substr($2,1)')')')dnl dnl dnl separate list 2 based on pi...
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 ...
#ML
ML
fun insertion_sort L = let fun insert (x,[]) = [x] | (x, y :: ys) = if x <= y then x :: y :: ys else y :: insert (x, ys) in foldr (insert,[]) L end;   println ` insertion_sort [6,8,5,9,3,2,1,4,7];  
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 ...
#Python
Python
def heapsort(lst): ''' Heapsort. Note: this function sorts in-place (it mutates the list). '''   # in pseudo-code, heapify only called once, so inline it here for start in range((len(lst)-2)/2, -1, -1): siftdown(lst, start, len(lst)-1)   for end in range(len(lst)-1, 0, -1): lst[end], lst[0] = lst[0], ls...
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 ...
#Lucid
Lucid
msort(a) = if iseod(first next a) then a else merge(msort(b0),msort(b1)) fi where p = false fby not p; b0 = a whenever p; b1 = a whenever not p; just(a) = ja where ja = a fby if iseod ja then eod else next a fi; end; merge(x,y) = if takexx then xx else yy fi where xx = (...
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...
#Tcl
Tcl
proc push {stackvar value} { upvar 1 $stackvar stack lappend stack $value } proc pop {stackvar} { upvar 1 $stackvar stack set value [lindex $stack end] set stack [lrange $stack 0 end-1] return $value } proc size {stackvar} { upvar 1 $stackvar stack llength $stack } proc empty {stackvar} ...
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 ...
#Maclisp
Maclisp
  ;; While not strictly required, it simplifies the ;; implementation considerably to use filter. MACLisp ;; Doesn't have one out of the box, so we bring our own (DEFUN FILTER (F LIST) (COND ((EQ LIST NIL) NIL) ((FUNCALL F (CAR LIST)) (CONS (CAR LIST) (FILTER F (CDR LIST)))) ...
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 ...
#Modula-3
Modula-3
MODULE InsertSort;   PROCEDURE IntSort(VAR item: ARRAY OF INTEGER) = VAR j, value: INTEGER; BEGIN FOR i := FIRST(item) + 1 TO LAST(item) DO value := item[i]; j := i - 1; WHILE j >= FIRST(item) AND item[j] > value DO item[j + 1] := item[j]; DEC(j); END; item[j + 1] :...
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 ...
#Quackery
Quackery
[ [] swap pqwith > dup pqsize times [ frompq rot join swap ] drop ] is hsort ( [ --> [ )   [] 23 times [ 90 random 10 + join ] say " " dup echo cr say " --> " hsort echo
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 ...
#Racket
Racket
  #lang racket (require (only-in srfi/43 vector-swap!))   (define (heap-sort! xs) (define (ref i) (vector-ref xs i)) (define (swap! i j) (vector-swap! xs i j)) (define size (vector-length xs))   (define (sift-down! r end) (define c (+ (* 2 r) 1)) (define c+1 (+ c 1)) (when (<= c end) (define 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 ...
#M2000_Interpreter
M2000 Interpreter
  module checkit { \\ merge sort group merge { function sort(right as stack) { if len(right)<=1 then =right : exit left=.sort(stack up right, len(right) div 2 ) right=.sort(right) \\ stackitem(right) is same as stackitem(right,1) if stackitem(left, len(left))<=stackitem(right) then \\ !left take ...
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...
#UnixPipes
UnixPipes
init() { if [ -e stack ]; then rm stack; fi } # force pop to blow up if empty push() { echo $1 >> stack; } pop() { tail -1 stack; x=`head -n -1 stack | wc -c` if [ $x -eq '0' ]; then rm stack; else truncate -s `head -n -1 stack | wc -c` stack fi } empty() { head -n -1 stack |wc -l; } stack_top() { tail -1 stack; ...
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 ...
#Maple
Maple
swap := proc(arr, a, b) local temp := arr[a]: arr[a] := arr[b]: arr[b] := temp: end proc: quicksort := proc(arr, low, high) local pi: if (low < high) then pi := qpart(arr,low,high): quicksort(arr, low, pi-1): quicksort(arr, pi+1, high): end if: end proc: qpart := proc(arr, low, high) local i,j,pivot; pivo...
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 ...
#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 . ds out " . while \\\\n+i<=\\\\n[\\$1.c] .as out "\\\\n[\\$1..\\\\n...
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 ...
#Raku
Raku
sub heap_sort ( @list ) { for ( 0 ..^ +@list div 2 ).reverse -> $start { _sift_down $start, @list.end, @list; }   for ( 1 ..^ +@list ).reverse -> $end { @list[ 0, $end ] .= reverse; _sift_down 0, $end-1, @list; } }   sub _sift_down ( $start, $end, @list ) { my $root = $start;...
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 ...
#Maple
Maple
merge := proc(arr, left, mid, right) local i, j, k, n1, n2, L, R; n1 := mid-left+1: n2 := right-mid: L := Array(1..n1): R := Array(1..n2): for i from 0 to n1-1 do L(i+1) :=arr(left+i): end do: for j from 0 to n2-1 do R(j+1) := arr(mid+j+1): end do: i := 1: j := 1: k := left: while(i <= n1 and j <= n2)...
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...
#UNIX_Shell
UNIX Shell
init() { if [[ -n $KSH_VERSION ]]; then set -A stack else stack=(); # this sets stack to '()' in ksh fi }   push() { stack=("$1" "${stack[@]}") }   stack_top() { # this approach sidesteps zsh indexing difference set -- "${stack[@]}" printf '%s\n' "$1" }   pop() { stack_top stack=("${stack[@]:...
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 ...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
QuickSort[x_List] := Module[{pivot}, If[Length@x <= 1, Return[x]]; pivot = RandomChoice@x; Flatten@{QuickSort[Cases[x, j_ /; j < pivot]], Cases[x, j_ /; j == pivot], QuickSort[Cases[x, j_ /; j > pivot]]} ]
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 ...
#Nanoquery
Nanoquery
def insertion_sort(L) for i in range(1, len(L) - 1) j = i - 1 key = L[i] while (L[j] > key) and (j >= 0) L[j + 1] = L[j] j -= 1 end L[j+1] = key end   return L 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 ...
#REXX
REXX
/*REXX pgm sorts an array (names of epichoric Greek letters) using a heapsort algorithm.*/ parse arg x; call init /*use args or default, define @ array.*/ call show "before sort:" /*#: the number of elements in array*/ call heapSort #; say copies('▒', 40...
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 ...
#Mathematica_.2F_Wolfram_Language
Mathematica / Wolfram Language
MergeSort[m_List] := Module[{middle}, If[Length[m] >= 2, middle = Ceiling[Length[m]/2]; Apply[Merge, Map[MergeSort, Partition[m, middle, middle, {1, 1}, {}]]], m ] ]   Merge[left_List, right_List] := Module[ {leftIndex = 1, rightIndex = 1}, Table[ Which[ leftIndex > Length[left], right[[...
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...
#VBA
VBA
'Simple Stack class 'uses a dynamic array of Variants to stack the values 'has read-only property "Size" 'and methods "Push", "Pop", "IsEmpty" Private myStack() Private myStackHeight As Integer   'method Push Public Function Push(aValue) 'increase stack height myStackHeight = myStackHeight + 1 ReDim Preserve my...
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 ...
#MATLAB
MATLAB
function sortedArray = quickSort(array)   if numel(array) <= 1 %If the array has 1 element then it can't be sorted sortedArray = array; return end   pivot = array(end); array(end) = [];   %Create two new arrays which contain the elements that are less than or %equal to the...
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 ...
#Nemerle
Nemerle
using System.Console; using Nemerle.English;   module InsertSort { public static Sort(this a : array[int]) : void { mutable value = 0; mutable j = 0; foreach (i in [1 .. (a.Length - 1)]) { value = a[i]; j = i - 1; while (j >= 0 and a[j] > 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 ...
#Ring
Ring
  # Project : Sorting algorithms/Heapsort   test = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1] see "before sort:" + nl showarray(test) heapsort(test) see "after sort:" + nl showarray(test)   func heapsort(a) cheapify(a) for e = len(a) to 1 step -1 temp = a[e] a[e] = a[1] a[1] = temp siftdown(a, 1, e-1) ne...
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 ...
#MATLAB
MATLAB
function list = mergeSort(list)   if numel(list) <= 1 return else middle = ceil(numel(list) / 2); left = list(1:middle); right = list(middle+1:end);   left = mergeSort(left); right = mergeSort(right);   if left(end) <= right(1) list = [left rig...
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...
#VBScript
VBScript
class stack dim tos dim stack() dim stacksize   private sub class_initialize stacksize = 100 redim stack( stacksize ) tos = 0 end sub   public sub push( x ) stack(tos) = x tos = tos + 1 end sub   public property get stackempty stackempty = ( tos = 0 ) end property   public property get stackfull ...
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 ...
#MAXScript
MAXScript
fn quickSort arr = ( less = #() pivotList = #() more = #() if arr.count <= 1 then ( arr ) else ( pivot = arr[arr.count/2] for i in arr do ( case of ( (i < pivot): (append less i) (i == pivot): (append...
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 ...
#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/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 ...
#Ruby
Ruby
class Array def heapsort self.dup.heapsort! end   def heapsort! # in pseudo-code, heapify only called once, so inline it here ((length - 2) / 2).downto(0) {|start| siftdown(start, length - 1)}   # "end" is a ruby keyword (length - 1).downto(1) do |end_| self[end_], self[0] = self[0], sel...
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 ...
#Maxima
Maxima
merge(a, b) := block( [c: [ ], i: 1, j: 1, p: length(a), q: length(b)], while i <= p and j <= q do ( if a[i] < b[j] then ( c: endcons(a[i], c), i: i + 1 ) else ( c: endcons(b[j], c), j: j + 1 ) ), if i > p then append(c, rest(b, j - 1)) else append(c, re...
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...
#Vlang
Vlang
const ( max_depth = 256 )   struct Stack { mut: data []f32 = []f32{len: max_depth} depth int }   fn (mut s Stack) push(v f32) { if s.depth >= max_depth { return } println('Push: ${v:3.2f}') s.data[s.depth] = v s.depth++ }   fn (mut s Stack) pop() ?f32 { if s.depth > 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 ...
#Mercury
Mercury
%%%-------------------------------------------------------------------   :- module quicksort_task_for_lists.   :- interface. :- import_module io. :- pred main(io, io). :- mode main(di, uo) is det.   :- implementation. :- import_module int. :- import_module list.   %%%----------------------------------------------------...
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 ...
#Nim
Nim
proc insertSort[T](a: var openarray[T]) = for i in 1 .. a.high: let value = a[i] var j = i while j > 0 and value < a[j-1]: a[j] = a[j-1] dec j a[j] = value   var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] insertSort a echo a
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 ...
#Rust
Rust
fn main() { let mut v = [4, 6, 8, 1, 0, 3, 2, 2, 9, 5]; heap_sort(&mut v, |x, y| x < y); println!("{:?}", v); }   fn heap_sort<T, F>(array: &mut [T], order: F) where F: Fn(&T, &T) -> bool, { let len = array.len(); // Create heap for start in (0..len / 2).rev() { shift_down(array, &or...
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 ...
#Scala
Scala
def heapSort[T](a: Array[T])(implicit ord: Ordering[T]) { import scala.annotation.tailrec // Ensure functions are tail-recursive import ord._   val indexOrdering = Ordering by a.apply   def numberOfLeaves(heapSize: Int) = (heapSize + 1) / 2   def children(i: Int, heapSize: Int) = { val leftChild = i * 2 +...
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 ...
#MAXScript
MAXScript
fn mergesort arr = ( local left = #() local right = #() local result = #() if arr.count < 2 then return arr else ( local mid = arr.count/2 for i = 1 to mid do ( append left arr[i] ) for i = (mid+1) to arr.count do ( append right arr[i] ) left = mergesort left right = mergesort right if l...
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...
#Wart
Wart
def (stack) (tag 'stack nil)   mac (push! x s) :qcase `(isa stack ,s) `(push! ,x (rep ,s))   mac (pop! s) :qcase `(isa stack ,s) `(pop! (rep ,s))   def (empty? s) :case (isa stack s) (empty? rep.s)
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 ...
#Modula-2
Modula-2
(*#####################*) DEFINITION MODULE QSORT; (*#####################*)   FROM SYSTEM IMPORT ADDRESS;   TYPE CmpFuncPtrs = PROCEDURE(ADDRESS, ADDRESS):INTEGER;   PROCEDURE QuickSortPtrs(VAR Array:ARRAY OF ADDRESS; N:CARDINAL; Compare:CmpFuncPtrs); END QSORT.  
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 ...
#Objeck
Objeck
  bundle Default { class Insert { function : Main(args : String[]) ~ Nil { values := [9, 7, 10, 2, 9, 7, 4, 3, 10, 2, 7, 10]; InsertionSort(values); each(i : values) { values[i]->PrintLine(); }; }   function : InsertionSort (a : Int[]) ~ Nil { each(i : a) { va...
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 ...
#Scheme
Scheme
; swap two elements of a vector (define (swap! v i j) (define temp (vector-ref v i)) (vector-set! v i (vector-ref v j)) (vector-set! v j temp))   ; sift element at node start into place (define (sift-down! v start end) (let ((child (+ (* start 2) 1))) (cond ((> child end) 'done) ; start has no childre...
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 ...
#Mercury
Mercury
  :- module merge_sort.   :- interface.   :- import_module list.   :- type split_error ---> split_error.   :- func merge_sort(list(T)) = list(T). :- pred merge_sort(list(T)::in, list(T)::out) is det.   :- implementation.   :- import_module int, exception.   merge_sort(U) = S :- merge_sort(U, S).   merge_sort(U, S) :- m...
http://rosettacode.org/wiki/Stack
Stack
Data Structure This illustrates a data structure, a means of storing data within a program. You may see other such structures in the Data Structures category. A stack is a container of elements with   last in, first out   access policy.   Sometimes it also called LIFO. The stack is accessed through its top. The ba...
#Wren
Wren
import "/seq" for Stack   var s = Stack.new() s.push(1) s.push(2) System.print("Stack contains %(s.toList)") System.print("Number of elements in stack = %(s.count)") var item = s.pop() System.print("'%(item)' popped from the stack") System.print("Last element is now %(s.peek())") s.clear() System.print("Stack cleared")...
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 ...
#Modula-3
Modula-3
GENERIC INTERFACE ArraySort(Elem);   PROCEDURE Sort(VAR a: ARRAY OF Elem.T; cmp := Elem.Compare);   END ArraySort.
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 ...
#OCaml
OCaml
let rec insert lst x = match lst with [] -> [x] | y :: ys when x <= y -> x :: y :: ys | y :: ys -> y :: insert ys x   ;; let insertion_sort = List.fold_left insert [];;   insertion_sort [6;8;5;9;3;2;1;4;7];;
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 ...
#Seed7
Seed7
const proc: downheap (inout array elemType: arr, in var integer: k, in integer: n) is func local var elemType: help is elemType.value; var integer: j is 0; begin if k <= n div 2 then help := arr[k]; repeat j := 2 * k; if j < n and arr[j] < arr[succ(j)] then incr(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 ...
#SequenceL
SequenceL
  import <Utilities/Sequence.sl>;   TUPLE<T> ::= (A: T, B: T);   heapSort(x(1)) := let heapified := heapify(x, (size(x) - 2) / 2 + 1); in sortLoop(heapified, size(heapified));   heapify(x(1), i) := x when i <= 0 else heapify(siftDown(x, i, size(x)), i - 1);   sortLoop(x(1), i) := x when i <= 2 else sortLoop...
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 ...
#Modula-2
Modula-2
  DEFINITION MODULE MSIterat;   PROCEDURE IterativeMergeSort( VAR a : ARRAY OF INTEGER);   END MSIterat.  
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...
#X86_Assembly
X86 Assembly
  ; x86_64 linux nasm   struc Stack maxSize: resb 8 currentSize: resb 8 contents: endStruc   section .data   soError: db "Stack Overflow Exception", 10 seError: db "Stack Empty Error", 10     section .text   createStack: ; IN: max number of elements (rdi) ; OUT: pointer to new stack (rax) push rdi xor rdx, rd...
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 ...
#Mond
Mond
fun quicksort( arr, cmp ) { if( arr.length() < 2 ) return arr;   if( !cmp ) cmp = ( a, b ) -> a - b;   var a = [ ], b = [ ]; var pivot = arr[0]; var len = arr.length();   for( var i = 1; i < len; ++i ) { var item = arr[i];   if( cmp( item, pivot ) < cmp( pivot...
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 ...
#Oforth
Oforth
: insertionSort(a) | l i j v | a asListBuffer ->l 2 l size for: i [ l at(i) ->v i 1- ->j while(j) [ l at(j) dup v <= ifTrue: [ drop break ] j 1+ swap l put j 1- ->j ] l put(j 1 +, v) ] l ;
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 ...
#Sidef
Sidef
func sift_down(a, start, end) { var root = start; while ((2*root + 1) <= end) { var child = (2*root + 1); if ((child+1 <= end) && (a[child] < a[child + 1])) { child += 1; } if (a[root] < a[child]) { a[child, root] = a[root, child]; root = child...