
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other 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

Given an integer   n,   return   1──►n   (inclusive)   in lexicographical order.



Show all output here on this page.



Example

Given   13,

return:   [1,10,11,12,13,2,3,4,5,6,7,8,9].

PROC PrintArray(INT ARRAY a INT size)
  INT i
 
  Put('[)
  FOR i=0 TO size-1
  DO
    IF i>0 THEN Put(' ) FI
    PrintI(a(i))
  OD
  Put(']) PutE()
RETURN
 
INT FUNC Compare(INT a1,a2)
  CHAR ARRAY s1(10),s2(10)
  INT res
 
  StrI(a1,s1) StrI(a2,s2)
  res=SCompare(s1,s2)
RETURN (res)
 
PROC InsertionSort(INT ARRAY a INT size)
  INT i,j,value
 
  FOR i=1 TO size-1
  DO
    value=a(i)
    j=i-1
    WHILE j>=0 AND Compare(a(j),value)>0
    DO
      a(j+1)=a(j)
      j==-1
    OD
    a(j+1)=value
  OD
RETURN
 
PROC Test(INT ARRAY a INT size)
  PrintE("Array before sort:")
  PrintArray(a,size)
  InsertionSort(a,size)
  PrintE("Array after sort:")
  PrintArray(a,size)
  PutE()
RETURN
 
PROC Main()
  DEFINE COUNT_A="13"
  DEFINE COUNT_B="50"
  INT ARRAY a(COUNT_A),b(COUNT_B)
  BYTE i
 
  FOR i=1 TO COUNT_A
  DO a(i-1)=i OD
 
  FOR i=1 TO COUNT_B
  DO b(i-1)=i OD
 
  Test(a,COUNT_A)
  Test(b,COUNT_B)
RETURN