{{subst:AfC submission/draftnew}} == Introduction == {{short description|Sorting algorithm}} {{No footnotes|date=May 2022}} {{Infobox Algorithm | name = {{PAGENAMEBASE}} | class = [[Sorting algorithm]] | category = Insert sort | data = [[Array data structure|Array]] | average-time = , 8.692 for n=1.000 | best-time = | time = | space = | stability = [[Sorting algorithm#Classification|Stable]] }} How it work? {{PAGENAMEBASE}} work as insert sort. Get next value (i+1) and insert to sorted array. Check possition from middle of sorted array, then middle of part... Properties: Algoritm not need extra memory. Ist slow for large array (moving a large array takes a lot of time). Have minimal comparation operations of all know algorithms (without counting sorts). Stable. == Statistics (average) ==
n = 1.000 value-min = 0 value-max = n/2 // 50% of array contain some repeating value ------------------ compares ~ 8.692 (Tim-sort ~8.680) cycles ~ 11.506 (Tim-sort ~20.969) moves ~ 1.753 (Tim-sort ~15.534, Select-sort ~2.979) stability = stable== Schematic of work ==
3 1 2 2 0 3 1 0 // input
3 // is first sorted array, (left = 0), half = 0, mid = 0 + floor(half / 2) (first pos_b), ''' cmp = 0 '''
. 1 // next i = 1, value = array[i] = 1, mid = 0
3-1 // compare 3-1
1 3 // 13 is now output, half = i, mid = 0 + floor(half / 2), ''' cmp + 1 '''
.
. 2 // next i, array[i], mid = 0
1---2 // compare(array[i], array[mid]), compare>=0, half = floor(half / 2), left = mid, mid = left + half
3-2 // compare(array[i], array[mid]), compare<0, end (because half = 0, cannot more divide), half = i, mid = 0 + round(half / 2), ''' cmp + 2 '''
1 2 3
. // ... note: for simplify, i remove lot of repeating text
2 // next i, mid = 1
2---2 // compare>=0, mid = left + half
. 3-2 // compare<0, end, ''' cmp + 2 '''
1 2 2 3
.
. 0 // next i, mid = 1
2-----0 // compare<0, mid = left - half
1-------0 // compare<0, end, ''' cmp + 2 '''
0 1 2 2 3
.
3 // next i, mid = 2
2-----3 // compare>=0, mid = left + half
. 2---3 // compare>=0, mid = left + half
. 3-3 // compare>=0, end, ''' cmp + 3 '''
0 1 2 2 3 3
.
. 1 // next i, mid = 2
2-------1 // compare<0, mid = left - half
1---------1 // compare>=0, end, ''' cmp + 2 ''' (my alg. code here compute cmp+3, because compare zero)
0 1 1 2 2 3 3
.
0 // next i, mid = 3
2-------0 // compare<0, mid = left - half
1-----------0 // compare<0, mid = left - half
0-------------0 // compare>=0, end, ''' cmp + 3 '''
===============
0 0 1 1 2 2 3 3 // output, suma(cmp) = 1+2+2+2+3+2+3 = 15
== Code (javascript) ==