{{subst:AfC submission/draftnew}} == Introduction == {{short description|Sorting algorithm}} {{No footnotes|date=May 2022}} {{Infobox Algorithm | name = {{PAGENAMEBASE}} | class = [[Sorting algorithm]] | category = Merge sort | data = [[Array data structure|Array]] | average-time = , 8.705 for n=1.000 | best-time = | time = | space = | stability = [[Sorting algorithm#Classification|Stable]] }} How it work? {{PAGENAMEBASE}} merge sorted list. First, you must detect sorted sub-arrays (by compare values on positions 0-1, 1-2, 2-3...). Or, we can say, always, sorted array have size 1. Then merge two arrays (best, arrays with the smallest size) to one. Repeat merging. Trick lies in it, sorted array can compare from top, smaller value move to save, not need more compare with any value in any in this two arrays. Properties: Algoritm need extra memory (copy from array 1 to array 2 and back). Ist fast. Stable. Can be modified to multi-thread. Version with detect sorted sub-arrays can be modified, return ascendecy (asc), descendency (desc) array as . Can save longer from one of asc or desc sub-arrays. == Statistics (average) ==
n = 1.000 value-min = 0 value-max = n/2 // 50% of array contain some repeating value ------------------ compares ~ 8.705 (Tim-sort ~8.680) cycles ~ 11.011 (Tim-sort ~20.969) moves ~ 10.000 (Tim-sort ~15.534, Select-sort ~2.979) stability = stable== Schematic of work ==
3 1 2 2 0 3 1 0 // input (array_1)
3-1 2-2 0-3 1-0 // compare top of sorted list A (list array-size = 1) with top of sorted list B (list length 1), C-D, E-F, G-H
1 3 2 2 0 3 0 1 // saved output at end in array_2, cmp = 4 (you still copy from array 1 to array 2 and back, need two array or two array with indexes)
1 3 | 2 2 | 0 3 | 0 1 // input (array_2; A-B, C-D, E-F, G-H is now new sorted array with array-size = 2)
1-----2 // compare first from AB with first from CD, smaller save
1 // save
3---2 // compare next from AB with first from CD, smaller save
2 // save
3-----2 // compare from last AB with last from CD, smaller save
2 // save
3 // save (copy)
1 2 2 3 // saved output at end (in array_1), cmp + 3
0-----0 // compare first (EF) with first (GH)
0 // save
3---0 // compare next (EF) with first (GH)
0 // save
3-----1 // compare last (EF) with last (GH)
1 // save
3 // save (copy)
0 0 1 3 // saved output at end (in array_1), cmp + 3
1 2 2 3 | 0 0 1 3 // new sorted lists
1---------0 // compare first (ABCD) with first (EFGH)
0 // save
1-----------0 // compare first (ABCD) with second (EFGH)
0 // save
1-------------1 // compare first (ABCD) with third (EFGH)
1 // save
2------------1 // compare second (ABCD) with third (EFGH)
1 // save
2--------------3 // compare second (ABCD) with last (EFGH)
2 // save
2------------3 // compare third (ABCD) with last (EFGH)
2 // save
3----------3 // compare last (ABCD) with last (EFGH)
3 // save
3 // save (copy), cmp + 7
==================
0 0 1 1 2 2 3 3 // output in array_2, return handle to array, suma(cmp) = 4+3+3+7 = 17
== Code (javascript) ==