29.12.2017, 03:47
Non-recursive QuickSort implementation for PAWN
If you don't know what QuickSort is, you should check:
https://en.wikipedia.org/wiki/Quicksort
Overview:
So, basically the other day I was playing around with some sorting algorithms in PAWN and I came across this non-recursive implementation of the quicksort algorithm by Darel Rex Finley. You can find it in the following link:
http://alienryderflex.com/quicksort/
I performed some benchmarks and it seems to be the fastest non-recursive quicksort implementation I have encountered so far. I decided not to implement the algorithm in PAWN code as it will of course execute slower.
Functions:
PHP Code:
/********************************************
Non-recursive implementation of the quicksort algorithm.
Available at:
http://alienryderflex.com/quicksort/nl67...2.02968025
********************************************/
/********************************************
Natives
********************************************/
/* quicksort_asc(array[], elements = sizeof(array));
Sorts a single dimension array in ascending order.
* Returns
- 0: function failed to execute
- 1: function executed successfully
* params
- array[]: the array you desire to sort.
- elements: the number of elements starting at index 0 that you wish to sort. (not final index).
*/
native quicksort_asc(array[], elements = sizeof(array));
/* quicksort_desc(array[], elements = sizeof(array));
Sorts a single dimension array in descending order.
* Returns
- 0: function failed to execute.
- 1: function executed successfully.
* params
- array[]: the array you desire to sort.
- elements: the number of elements starting at index 0 that you wish to sort (not final index).
*/
native quicksort_desc(array[], elements = sizeof(array));
/********************************************
Useful Macros
********************************************/
/* quicksort_ascEx(array[], start, end);
Sorts a single dimension array in ascending order starting at a specific index.
* Returns
- 0: function failed to execute
- 1: function executed successfully
* params
- array[]: the array you desire to sort.
- start: the starting index
- end: the final index
*/
#define quicksort_ascEx(%0,%1,%2) (quicksort_asc(%0[%1],%2-%1+1))
/* quicksort_descEx(array[], start, end);
Sorts a single dimension array in descending order starting at a specific index.
* Returns
- 0: function failed to execute
- 1: function executed successfully
* params
- array[]: the array you desire to sort.
- start: the starting index
- end: the final index
*/
#define quicksort_descEx(%0,%1,%2) (quicksort_desc(%0[%1],%2-%1+1))
Ascending order:
PHP Code:
new array[20] = {43, 99, -10, 0, 1, 9, 8, 7, 1939944, 1323324, 123455, -1234555, 21476834, 55433, -12345593, 1444, 23444, 9999, 77, -244};
quicksort_asc(array);
Code:
array[0] = -12345593 array[1] = -1234555 array[2] = -244 array[3] = -10 array[4] = 0 array[5] = 1 array[6] = 7 array[7] = 8 array[8] = 9 array[9] = 43 array[10] = 77 array[11] = 99 array[12] = 1444 array[13] = 9999 array[14] = 23444 array[15] = 55433 array[16] = 123455 array[17] = 1323324 array[18] = 1939944 array[19] = 21476834
PHP Code:
new array[5] = {4, 9, 10, 1, 2};
quicksort_ascEx(array, 1, 3);
Code:
array[0] = 4 array[1] = 1 array[2] = 9 array[3] = 10 array[4] = 2
PHP Code:
new array[20] = {43, 99, -10, 0, 1, 9, 8, 7, 1939944, 1323324, 123455, -1234555, 21476834, 55433, -12345593, 1444, 23444, 9999, 77, -244};
quicksort_desc(array);
Code:
array[0] = 21476834 array[1] = 1939944 array[2] = 1323324 array[3] = 123455 array[4] = 55433 array[5] = 23444 array[6] = 9999 array[7] = 1444 array[8] = 99 array[9] = 77 array[10] = 43 array[11] = 9 array[12] = 8 array[13] = 7 array[14] = 1 array[15] = 0 array[16] = -10 array[17] = -244 array[18] = -1234555 array[19] = -12345593
PHP Code:
new array[5] = {4, 9, 10, 1, 2};
quicksort_descEx(array, 1, 3);
Code:
array[0] = 4 array[1] = 10 array[2] = 9 array[3] = 1 array[4] = 2
Source