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/Sort_an_array_of_composite_structures
Sort an array of composite structures
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Action.21
Action!
DEFINE PTR="CARD" DEFINE PAIR_SIZE="4" DEFINE PAIR_COUNT="1"   TYPE Pair=[PTR name,value]   BYTE ARRAY pairs(100) BYTE count=[0]   PTR FUNC GetItemAddr(INT index) PTR addr   addr=pairs+index*PAIR_SIZE RETURN (addr)   PROC PrintArray() INT i Pair POINTER p   Put('[) FOR i=0 TO count-1 DO IF i>0 THEN Pu...
http://rosettacode.org/wiki/Sort_an_array_of_composite_structures
Sort an array of composite structures
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Ada
Ada
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Text_IO; use Ada.Text_IO;   with Ada.Containers.Generic_Array_Sort;   procedure Demo_Array_Sort is   function "+" (S : String) return Unbounded_String renames To_Unbounded_String;   type A_Composite is record Name  : Unbounde...
http://rosettacode.org/wiki/Sorting_algorithms/Counting_sort
Sorting algorithms/Counting sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PowerShell
PowerShell
  function countingSort($array) { $minmax = $array | Measure-Object -Minimum -Maximum $min, $max = $minmax.Minimum, $minmax.Maximum $count = @(0) * ($max - $min + 1) foreach ($number in $array) { $count[$number - $min] = $count[$number - $min] + 1 } $z = 0 foreach ($i in $min..$max...
http://rosettacode.org/wiki/Solve_a_Numbrix_puzzle
Solve a Numbrix puzzle
Numbrix puzzles are similar to Hidato. The most important difference is that it is only possible to move 1 node left, right, up, or down (sometimes referred to as the Von Neumann neighborhood). Published puzzles also tend not to have holes in the grid and may not always indicate the end node. Two examples follow: Exam...
#C.2B.2B
C++
  #include <vector> #include <sstream> #include <iostream> #include <iterator> #include <cstdlib> #include <string> #include <bitset>   using namespace std; typedef bitset<4> hood_t;   struct node { int val; hood_t neighbors; };   class nSolver { public:   void solve(vector<string>& puzz, int max_wid) { if (puzz....
http://rosettacode.org/wiki/Sort_an_integer_array
Sort an integer array
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#AWK
AWK
  # syntax: GAWK -f SORT_AN_INTEGER_ARRAY.AWK BEGIN { split("9,10,3,1234,99,1,200,2,0,-2",arr,",") show("@unsorted","unsorted") show("@val_num_asc","sorted ascending") show("@val_num_desc","sorted descending") exit(0) } function show(sequence,description, i) { PROCINFO["sorted_in"] = sequence ...
http://rosettacode.org/wiki/Sort_a_list_of_object_identifiers
Sort a list of object identifiers
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#C.23
C#
using System; using System.Linq; using System.Collections.Generic;   public class Program { public static void Main() { var oids = new [] { "1.3.6.1.4.1.11.2.17.19.3.4.0.10", "1.3.6.1.4.1.11.2.17.5.2.0.79", "1.3.6.1.4.1.11.2.17.19.3.4.0.4", "1.3.6.1.4.1.11150....
http://rosettacode.org/wiki/Sort_disjoint_sublist
Sort disjoint sublist
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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.algorithm, std.range, std.array;   void main() { auto data = [7, 6, 5, 4, 3, 2, 1, 0]; auto indices = [6, 1, 7];   data.indexed(indices.sort()).sort();   assert(data == [7, 0, 5, 4, 3, 2, 1, 6]); }
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
  Module Stable { Inventory queue alfa Stack New { Data "UK", "London","US", "New York","US", "Birmingham", "UK","Birmingham" While not empty { Append alfa, Letter$:=letter$ } } sort alfa k=Each(alfa) Document A$ NL$={ ...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 Stable { Inventory queue alfa Stack New { Data "UK", "London","US", "New York","US", "Birmingham", "UK","Birmingham" While not empty { Append alfa, Letter$:=letter$ } } sort alfa k=Each(alfa) Document A$ NL$={ ...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
mylist = {{1, 2, 3}, {4, 5, 6}, {5, 4, 3}, {9, 5, 1}}; Sort[mylist, (#1[[2]] < #2[[2]]) &] #[[Ordering[#[[All, 2]]]]] &[mylist]
http://rosettacode.org/wiki/Sort_numbers_lexicographically
Sort numbers lexicographically
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 lex_order (n) { [range(1, n, n.sgn)...].sort_by { Str(_) } }   [13, 21, -22].each {|n| printf("%4s: %s\n", n, lex_order(n)) }
http://rosettacode.org/wiki/Sort_numbers_lexicographically
Sort numbers lexicographically
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 lex(n: Int) -> [Int] { return stride(from: 1, through: n, by: n.signum()).map({ String($0) }).sorted().compactMap(Int.init) }   print("13: \(lex(n: 13))") print("21: \(lex(n: 21))") print("-22: \(lex(n: -22))")
http://rosettacode.org/wiki/Sort_numbers_lexicographically
Sort numbers lexicographically
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
proc iota {num {start 0} {step 1}} { set res {} set end [+ $start [* $step $num]] for {set n $start} {$n != $end} {incr n $step} { lappend res $n } return $res }   puts [lsort [iota 13 1]]
http://rosettacode.org/wiki/Sort_three_variables
Sort three variables
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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   # Sort three variables that may contain any value (numbers and/or literals)   # # Variables: # xl='lions, tigers, and' yl='bears, oh my!' zl='(from the "Wizard of OZ")'   typeset -i xn=77444 typeset -F yn=-12.0 typeset -i zn=0   # # Functions: #   # # Function _intoarray(x, y, z, arr) - put 3 variables i...
http://rosettacode.org/wiki/Sort_three_variables
Sort three variables
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Little_Man_Computer
Little Man Computer
  // Little Man Computer // Sort x, y, z, in ascending order // Based on a sorting network: // if x > z then swap( x, z) // if x > y then swap( x, y) // if y > z then swap( y, z) // with the addition that if the 2nd swap is executed // then the 3rd comparison is not needed.   // Read numbers x, y, z, and display ...
http://rosettacode.org/wiki/Sort_using_a_custom_comparator
Sort using a custom comparator
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#FunL
FunL
def preceeds( a, b ) = b.length() < a.length() or b.length() == a.length() and a.compareToIgnoreCase( b ) < 0   println( ["here", "are", "Some", "sample", "strings", "to", "be", "sorted"].sortWith(preceeds) )
http://rosettacode.org/wiki/Sort_using_a_custom_comparator
Sort using a custom comparator
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#F.C5.8Drmul.C3.A6
Fōrmulæ
package main   import ( "fmt" "sort" "strings" )   type sortable []string   func (s sortable) Len() int { return len(s) } func (s sortable) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s sortable) Less(i, j int) bool { a, b := s[i], s[j] if len(a) != len(b) { return len(a) > len(b) ...
http://rosettacode.org/wiki/Sorting_algorithms/Comb_sort
Sorting algorithms/Comb sort
Sorting algorithms/Comb sort You are encouraged to solve this task according to the task description, using any language you may know. Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algor...
#REXX
REXX
/*REXX program sorts and displays a stemmed array using the comb sort algorithm. */ call gen /*generate the @ array elements. */ call show 'before sort' /*display the before array elements. */ say copies('▒', 60...
http://rosettacode.org/wiki/Sorting_algorithms/Bogosort
Sorting algorithms/Bogosort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 bogosort (Lst) (loop (map '((L) (rot L (rand 1 (length L)))) Lst ) (T (apply <= Lst) Lst) ) )
http://rosettacode.org/wiki/Sorting_algorithms/Bogosort
Sorting algorithms/Bogosort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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; bogosort: Proc Options(main); Dcl SYSPRINT Print; Dcl (HBOUND,RANDOM,TIME) Builtin; Dcl tim Pic'(9)9'; Dcl timms Pic'(3)9' def tim pos(7); tim=time(); x=random(timms); Dcl a(5) Dec Fixed(5,1) Init(-21,333,0,444.4,1); Dcl (x,y,temp) Dec Fixed(5,1); Dcl (n,bogo,j,u,v) Bin Fixed(31); ...
http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort
Sorting algorithms/Bubble 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
List<num> bubbleSort(List<num> list) { var retList = new List<num>.from(list); var tmp; var swapped = false; do { swapped = false; for(var i = 1; i < retList.length; i++) { if(retList[i - 1] > retList[i]) { tmp = retList[i - 1]; retList[i - 1] = retList[i]; retList[i] = tmp...
http://rosettacode.org/wiki/Sorting_algorithms/Gnome_sort
Sorting algorithms/Gnome 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
// version 1.1.0   fun <T: Comparable<T>> gnomeSort(a: Array<T>, ascending: Boolean = true) { var i = 1 var j = 2 while (i < a.size) if (ascending && (a[i - 1] <= a[i]) || !ascending && (a[i - 1] >= a[i])) i = j++ else { val temp = a[i - 1] a[i...
http://rosettacode.org/wiki/Sorting_algorithms/Bead_sort
Sorting algorithms/Bead sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PowerShell
PowerShell
Function BeadSort ( [Int64[]] $indata ) { if( $indata.length -gt 1 ) { $min = $indata[ 0 ] $max = $indata[ 0 ] for( $i = 1; $i -lt $indata.length; $i++ ) { if( $indata[ $i ] -lt $min ) { $min = $indata[ $i ] } if( $indata[ $i ] -gt $max ) { $max = $indata[ $i ] } } #Find the min & max...
http://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort
Sorting algorithms/Cocktail sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#J
J
bigToLeft=: (([ (>. , <.) {.@]) , }.@])/ smallToLeft=: (([ (<. , >.) {.@]) , }.@])/ cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)
http://rosettacode.org/wiki/Solve_a_Hopido_puzzle
Solve a Hopido puzzle
Hopido puzzles are similar to Hidato. The most important difference is that the only moves allowed are: hop over one tile diagonally; and over two tiles horizontally and vertically. It should be possible to start anywhere in the path, the end point isn't indicated and there are no intermediate clues. Hopido Design Pos...
#AutoHotkey
AutoHotkey
SolveHopido(Grid, Locked, Max, row, col, num:=1, R:="", C:=""){ if (R&&C) ; if neighbors (not first iteration) { Grid[R, C] := ">" num ; place num in current neighbor and mark it visited ">" row:=R, col:=C ; move to current neighbor }   num++ ; increment num if (num=max) ; if reach...
http://rosettacode.org/wiki/Sort_an_array_of_composite_structures
Sort an array of composite structures
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#ALGOL_68
ALGOL 68
MODE SORTSTRUCT = PERSON; OP < = (PERSON a,b)BOOL: age OF a < age OF b; PR READ "prelude/sort.a68" PR;   MODE PERSON = STRUCT (STRING name, INT age); FORMAT person repr = $"Name: "g", Age: "g(0)l$;   []SORTSTRUCT person = (("joe", 120), ("foo", 31), ("bar", 51)); printf((person repr, shell sort(person), $l$))
http://rosettacode.org/wiki/Sorting_algorithms/Counting_sort
Sorting algorithms/Counting sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PureBasic
PureBasic
Procedure Counting_sort(Array data_array(1), min, max) Define i, j Dim c(max - min)   For i = 0 To ArraySize(data_array()) c(data_array(i) - min) + 1 Next   For i = 0 To ArraySize(c()) While c(i) data_array(j) = i + min j + 1 c(i) - 1 Wend Next EndProcedure
http://rosettacode.org/wiki/Solve_the_no_connection_puzzle
Solve the no connection puzzle
You are given a box with eight holes labelled   A-to-H,   connected by fifteen straight lines in the pattern as shown below: A B /│\ /│\ / │ X │ \ / │/ \│ \ C───D───E───F \ │\ /│ / \ │ X │ / \│/ \│/ G H You ar...
#11l
11l
V connections = [(0, 2), (0, 3), (0, 4), (1, 3), (1, 4), (1, 5), (6, 2), (6, 3), (6, 4), (7, 3), (7, 4), (7, 5), (2, 3), (3, 4), (4, 5)]   F ok(conn, perm) R abs(perm[conn[0]] - perm[conn[1]]) != 1   F solve() [[Int]] r V perm = Array(1..8) ...
http://rosettacode.org/wiki/Solve_a_Numbrix_puzzle
Solve a Numbrix puzzle
Numbrix puzzles are similar to Hidato. The most important difference is that it is only possible to move 1 node left, right, up, or down (sometimes referred to as the Von Neumann neighborhood). Published puzzles also tend not to have holes in the grid and may not always indicate the end node. Two examples follow: Exam...
#D
D
import std.stdio, std.conv, std.string, std.range, std.array, std.typecons, std.algorithm;   struct { alias BitSet8 = ubyte; // A set of 8 bits. alias Cell = uint; enum : string { unavailableInCell = "#", availableInCell = "." } enum : Cell { unavailableCell = Cell.max, availableCell = 0 }   this(i...
http://rosettacode.org/wiki/Sort_an_integer_array
Sort an integer array
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Axe
Axe
2→{L₁} 4→{L₁+1} 3→{L₁+2} 1→{L₁+3} 2→{L₁+4}   SortD(L₁,5)
http://rosettacode.org/wiki/Sort_an_integer_array
Sort an integer array
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Babel
Babel
babel> nil { zap {1 randlf 100 rem} 20 times collect ! } nest dup lsnum ! --> Create a list of random numbers ( 20 47 69 71 18 10 92 9 56 68 71 92 45 92 12 7 59 55 54 24 ) babel> ls2lf --> Convert list to array for sorting babel> dup {fnord} merge_sort ...
http://rosettacode.org/wiki/Sort_a_list_of_object_identifiers
Sort a list of object identifiers
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#C.2B.2B
C++
#include <string> #include <vector> #include <algorithm> #include <boost/tokenizer.hpp> #include <iostream>   std::vector<std::string> splitOnChar ( std::string & s , const char c ) { typedef boost::tokenizer<boost::char_separator<char>> tokenizer ; std::vector<std::string> parts ; boost::char_separator<char> ...
http://rosettacode.org/wiki/Sort_disjoint_sublist
Sort disjoint sublist
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
  (define (sort-disjoint values indices) (define sorted (list-sort < (for/list [(v values) (i (in-naturals))] #:when (member i indices) v)))   (for/list [(v values) (i (in-naturals))] (if (not (member i indices)) v (begin0 (first sorted) (set! sorted (rest sorted))))))   (define (task) (sort-disjoi...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
/* NetRexx */ options replace format comments java crossref savelog symbols nobinary   class RCSortStability   method main(args = String[]) public constant   cityList = [String "UK London", "US New York", "US Birmingham", "UK Birmingham"]   cn = String[cityList.length]   say say "Before sort:" System.arra...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 nobinary   class RCSortStability   method main(args = String[]) public constant   cityList = [String "UK London", "US New York", "US Birmingham", "UK Birmingham"]   cn = String[cityList.length]   say say "Before sort:" System.arra...
http://rosettacode.org/wiki/Sort_numbers_lexicographically
Sort numbers lexicographically
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#VBA
VBA
Public Function sortlexicographically(N As Integer) Dim arrList As Object Set arrList = CreateObject("System.Collections.ArrayList") For i = 1 To N arrList.Add CStr(i) Next i arrList.Sort Dim item As Variant For Each item In arrList Debug.Print item & ", "; Next End Funct...
http://rosettacode.org/wiki/Sort_numbers_lexicographically
Sort numbers lexicographically
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Wren
Wren
import "/sort" for Sort   var a = (1..13).map { |i| "%(i)" }.toList Sort.quick(a) System.print(a)
http://rosettacode.org/wiki/Sort_numbers_lexicographically
Sort numbers lexicographically
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 lexN(n){ n.pump(List,'+(1),"toString").sort().apply("toInt") }
http://rosettacode.org/wiki/Sort_three_variables
Sort three variables
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
function variadicSort (...) local t = {} for _, x in pairs{...} do table.insert(t, x) end table.sort(t) return unpack(t) end   local testCases = { { x = 'lions, tigers, and', y = 'bears, oh my!', z = '(from the "Wizard of OZ")' }, { x = 77444, y = -12, z = 0 } } for i, case in ipai...
http://rosettacode.org/wiki/Sort_using_a_custom_comparator
Sort using a custom comparator
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Go
Go
package main   import ( "fmt" "sort" "strings" )   type sortable []string   func (s sortable) Len() int { return len(s) } func (s sortable) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s sortable) Less(i, j int) bool { a, b := s[i], s[j] if len(a) != len(b) { return len(a) > len(b) ...
http://rosettacode.org/wiki/Sorting_algorithms/Comb_sort
Sorting algorithms/Comb sort
Sorting algorithms/Comb sort You are encouraged to solve this task according to the task description, using any language you may know. Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algor...
#Ring
Ring
  aList = [3,5,1,2,7,4,8,3,6,4,1] see combsort(aList)   func combsort t gapd = 1.2473 gap = len(t) swaps = 0 while gap + swaps > 1 k = 0 swaps = 0 if gap > 1 gap = floor(gap / gapd) ok for k = 1 to len(t) - gap if t[k] > t[k + gap] ...
http://rosettacode.org/wiki/Sorting_algorithms/Bogosort
Sorting algorithms/Bogosort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 shuffle ($a) { $c = $a.Clone() # make copy to avoid clobbering $a 1..($c.Length - 1) | ForEach-Object { $i = Get-Random -Minimum $_ -Maximum $c.Length $c[$_-1],$c[$i] = $c[$i],$c[$_-1] $c[$_-1] # return newly-shuffled value } $c[-1] # last value }   function isSorted...
http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort
Sorting algorithms/Bubble 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 TestBubbleSort;   {$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/Gnome_sort
Sorting algorithms/Gnome 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
function gnomeSort(a) local i, j = 2, 3   while i <= #a do if a[i-1] <= a[i] then i = j j = j + 1 else a[i-1], a[i] = a[i], a[i-1] -- swap i = i - 1 if i == 1 then -- 1 instead of 0 i = j j = j + 1 ...
http://rosettacode.org/wiki/Sorting_algorithms/Bead_sort
Sorting algorithms/Bead 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
#MAXNUM=100   Dim MyData(Random(15)+5) Global Dim Abacus(0,0)   Declare BeadSort(Array InData(1)) Declare PresentData(Array InData(1))   If OpenConsole() Define i ;- Generate a random array For i=0 To ArraySize(MyData()) MyData(i)=Random(#MAXNUM) Next i PresentData(MyData()) ; ;- Sort the array Bead...
http://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort
Sorting algorithms/Cocktail sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Java
Java
public static void cocktailSort( int[] A ){ boolean swapped; do { swapped = false; for (int i =0; i<= A.length - 2;i++) { if (A[ i ] > A[ i + 1 ]) { //test whether the two elements are in the wrong order int temp = A[i]; A[i] = A[i+1]; A[i+1]=temp; swapped = true; } } if (!swapped)...
http://rosettacode.org/wiki/Solve_a_Holy_Knight%27s_tour
Solve a Holy Knight's tour
Chess coaches have been known to inflict a kind of torture on beginners by taking a chess board, placing pennies on some squares and requiring that a Knight's tour be constructed that avoids the squares with pennies. This kind of knight's tour puzzle is similar to   Hidato. The present task is to produce a solution ...
#11l
11l
V moves = [ [-1, -2], [1, -2], [-1, 2], [1, 2], [-2, -1], [-2, 1], [2, -1], [2, 1] ]   F solve(&pz, sz, sx, sy, idx, cnt) I idx > cnt R 1   L(i) 0 .< :moves.len V x = sx + :moves[i][0] V y = sy + :moves[i][1] I sz > x & x > -1 & sz > y & y > -1 & pz[x][y] == 0 pz[x][y] = i...
http://rosettacode.org/wiki/Solve_a_Hopido_puzzle
Solve a Hopido puzzle
Hopido puzzles are similar to Hidato. The most important difference is that the only moves allowed are: hop over one tile diagonally; and over two tiles horizontally and vertically. It should be possible to start anywhere in the path, the end point isn't indicated and there are no intermediate clues. Hopido Design Pos...
#C.23
C#
using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; using static System.Linq.Enumerable;   public class Solver { private static readonly (int dx, int dy)[] //other puzzle types elided hopidoMoves = {(-3,0),(0,-3),(0,3),(3,0),(-2,-2),(-2,...
http://rosettacode.org/wiki/Sort_an_array_of_composite_structures
Sort an array of composite structures
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#AppleScript
AppleScript
use framework "Foundation"   ----- SORTING COMPOSITE STRUCTURES (BY MULTIPLE KEYS) ------   -- List of {strKey, blnAscending} pairs -> list of records -> sorted list of records   -- sortByComparing :: [(String, Bool)] -> [Record] -> [Record] on sortByComparing(keyDirections, xs) set ca to current application   ...
http://rosettacode.org/wiki/Sorting_algorithms/Counting_sort
Sorting algorithms/Counting 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
>>> from collections import defaultdict >>> def countingSort(array, mn, mx): count = defaultdict(int) for i in array: count[i] += 1 result = [] for j in range(mn,mx+1): result += [j]* count[j] return result   >>> data = [9, 7, 10, 2, 9, 7, 4, 3, 10, 2, 7, 10, 2, 1, 3, 8, 7, 3, 9, 5, 8, 5, 1, 6, 3, 7, 5, 4, 6, ...
http://rosettacode.org/wiki/Solve_the_no_connection_puzzle
Solve the no connection puzzle
You are given a box with eight holes labelled   A-to-H,   connected by fifteen straight lines in the pattern as shown below: A B /│\ /│\ / │ X │ \ / │/ \│ \ C───D───E───F \ │\ /│ / \ │ X │ / \│/ \│/ G H You ar...
#Ada
Ada
  With Ada.Text_IO, Connection_Types, Connection_Combinations;   procedure main is Result : Connection_Types.Partial_Board renames Connection_Combinations; begin Ada.Text_IO.Put_Line( Connection_Types.Image(Result) ); end;
http://rosettacode.org/wiki/Solve_the_no_connection_puzzle
Solve the no connection puzzle
You are given a box with eight holes labelled   A-to-H,   connected by fifteen straight lines in the pattern as shown below: A B /│\ /│\ / │ X │ \ / │/ \│ \ C───D───E───F \ │\ /│ / \ │ X │ / \│/ \│/ G H You ar...
#APL
APL
      perms←{ ⍝∇ 20100513/20140818 ra⌈ --()-- 1=⍴⍴⍵:⍵[∇ ''⍴⍴⍵] ↑{0∊⍴⍵:⍵ ⋄ (⍺[1]⌷⍵),(1↓⍺)∇ ⍵~⍺[1]⌷⍵}∘(⍳⍵)¨↓⍉1+(⌽⍳⍵)⊤¯1+⍳!⍵ }   solution←{ links← (3 4 5) (4 5 6) (1 4 7) (1 2 3 5 7 8) (1 2 4 6 7 8) (2 5 8) (3 4 5) (4 5 6) ⍝ node i connects with nodes i⊃links tries←8 perms 8 fails←...
http://rosettacode.org/wiki/Solve_a_Numbrix_puzzle
Solve a Numbrix puzzle
Numbrix puzzles are similar to Hidato. The most important difference is that it is only possible to move 1 node left, right, up, or down (sometimes referred to as the Von Neumann neighborhood). Published puzzles also tend not to have holes in the grid and may not always indicate the end node. Two examples follow: Exam...
#Elixir
Elixir
# require HLPsolver   adjacent = [{-1, 0}, {0, -1}, {0, 1}, {1, 0}]   board1 = """ 0 0 0 0 0 0 0 0 0 0 0 46 45 0 55 74 0 0 0 38 0 0 43 0 0 78 0 0 35 0 0 0 0 0 71 0 0 0 33 0 0 0 59 0 0 0 17 0 0 0 0 0 67 0 0 18 0 0 11 0 0 64 0 0 0 24 21 0 1 2 0 0 0 0 0 0 0 0 0 ...
http://rosettacode.org/wiki/Solve_a_Numbrix_puzzle
Solve a Numbrix puzzle
Numbrix puzzles are similar to Hidato. The most important difference is that it is only possible to move 1 node left, right, up, or down (sometimes referred to as the Von Neumann neighborhood). Published puzzles also tend not to have holes in the grid and may not always indicate the end node. Two examples follow: Exam...
#Go
Go
package main   import ( "fmt" "sort" "strconv" "strings" )   var example1 = []string{ "00,00,00,00,00,00,00,00,00", "00,00,46,45,00,55,74,00,00", "00,38,00,00,43,00,00,78,00", "00,35,00,00,00,00,00,71,00", "00,00,33,00,00,00,59,00,00", "00,17,00,00,00,00,00,67,00", "00,18,00,...
http://rosettacode.org/wiki/Sort_an_integer_array
Sort an integer array
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#BaCon
BaCon
' Sort an integer array DECLARE values[5] TYPE NUMBER values[0] = 23 values[1] = 32 values[2] = 12 values[3] = 21 values[4] = 01   SORT values   FOR i = 0 TO 3 PRINT values[i], ", "; NEXT PRINT values[4]
http://rosettacode.org/wiki/Sort_an_integer_array
Sort an integer array
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#BBC_BASIC
BBC BASIC
INSTALL @lib$+"SORTLIB" sort% = FN_sortinit(0,0)   DIM array(8) array() = 8, 2, 5, 9, 1, 3, 6, 7, 4   C% = DIM(array(),1) + 1 CALL sort%, array(0)   FOR i% = 0 TO DIM(array(),1) - 1 PRINT ; array(i%) ", "; NEXT PRINT ; array(i%)
http://rosettacode.org/wiki/Sort_a_list_of_object_identifiers
Sort a list of object identifiers
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 oid-vec [oid-str] (->> (clojure.string/split oid-str #"\.") (map #(Long. %))))   (defn oid-str [oid-vec] (clojure.string/join "." oid-vec))   ;; ;; If vals differ before shorter vec ends, ;; use comparison of that "common header". ;; If common part matches, compare based on num elements. ;; (defn oid...
http://rosettacode.org/wiki/Sort_disjoint_sublist
Sort disjoint sublist
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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'culture;   extension op { sortSublist(indices) { var subList := indices.orderBy:(x => x) .zipBy(indices.selectBy:(i => self[i]) .orderBy:(x => x), (index,val => new{ Index = index; Value...
http://rosettacode.org/wiki/Sort_disjoint_sublist
Sort disjoint sublist
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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_disjoint do def sublist(values, indices) when is_list(values) and is_list(indices) do indices2 = Enum.sort(indices) selected = select(values, indices2, 0, []) |> Enum.sort replace(values, Enum.zip(indices2, selected), 0, []) end   defp select(_, [], _, selected), do: selected defp sel...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
import algorithm   const Records = [(country: "UK", city: "London"), (country: "US", city: "New York"), (country: "US", city: "Birmingham"), (country: "UK", city: "Birmingham")]   echo "Original order:" for record in Records: echo record.country, " ", record.city ech...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
/* Rexx */ Do cities = .array~of('UK London', 'US New York', 'US Birmingham', 'UK Birmingham',)   Say; Say 'Original table' Call display cities   Say; Say 'Unstable sort on city' sorted = cities~copy sorted~sortWith(.ColumnComparator~new(4, 20)) Call display sorted   Say; Say 'Stable sort on city' ...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 */ Do cities = .array~of('UK London', 'US New York', 'US Birmingham', 'UK Birmingham',)   Say; Say 'Original table' Call display cities   Say; Say 'Unstable sort on city' sorted = cities~copy sorted~sortWith(.ColumnComparator~new(4, 20)) Call display sorted   Say; Say 'Stable sort on city' ...
http://rosettacode.org/wiki/Sort_three_variables
Sort three variables
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 Sort3 { Let X=77744, Y=-12, Z=0 Let X$ = "lions, tigers, and", Y$ = "bears, oh my!", Z$ = {(from the "Wizard of OZ")} \\ & use for by reference pass Module SortSome (&X$, &Y$, &Z$){ If Type$(X$)<>"String" Then { Link X$,Y$, Z$ to X,Y,Z ...
http://rosettacode.org/wiki/Sort_three_variables
Sort three variables
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
lst := sort([x,y,z]): x,y,z := lst[1],lst[2],lst[3]:
http://rosettacode.org/wiki/Sort_using_a_custom_comparator
Sort using a custom comparator
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Groovy
Groovy
def strings = "Here are some sample strings to be sorted".split() strings.sort { x, y -> y.length() <=> x.length() ?: x.compareToIgnoreCase(y) } println strings
http://rosettacode.org/wiki/Sorting_algorithms/Comb_sort
Sorting algorithms/Comb sort
Sorting algorithms/Comb sort You are encouraged to solve this task according to the task description, using any language you may know. Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algor...
#Ruby
Ruby
class Array def combsort! gap = size swaps = true while gap > 1 or swaps gap = [1, (gap / 1.25).to_i].max swaps = false 0.upto(size - gap - 1) do |i| if self[i] > self[i+gap] self[i], self[i+gap] = self[i+gap], self[i] swaps = true end end en...
http://rosettacode.org/wiki/Sorting_algorithms/Bogosort
Sorting algorithms/Bogosort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Prolog
Prolog
bogo_sort(L,Rl) :- min_list(L,Min), repeat, random_permutation(L,Rl), is_sorted(Rl,Min), !.   is_sorted([],_). is_sorted([N|T],P) :- N >= P, is_sorted(T,N).
http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort
Sorting algorithms/Bubble 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 ...
#Draco
Draco
/* Bubble sort an array of integers */ proc nonrec bubblesort([*] int a) void: bool sorted; int i, temp; sorted := false; while not sorted do sorted := true; for i from 1 upto dim(a,1)-1 do if a[i-1] > a[i] then sorted := false; temp := a[i-1];...
http://rosettacode.org/wiki/Sorting_algorithms/Gnome_sort
Sorting algorithms/Gnome 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): i := 2: while (i <= len) do if (i=1 or arr[i] >= arr[i-1]) then i++: else temp:= arr[i]: arr[i] := arr[i-1]: arr[i-1] := temp: i--: end if: end do: arr;
http://rosettacode.org/wiki/Sorting_algorithms/Bead_sort
Sorting algorithms/Bead 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
  #!/bin/python3 from itertools import zip_longest   # This is wrong, it works only on specific examples def beadsort(l): return list(map(sum, zip_longest(*[[1] * e for e in l], fillvalue=0)))     # Demonstration code: print(beadsort([5,3,1,7,4,1,1]))  
http://rosettacode.org/wiki/Sorting_algorithms/Bead_sort
Sorting algorithms/Bead 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 ...
#QB64
QB64
  #lang QB64 '*************************************************** '* BeadSort is VERY fast for small CGSortLibArray(max)-CGSortLibArray(min). Typical performance is '* O(NlogN) or better. However as the key values (array values and ranges) go up, the performance '* drops steeply excellent for small-ranged arrays. Integ...
http://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort
Sorting algorithms/Cocktail 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
  // Node 5.4.1 tested implementation (ES6) "use strict";   let arr = [4, 9, 0, 3, 1, 5]; let isSorted = true; while (isSorted){ for (let i = 0; i< arr.length - 1;i++){ if (arr[i] > arr[i + 1]) { let temp = arr[i]; arr[i] = arr[i + 1]; arr[i...
http://rosettacode.org/wiki/Solve_a_Holy_Knight%27s_tour
Solve a Holy Knight's tour
Chess coaches have been known to inflict a kind of torture on beginners by taking a chess board, placing pennies on some squares and requiring that a Knight's tour be constructed that avoids the squares with pennies. This kind of knight's tour puzzle is similar to   Hidato. The present task is to produce a solution ...
#Ada
Ada
with Knights_Tour, Ada.Text_IO, Ada.Command_Line;   procedure Holy_Knight is   Size: Positive := Positive'Value(Ada.Command_Line.Argument(1)); package KT is new Knights_Tour(Size => Size); Board: KT.Tour := (others => (others => Natural'Last));   Start_X, Start_Y: KT.Index:= 1; -- default start place (1,1) ...
http://rosettacode.org/wiki/Solve_a_Hopido_puzzle
Solve a Hopido puzzle
Hopido puzzles are similar to Hidato. The most important difference is that the only moves allowed are: hop over one tile diagonally; and over two tiles horizontally and vertically. It should be possible to start anywhere in the path, the end point isn't indicated and there are no intermediate clues. Hopido Design Pos...
#C.2B.2B
C++
  #include <vector> #include <sstream> #include <iostream> #include <iterator> #include <stdlib.h> #include <string.h>   using namespace std;   struct node { int val; unsigned char neighbors; };   class nSolver { public: nSolver() { dx[0] = -2; dy[0] = -2; dx[1] = -2; dy[1] = 2; dx[2] = 2; dy[2] = -...
http://rosettacode.org/wiki/Sort_an_array_of_composite_structures
Sort an array of composite structures
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#ARM_Assembly
ARM Assembly
    /* ARM assembly Raspberry PI */ /* program compositeSort.s */   /* REMARK 1 : this program use routines in a include file see task Include a file language arm assembly for the routine affichageMess conversion10 see at end of this program the instruction include */ /* for constantes see task include...
http://rosettacode.org/wiki/Sorting_algorithms/Counting_sort
Sorting algorithms/Counting 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
[ 2dup peek 1+ unrot poke ] is [1+] ( [ n --> [ )   [ 1+ dip tuck - rot 0 swap of swap rot witheach [ over + rot swap [1+] swap ] negate swap [] swap witheach [ dip [ over i^ + ] of join ] nip ] is csort ( [ n n --> [ )   ...
http://rosettacode.org/wiki/Sorting_algorithms/Counting_sort
Sorting algorithms/Counting sort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#R
R
counting_sort <- function(arr, minval, maxval) { r <- arr z <- 1 for(i in minval:maxval) { cnt = sum(arr == i) while(cnt > 0) { r[z] = i z <- z + 1 cnt <- cnt - 1 } } r }   # 140+1 instead of 140, since random numbers generated # by runif are always less than the given maximum; #...
http://rosettacode.org/wiki/Solve_the_no_connection_puzzle
Solve the no connection puzzle
You are given a box with eight holes labelled   A-to-H,   connected by fifteen straight lines in the pattern as shown below: A B /│\ /│\ / │ X │ \ / │/ \│ \ C───D───E───F \ │\ /│ / \ │ X │ / \│/ \│/ G H You ar...
#ARM_Assembly
ARM Assembly
    /* ARM assembly Raspberry PI */ /* program noconnpuzzle.s */   /************************************/ /* Constantes */ /************************************/ .equ STDOUT, 1 @ Linux output console .equ EXIT, 1 @ Linux syscall .equ WRITE, 4 @ Linux syscall   .equ NBBOX, 8 .eq...
http://rosettacode.org/wiki/Solve_a_Numbrix_puzzle
Solve a Numbrix puzzle
Numbrix puzzles are similar to Hidato. The most important difference is that it is only possible to move 1 node left, right, up, or down (sometimes referred to as the Von Neumann neighborhood). Published puzzles also tend not to have holes in the grid and may not always indicate the end node. Two examples follow: Exam...
#Icon_and_Unicon
Icon and Unicon
global nCells, cMap, best record Pos(r,c)   procedure main(A) puzzle := showPuzzle("Input",readPuzzle()) QMouse(puzzle,findStart(puzzle),&null,0) showPuzzle("Output", solvePuzzle(puzzle)) | write("No solution!") end   procedure readPuzzle() # Start with a reduced puzzle space p := [] nCells := m...
http://rosettacode.org/wiki/Sort_an_integer_array
Sort an integer array
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Beads
Beads
beads 1 program 'Sort an integer array' calc main_init var arr = [4, 1, 2, -1, 3, 0, 2] var newarr : array of num loop across:arr sort:val count:c val:v newarr[c] = v
http://rosettacode.org/wiki/Sort_an_integer_array
Sort an integer array
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Befunge
Befunge
v > 543** >  :#v_ $&>  :#v_ 1 > :0g >  :#v_ $ 1+: 543** `! #v_ 25*,@ ^-1p0\0:< ^-1 p0\+1 g0:&< ^-1\.:\< ^ <
http://rosettacode.org/wiki/Sort_a_list_of_object_identifiers
Sort a list of object identifiers
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 oid->list (oid) (loop for start = 0 then (1+ pos) for pos = (position #\. oid :start start) collect (parse-integer oid :start start :end pos) while pos))   (defun list< (list1 list2) (loop for e1 in list1 for e2 in list2 do (cond ((< e1 e2) (return t)...
http://rosettacode.org/wiki/Sort_a_list_of_object_identifiers
Sort a list of object identifiers
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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_by_OID do def numbers(list) do Enum.sort_by(list, fn oid -> String.split(oid, ".") |> Enum.map(&String.to_integer/1) end) end end   ~w[ 1.3.6.1.4.1.11.2.17.19.3.4.0.10 1.3.6.1.4.1.11.2.17.5.2.0.79 1.3.6.1.4.1.11.2.17.19.3.4.0.4 1.3.6.1.4.1.11150.3.4.0.1 1.3.6.1.4.1.11.2.17.19....
http://rosettacode.org/wiki/Sort_disjoint_sublist
Sort disjoint sublist
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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_disjoint ).   -export( [sublist/2, task/0] ).   sublist( Values, Indices ) -> Sorted_indices = lists:sort( Indices ), Values_indexes = lists:seq( 1, erlang:length(Values) ), {[], [], Indices_values} = lists:foldl( fun indices_values/2, {Values, Sorted_indices, []}, Values_indexes ), Sorted_indices_v...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#OpenEdge.2FProgress
OpenEdge/Progress
DEFINE TEMP-TABLE tt FIELD country AS CHAR FORMAT 'x(2)' FIELD city AS CHAR FORMAT 'x(16)' .   DEFINE VARIABLE cc AS CHARACTER EXTENT 2.   CREATE tt. ASSIGN tt.country = 'UK' tt.city = 'London'. CREATE tt. ASSIGN tt.country = 'US' tt.city = 'New York'. CREATE tt. ASSIGN tt.country = 'US' tt.city = 'Birmin...
http://rosettacode.org/wiki/Sort_stability
Sort stability
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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 Cities = ['UK'#'London' 'US'#'New York' 'US'#'Birmingham' 'UK'#'Birmingham'] in %% sort by city; stable because '=<' is reflexiv {Show {Sort Cities fun {$ A B} A.2 =< B.2 end}}   %% sort by country; NOT stable because '<' is not reflexiv {Show {Sort Cities fun {$ A ...
http://rosettacode.org/wiki/Sort_three_variables
Sort three variables
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other 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
{x, y, z} = Sort[{x, y, z}]
http://rosettacode.org/wiki/Sort_three_variables
Sort three variables
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#min
min
(=c =b =a (a -> b -> c ->) => '> sort -> c @ b @ a @) :sort3   "lions, tigers, and" :x "bears, oh my!" :y "(from the \"Wizard of OZ\")" :z   'x 'y 'z sort3 x puts! y puts! z puts!   77444 :x -12 :y 0 :z   'x 'y 'z sort3 x puts! y puts! z puts!
http://rosettacode.org/wiki/Sort_using_a_custom_comparator
Sort using a custom comparator
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#Haskell
Haskell
import Data.Char (toLower) import Data.List (sortBy) import Data.Ord (comparing)   -------------------- CUSTOM COMPARATORS ------------------   lengthThenAZ :: String -> String -> Ordering lengthThenAZ = comparing length <> comparing (fmap toLower)   descLengthThenAZ :: String -> String -> Ordering descLengthThenAZ = ...
http://rosettacode.org/wiki/Sorting_algorithms/Comb_sort
Sorting algorithms/Comb sort
Sorting algorithms/Comb sort You are encouraged to solve this task according to the task description, using any language you may know. Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algor...
#Rust
Rust
fn comb_sort<T: PartialOrd>(a: &mut [T]) { let len = a.len(); let mut gap = len; let mut swapped = true; while gap > 1 || swapped { gap = (4 * gap) / 5; if gap < 1 { gap = 1; } let mut i = 0; swapped = false; while i + gap < len { i...
http://rosettacode.org/wiki/Sorting_algorithms/Bogosort
Sorting algorithms/Bogosort
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#PureBasic
PureBasic
Procedure KnuthShuffle (Array a(1)) Protected i, Size = ArraySize(a()) For i = 0 To Size Swap a(i), a(Random(Size)) Next EndProcedure   Procedure isSorted(Array a(1)) Protected i, Size = ArraySize(a()) For i = 1 To Size If a(i) < a(i - 1) ProcedureReturn #False EndIf Next ...
http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort
Sorting algorithms/Bubble 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 ...
#Dyalect
Dyalect
func bubbleSort(list) { var done = false while !done { done = true for i in 1..(list.Length()-1) { if list[i - 1] > list[i] { var x = list[i] list[i] = list[i - 1] list[i - 1] = x done = false } } ...
http://rosettacode.org/wiki/Sorting_algorithms/Gnome_sort
Sorting algorithms/Gnome 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
gnomeSort[list_]:=Module[{i=2,j=3}, While[ i<=Length[[list]], If[ list[[i-1]]<=list[[i]], i=j; j++;, list[[i-1;;i]]=list[[i;;i-1]];i--;]; If[i==1,i=j;j++;] ]]
http://rosettacode.org/wiki/Sorting_algorithms/Bead_sort
Sorting algorithms/Bead 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 (require rackunit)   (define (columns lst) (match (filter (λ (l) (not (empty? l))) lst) ['() '()] [l (cons (map car l) (columns (map cdr l)))]))   (define (bead-sort lst) (map length (columns (columns (map (λ (n) (make-list n 1)) lst)))))   ;; unit test (check-equal? (bead-sort '(5 3 1 7 4 ...
http://rosettacode.org/wiki/Sorting_algorithms/Bead_sort
Sorting algorithms/Bead 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
# routine cribbed from List::Utils; sub transpose(@list is copy) { gather { while @list { my @heads; if @list[0] !~~ Positional { @heads = @list.shift; } else { @heads = @list.map({$_.shift unless $_ ~~ []}); } @list = @list.map({$_ unless $_ ~~ []}); ...
http://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort
Sorting algorithms/Cocktail 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
# In case your jq does not have "until" defined: def until(cond; next): def _until: if cond then . else (next|_until) end; _until;
http://rosettacode.org/wiki/Solve_a_Holy_Knight%27s_tour
Solve a Holy Knight's tour
Chess coaches have been known to inflict a kind of torture on beginners by taking a chess board, placing pennies on some squares and requiring that a Knight's tour be constructed that avoids the squares with pennies. This kind of knight's tour puzzle is similar to   Hidato. The present task is to produce a solution ...
#ALGOL_68
ALGOL 68
# directions for moves # INT nne = 1, ne = 2, se = 3, sse = 4; INT ssw = 5, sw = 6, nw = 7, nnw = 8;   INT lowest move = nne; INT highest move = nnw;   # the vertical position changes of the moves # []INT offset v = ( -2, -1, 1, 2, 2, 1, -1, -2 ); # the horizontal position changes of ...
http://rosettacode.org/wiki/Solve_a_Hopido_puzzle
Solve a Hopido puzzle
Hopido puzzles are similar to Hidato. The most important difference is that the only moves allowed are: hop over one tile diagonally; and over two tiles horizontally and vertically. It should be possible to start anywhere in the path, the end point isn't indicated and there are no intermediate clues. Hopido Design Pos...
#D
D
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.typecons;     struct HopidoPuzzle { private alias InputCellBaseType = char; private enum InputCell : InputCellBaseType { available = '#', unavailable = '.' } private alias Cell = uint; private enum : Cell { unknownCell = 0, unavailabl...
http://rosettacode.org/wiki/Sort_an_array_of_composite_structures
Sort an array of composite structures
Sorting Algorithm This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see compare sorts.   For other sorting algorithms,   see sorting algorithms,   or: O(n logn) sorts Heap sort | Merge sort | Patience sort | Quick sort O(n log2n) sorts Shell Sort ...
#AutoHotkey
AutoHotkey
start: Gui, Add, ListView, r20 w200, 1|2 data = ( foo,53 joe,34 bar,23 )   Loop, parse, data, `n { stringsplit, row, A_LoopField, `, LV_Add(row, row1, row2) } LV_ModifyCol() ; Auto-size columns Gui, Show msgbox, sorting by column1 LV_ModifyCol(1, "sort") ; sort by first column msgbox, sorting by column2 LV_ModifyC...
http://rosettacode.org/wiki/Sorting_algorithms/Counting_sort
Sorting algorithms/Counting 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 (counting-sort xs min max) (define ns (make-vector (+ max (- min) 1) 0)) (for ([x xs]) (vector-set! ns (- x min) (+ (vector-ref ns (- x min)) 1))) (for/fold ([i 0]) ([n ns] [x (in-naturals)]) (for ([j (in-range i (+ i n ))]) (vector-set! xs j (+ x min))) (+ i n)) xs)   (c...