Data Structures and Algorithms/SortedListMergingSort

Description
How it work? SortedListMergingSort 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 have sorted array 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 $$O(n)$$. Can save longer from one of asc or desc sub-arrays.

Note: Merging sorted arrays use TimSort, WikiSort.

Statistics (average)
n        = 1.024 value-min = 0 value-max = n/2    // 50% of array contain some repeating value -- compares ~ 8.886    (Tim-sort ~8.961) cycles   ~ 11.273   (Tim-sort ~16.097) moves    ~ 10.240   (Tim-sort ~13.659, Select-sort ~3.054)

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 21        // compare second (ABCD) with third (EFGH) 1        // save 2--3      // compare second (ABCD) with last (EFGH) 2                     // save 23      // 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