ACE3/addons/common/functions/fnc_insertionSort.sqf

39 lines
643 B
Plaintext
Raw Normal View History

2015-09-19 20:27:23 +00:00
/*
* Author: Ruthberg
* Sorts an array of numbers
2015-01-16 23:21:47 +00:00
*
2015-09-19 20:27:23 +00:00
* Arguments:
* 0: array <ARRAY>
* 1: ascending (optional) <BOOL>
*
* Return Value:
* sortedArray (ARRAY)
*
* Public: Yes
2015-01-16 23:21:47 +00:00
*/
#include "script_component.hpp"
2015-09-19 20:27:23 +00:00
params ["_list", ["_ascending", true]];
2015-01-16 23:21:47 +00:00
2015-09-19 20:27:23 +00:00
_list = + _list; // copy array to not alter the original one
private "_tmp";
for "_i" from 1 to (count _list - 1) do {
2015-01-18 19:09:19 +00:00
_tmp = _list select _i;
_j = _i;
2015-09-19 20:27:23 +00:00
2015-01-18 19:09:19 +00:00
while {_j >= 1 && {_tmp < _list select (_j - 1)}} do {
_list set [_j, _list select (_j - 1)];
_j = _j - 1;
};
2015-09-19 20:27:23 +00:00
_list set [_j, _tmp];
2015-01-16 23:21:47 +00:00
};
if (!_ascending) then {
2015-01-18 19:09:19 +00:00
reverse _list;
2015-01-16 23:21:47 +00:00
};
2015-09-19 20:27:23 +00:00
_list