@smartroad

As I think has been well pointed out there are much better ways of doing a simple sort. For short and tight and waaaaaay more intuitive and understandable can I offer an insert sort?

is 5 smaller than 2? - no - leave array as is: 2, 5, 10, 1, 31

is 10 smaller than 5? - no - leave array as is: 2, 5, 10, 1, 31

is 1 smaller than 10? - yes

is 1 smaller than 5? - yes

is 1 smaller than 2? - yes

move 2, 5 and 10 to the right and slot in 1 - array is: 1, 2, 5, 10, 31

is 31 smaller than 10 - no - leave array as is: 1, 2, 5, 10, 31

`byte numbers[] = {2, 5, 10, 1, 31};`

void setup()

{

Serial.begin(9600);

printArray(numbers, sizeof(numbers));

isort(numbers, sizeof(numbers));

printArray(numbers, sizeof(numbers));

}

//Bubble sort my ar*e

void isort(byte *a, int n)

{

for (int i = 1; i < n; ++i)

{

int j = a[i];

int k;

for (k = i - 1; (k >= 0) && (j < a[k]); k--)

{

a[k + 1] = a[k];

}

a[k + 1] = j;

}

}

//what it says on the tin

void printArray(byte *a, int n)

{

for (int i = 0; i < n; i++)

{

Serial.print(a[i], DEC);

Serial.print(' ');

}

Serial.println();

}

//we'll not use loop for this

void loop()

{

}