1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-13 02:04:16 +02:00

combination of quick sort and insertion sort

This commit is contained in:
Justin Lin
2022-04-12 14:46:42 +08:00
parent 4f09162c28
commit 21a822e989

View File

@@ -1,9 +1,23 @@
use <_vt_default_comparator.scad>;
function _insert(sorted, elem, less, leng, before = [], i = 0) =
i == leng ? [each sorted, elem] :
less(elem, sorted[i]) ?
[each before, elem, each [for(j = [i:leng - 1]) sorted[j]]] :
_insert(sorted, elem, less, leng, [each before, sorted[i]], i + 1);
function _insert_sort_sub(lt, sorted, less, leng, i = 1) =
i == leng ? sorted :
_insert_sort_sub(lt, _insert(sorted, lt[i], less, len(sorted)), less, leng, i + 1);
function _insert_sort(lt, less) =
let(leng = len(lt))
leng == 0 ? lt : _insert_sort_sub(lt, [lt[0]], less, leng);
function _sort(lt, less, gt_eq) =
let(leng = len(lt))
leng <= 1 ? lt :
leng == 2 ? gt_eq(lt[1], lt[0]) ? lt : [lt[1], lt[0]] :
leng <= 7 ? _insert_sort(lt, less) :
// quick sort
let(
pivot = lt[0],
before = [for(j = 1; j < leng; j = j + 1) if(less(lt[j], pivot)) lt[j]],