Comment detail

与えられた並べ替えを実現するあみだくじの生成 (Nested Flatten)
とりあえずこんな感じ。

0 1 2 3 4 5
| |-| | | |
|-| |-| |-|
| |-| |-| |
|-| |-| |-|
| |-| |-| |
3 5 2 4 0 1
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import std.stdio;
import std.string;

void amidaSort(uint[] origList){
    auto sortedList = origList.dup.sort;
    auto tmpList = origList.dup;
    auto amidaRowProto = cast(char[])repeat("| ", origList.length)[0..($ - 1)];
    string[] amida;
    while(tmpList != sortedList){
        for(uint start; start <= 1; start++){
            uint[] swap;
            for(uint i = start; i < tmpList.length - 1; i += 2){
                if(tmpList[i] > tmpList[i + 1]){
                    swap ~= i;
                    auto tmp = tmpList[i + 1];
                    tmpList[i + 1] = tmpList[i];
                    tmpList[i] = tmp;
                }
            }
            if(swap.length){
                auto amidaRow = amidaRowProto.dup;
                foreach(i; swap){
                    amidaRow[(i * 2) + 1] = '-';
                }
                amida ~= cast(string)amidaRow;
            }
        }
    }
    string[] s;
    foreach(n; sortedList){
        s ~= toString(n);
    }
    string[] o;
    foreach(n; origList){
        o ~= toString(n);
    }
    if(!amida.length){
        amida ~= cast(string)amidaRowProto.dup;
    }
    writefln((s.join(" ") ~ amida.reverse ~ o.join(" ")).join("\n"));
}

void main(){
    amidaSort([3, 5, 2, 4, 0, 1]);
}

Index

Feed

Other

Link

Pathtraq

loading...