== Description == '''PyramidSelectionSort''' get first pair of values, compare it and save minimal value (index) to new array. Repeat for all pair, create row 0. Repeat for row 0, create row 1... Find minimal value. Create tournament table of winners. Then remove minimal and rebuild pyramid branch (where minimal figured) and again find minimal value. {{Aligned table |cols=2|class=wikitable |col1header=on |col1align=left | Category | Sorting algorithm | Sub category | Selection sort | Name | '''PyramidSelectionSort''' | Data structure | Array | Comparations | O(n\log n) | Timing | O(n\log n) | Spacing | 2*n + n (input + output + index table) | Stability | Stable algorithm }} == Statistics from real code execution (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, Select-sort ~523.776)
cycles    ~ 11.262   (Tim-sort ~16.097)
moves     ~ 1.798    (Tim-sort ~13.659, Select-sort ~3.054)
stability = stable
== Schematic of work ==
pavel vs. tomas    zdenek vs. michal
  |         |         |         |
  +----+----+         +----+----+
       |                   |
     tomas               zdenek
       |                   |
       +---------+---------+
                 |
               zdenek                 --- out: zdenek

pavel vs. tomas       -       michal  --- remove winner and find new winner in this branch
  |         |         |         |
  +----+----+         +----+----+
       |                   |
     tomas               michal
       |                   |
       +---------+---------+
                 |
               tomas                  --- out: zdenek, tomas

pavel       -         -       michal
  |         |         |         |
  +----+----+         +----+----+
       |                   |
     pavel               michal
       |                   |
       +---------+---------+
                 |
               pavel                  --- out: zdenek, tomas, pavel, michal


3 1 2 2 0 3 1 0    // input

3-1 2-2 0-3 1-0    // compare pair from input and create row 0 of minimal
  1-2   0-----0    // row 0, pyramid of minimal values / index of position (for scheme i use value, use position in alg. code)
  1-----0 . .      // row 1
        0 . .      // row 2, save minimal to out "0", cmp = 7
          . .
  1 2     3---0    // rebuild branch (row[0][4,5,6,7], row[1][3,4], row[2][1]) and compare new winner in branch
  1-----------0
            . 0    // save "0", cmp + 2
            . x
  1 2     3-1 x    // rebuild branch
  1---------1
  1                // save "1", cmp + 2
  x
3---2     3 1      // rebuild branch
    2-------1
            1      // save "1", cmp + 2
            x
3   2     3 x      // rebuild branch (when not even or odd value from input, use "x" (-1 in alg. code), when "x" copy second index to next level)
    2-----3
    2              // save "2", cmp + 1
    x
3-----2   3        // rebuild branch (when "x", copy index to next level)
      2---3
      2            // save "2", cmp + 2
      x
3     x   3        // rebuild branch (when "x", copy index to next level)
3---------3
3                  // save "3", cmp + 1

          3        // save last "3"
===============
0 0 1 1 2 2 3 3    // output, suma(cmp) = 7+2+2+2+1+2+1 = 17
== Code (javascript) ==
== References == {{reflist}} == Links == === Internal links === * [[Data Structures and Algorithms/Sorting Data]] === External links === * [[wikipedia:Tournament_sort|Selection Tournament_sort]] * [https://mlich.zam.slu.cz/js-sort/sorting4-pokus.htm Test of sorting algorithms], generate statics (javascript)