While working with the Sabre engine, I had to use a sorting algorithm and I ended up

searching information about different sorting algorithms.

I found this one called "Shaker sort", (also known as "Cocktail sort") and

with the help of this link: http://www.dreamincode.net/forums/topic ... aker-sort/ wrote

a GE version of it.

It's pretty simple and it works well, so I decided to share it with you.

Here's how Wikipedia animates the sorting process:

So, simplified, what the function does, is that it sweeps back and forth through the array,

swapping adjacent elements if they are in wrong order (bigger one before smaller one) until the array has been sorted.

Download example ged:

More info about Shaker sort:

http://en.wikipedia.org/wiki/Cocktail_sort

The function code with some commenting to make it more understandable:

- Code: Select all
`void shakerSort(int * input_array, int array_count)`

{

int i;

int swap;

int temp;

do

{

swap = 0; //no values have been swapped

for (i = array_count - 1; i > 0; i --) //go through the array starting from the end

{

if (testArray[i - 1] > testArray[i]) //if the value of the smaller index element is

{ //higher than the value of the current element

temp = input_array[i - 1]; //swap the values

testArray[i - 1] = testArray[i];

testArray[i] = temp;

swap = 1; //some values have been swapped

}

}

for (i = 1; i < array_count; i ++) //go through the array starting from the beginning

{

if (testArray[i - 1] > testArray[i]) //if the value of the smaller index element is

{ //higher than the value of the current element

temp = testArray[i - 1]; //swap the values

testArray[i - 1] = testArray[i];

testArray[i] = temp;

swap = 1; //some values have been swapped

}

}

}

while(swap); //continue the do loop when swap is equal to 1 (-> some values have been swapped)

}