
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other sorting algorithms,   see sorting algorithms,   or:

O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort


Task

Sort an array (or list) of integers in ascending numerical order.



Use a sorting facility provided by the language/library if possible.




/**
 <doc><h2>Sort integer array, in Neko</h2>
   <p>Array sort function modified from Haxe codegen with -D neko-source</p>
   <p>The Neko target emits support code for Haxe basics, sort is included</p>
   <p>Tectonics:<br />prompt$ nekoc sort.neko<br />prompt$ neko sort</p>
 </doc>
**/
 
var sort = function(a) {
    var i = 0;
    var len = $asize(a);
    while ( i < len ) {
        var swap = false;
        var j = 0;
        var max = (len - i) - 1;
        while ( j < max ) {
            if ( (a[j] - a[j + 1]) > 0 ) {
                var tmp = a[j + 1];
                a[j + 1] = a[j];
                a[j] = tmp;
                swap = true;
            }
            j += 1;
        }
        if ( $not(swap) )
            break;;
        i += 1;
    }
    return a;
}
 
var arr = $array(5,3,2,1,4)
$print(arr, "\n")
 
/* Sorts in place */
sort(arr)
$print(arr, "\n")
 
/* Also returns the sorted array for chaining */
$print(sort($array(3,1,4,1,5,9,2,6,5,3,5,8)), "\n")