ACE3/addons/common/functions/fnc_insertionSort.sqf

34 lines
695 B
Plaintext
Raw Normal View History

2015-01-16 23:21:47 +00:00
/**
* fn_insertionSort.sqf
* @Descr: Sorts an array of numbers
* @Author: Ruthberg
*
* @Arguments: [array ARRAY, (optional) ascending BOOL]
* @Return: sortedArray ARRAY
* @PublicAPI: true
*/
#include "script_component.hpp"
private ["_list", "_ascending", "_tmp", "_i", "_j"];
_list = +(_this select 0);
_ascending = true;
if (count _this > 1) then {
2015-01-18 19:09:19 +00:00
_ascending = _this select 1;
2015-01-16 23:21:47 +00:00
};
for "_i" from 1 to (count _list) - 1 do {
2015-01-18 19:09:19 +00:00
_tmp = _list select _i;
_j = _i;
while {_j >= 1 && {_tmp < _list select (_j - 1)}} do {
_list set [_j, _list select (_j - 1)];
_j = _j - 1;
};
_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
};
_list