mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-15 03:05:41 +02:00
combination of quick sort and insertion sort
This commit is contained in:
@@ -1,9 +1,23 @@
|
|||||||
use <_vt_default_comparator.scad>;
|
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) =
|
function _sort(lt, less, gt_eq) =
|
||||||
let(leng = len(lt))
|
let(leng = len(lt))
|
||||||
leng <= 1 ? lt :
|
leng <= 7 ? _insert_sort(lt, less) :
|
||||||
leng == 2 ? gt_eq(lt[1], lt[0]) ? lt : [lt[1], lt[0]] :
|
// quick sort
|
||||||
let(
|
let(
|
||||||
pivot = lt[0],
|
pivot = lt[0],
|
||||||
before = [for(j = 1; j < leng; j = j + 1) if(less(lt[j], pivot)) lt[j]],
|
before = [for(j = 1; j < leng; j = j + 1) if(less(lt[j], pivot)) lt[j]],
|
||||||
|
Reference in New Issue
Block a user