クワッドソートを使用する

クワッドソート (quadsort) は、安定・適応的なボトムアップ型マージソートである。小区間を 4 要素単位で整え、隣接する整列済みブロックを 4 本まとめてマージする点(ピンポン・クワッドマージ)が特徴で、整列済み区間のマージを省略できる。

本記事の実装は説明用に簡略化しており、実際の実装で用いられる無分岐のパリティマージやクロスマージの代わりに、通常の安定二分マージを用いる。ブロック長も 8 ではなく 4 から始める。

  1. 整列・逆整列の早期終了: 全体が昇順なら何もしない。厳密な降順なら反転して終了する(比較・移動とも \(O(n)\))。
  2. クワッドスワップ: 4 要素ごとに比較交換ネットワークで整列し、長さ 4 の整列済みブロック列にする。余りは挿入ソートで整える。
  3. ピンポン・クワッドマージ: 隣接 4 ブロックを、まず 2 組ずつ補助配列へマージし、続けて本配列へ戻す 1 回のマージで 4 倍長の整列済み列にする(往復コピーを減らす)。
  4. スキップ: 4 ブロックの境界がすべて昇順ならマージを省略する。これにより完全整列済み入力は追加の \(O(n \log n)\) マージを避けられる。
  5. 余り: 4 ブロックに満たない残りは、通常の二分ボトムアップマージで吸収する。ブロック長を 4 倍しながら繰り返す。
procedure quad_swap4(A, i0, i1, i2, i3)
  compare-exchange pairs and cross pairs until four keys are sorted

procedure quad_merge_four(A, swap, start, block)
  if A[start+block-1] <= A[start+block]
     and A[start+2*block-1] <= A[start+2*block]
     and A[start+3*block-1] <= A[start+3*block] then
    return
  merge A[start .. start+2*block) into swap via two halves
  merge A[start+2*block .. start+4*block) into swap
  merge the two halves of swap back into A[start .. start+4*block)

procedure quadsort(A)
  if A is sorted then return
  if A is reverse-sorted then reverse(A); return
  for each complete group of 4 elements
    quad_swap4(group)
  insertion_sort(tail shorter than 4)
  block := 4
  while block < length(A)
    for each aligned span of 4*block elements
      quad_merge_four(A, swap, span, block)
    binary bottom-up merge any leftover runs of size block
    block := block * 4

最良は整列済み検出により \(O(n)\)、平均・最悪は \(O(n \log n)\) である。補助配列に最大 \(O(n)\) を使う安定ソートである。

類似アルゴリズムとの相違点

マージソートは 2 分割再帰が基本である。クワッドソートはボトムアップで 4 ブロック単位のピンポンマージと、境界昇順時のスキップを前面に出す。

ティムソートは自然ランの検出とギャロッピングマージが中心である。クワッドソートは固定長ブロックのクワッドスワップから始め、整列度に応じてマージを省略する。

フラックスソートはクイック型の安定分割を主とし、小区間でクワッドソート系の仕上げを使う。本記事のクワッドソートは分割を行わず、マージ側だけで完結する。

ツインソート (twinsort) はクワッドソートの簡略版である。2 要素単位のツインスワップで小区間を整え、ボトムアップのテイルマージで仕上げる安定・適応的マージソートで、コード量が少なくポインタや goto を使わないため移植や理解の足場になる。無分岐ではなく、クワッドソートほどの適応・小区間処理は持たない。

ピポソート (piposort) は名前どおりピンポン(ping-pong)に由来する。トップダウンで 4 分割し、小区間を整えたあと無分岐のパリティマージで 4 本ずつ往復マージする簡略版である。適応性はクワッドソートより弱いが、コード量と複雑さを大きく削いだまま高速で、本番実装への移植の出発点として位置づけられる。

時間計算量および空間計算量を計測する

Size Average time (s) Maximum time (s) Average memory (KiB) Maximum memory (KiB)
256 0.000010 0.000949 2 2
512 0.000022 0.000112 4 4
1024 0.000049 0.000438 8 8
2048 0.000108 0.000348 16 16
4096 0.000230 0.000459 32 32
8192 0.000474 0.000809 64 64
16384 0.001046 0.002592 128 128
32768 0.002135 0.003658 256 256
65536 0.004232 0.010144 512 512
131072 0.007658 0.013991 1024 1024
262144 0.016158 0.029996 2048 2048
計測に使用したコードを表示する

#!/usr/bin/env swift
import Foundation

// This standalone Swift driver creates the same temporary Docker build
// context as the former shell wrapper.  The benchmark program itself remains
// embedded below so readers can copy one complete, reproducible file.
struct BenchmarkError: Error, CustomStringConvertible {
    let message: String

    var description: String { message }

    init(_ message: String) {
        self.message = message
    }
}

func runCommand(_ executable: String, _ arguments: [String]) throws {
    let process = Process()
    process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
    process.arguments = [executable] + arguments
    process.standardInput = FileHandle.standardInput
    process.standardOutput = FileHandle.standardOutput
    process.standardError = FileHandle.standardError

    do {
        try process.run()
    } catch {
        throw BenchmarkError("Could not start \(executable): \(error)")
    }
    process.waitUntilExit()
    guard process.terminationStatus == 0 else {
        throw BenchmarkError(
            "Command failed (\(process.terminationStatus)): " +
            "\(executable) \(arguments.joined(separator: " "))"
        )
    }
}

do {
    // The UUID avoids collisions when two benchmark copies are run at once.
    let workdir = FileManager.default.temporaryDirectory
        .appendingPathComponent("swift-sort-benchmark-\(UUID().uuidString)")
    try FileManager.default.createDirectory(at: workdir, withIntermediateDirectories: true)
    defer { try? FileManager.default.removeItem(at: workdir) }

    // A raw Swift string is used so the nested main.swift keeps its own
    // interpolation expressions such as \(seed) until Docker compiles it.
    let dockerfile = #"""
FROM swift:6.0

WORKDIR /app

RUN cat > alloc_track.c <<'ALLOC'
#define _GNU_SOURCE
#include <dlfcn.h>
#include <malloc.h>
#include <stdatomic.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

static atomic_size_t live_bytes = 0;
static atomic_size_t peak_bytes = 0;

static void *(*real_malloc)(size_t) = NULL;
static void *(*real_calloc)(size_t, size_t) = NULL;
static void *(*real_realloc)(void *, size_t) = NULL;
static void (*real_free)(void *) = NULL;

static void init_reals(void) {
    if (real_malloc) {
        return;
    }
    real_malloc = (void *(*)(size_t))dlsym(RTLD_NEXT, "malloc");
    real_calloc = (void *(*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");
    real_realloc = (void *(*)(void *, size_t))dlsym(RTLD_NEXT, "realloc");
    real_free = (void (*)(void *))dlsym(RTLD_NEXT, "free");
}

static void record_alloc(size_t size) {
    size_t live = atomic_fetch_add(&live_bytes, size) + size;
    size_t peak = atomic_load(&peak_bytes);
    while (live > peak) {
        if (atomic_compare_exchange_weak(&peak_bytes, &peak, live)) {
            break;
        }
    }
}

void alloc_track_reset_peak(void) {
    atomic_store(&peak_bytes, atomic_load(&live_bytes));
}

size_t alloc_track_live(void) { return atomic_load(&live_bytes); }
size_t alloc_track_peak(void) { return atomic_load(&peak_bytes); }

void *malloc(size_t size) {
    init_reals();
    void *p = real_malloc(size);
    if (p) {
        record_alloc(malloc_usable_size(p));
    }
    return p;
}

void *calloc(size_t nmemb, size_t size) {
    init_reals();
    void *p = real_calloc(nmemb, size);
    if (p) {
        record_alloc(malloc_usable_size(p));
    }
    return p;
}

void *realloc(void *ptr, size_t size) {
    init_reals();
    size_t old_size = 0;
    if (ptr) {
        old_size = malloc_usable_size(ptr);
    }
    void *p = real_realloc(ptr, size);
    if (p) {
        atomic_fetch_sub(&live_bytes, old_size);
        record_alloc(malloc_usable_size(p));
    } else if (size == 0) {
        atomic_fetch_sub(&live_bytes, old_size);
    }
    return p;
}

void free(void *ptr) {
    init_reals();
    if (ptr) {
        atomic_fetch_sub(&live_bytes, malloc_usable_size(ptr));
        real_free(ptr);
    }
}

ALLOC

RUN cat > main.swift <<'SWIFT'
import Foundation
#if canImport(Glibc)
import Glibc
#elseif canImport(Darwin)
import Darwin
#endif

@_silgen_name("alloc_track_live") func alloc_track_live() -> Int
@_silgen_name("alloc_track_peak") func alloc_track_peak() -> Int
@_silgen_name("alloc_track_reset_peak") func alloc_track_reset_peak()

extension UnsafeMutableBufferPointer where Element == Int {
    func swapAt(_ i: Int, _ j: Int) {
        let t = self[i]; self[i] = self[j]; self[j] = t
    }
}

let MIN_POWER: Int = 8
let MAX_POWER: Int = 18
let RUNS: Int = 8192


/// Sort four elements with a small sorting network (equals keep order via `>`).
fileprivate func quad_swap4(
    _ a: UnsafeMutableBufferPointer<Int>,
    _ i0: Int,
    _ i1: Int,
    _ i2: Int,
    _ i3: Int
) {
    if a[i0] > a[i1] {
        a.swapAt(i0, i1)
    }
    if a[i2] > a[i3] {
        a.swapAt(i2, i3)
    }
    if a[i0] > a[i2] {
        a.swapAt(i0, i2)
    }
    if a[i1] > a[i3] {
        a.swapAt(i1, i3)
    }
    if a[i1] > a[i2] {
        a.swapAt(i1, i2)
    }
}

fileprivate func quad_is_sorted(_ a: UnsafeMutableBufferPointer<Int>) -> Bool {
    for i in 1..<a.count {
        if a[i - 1] > a[i] {
            return false
        }
    }
    return true
}

fileprivate func quad_is_reverse_sorted(_ a: UnsafeMutableBufferPointer<Int>) -> Bool {
    for i in 1..<a.count {
        if a[i - 1] < a[i] {
            return false
        }
    }
    return true
}

fileprivate func quad_reverse(_ a: UnsafeMutableBufferPointer<Int>) {
    var lo = 0
    var hi = a.count
    while lo + 1 < hi {
        hi -= 1
        a.swapAt(lo, hi)
        lo += 1
    }
}

/// Stable two-way merge from `src[lo..<mid)` and `src[mid..<hi)` into `dst[lo..<hi)`.
fileprivate func quad_merge_two(
    _ src: UnsafeMutableBufferPointer<Int>,
    _ dst: UnsafeMutableBufferPointer<Int>,
    _ lo: Int,
    _ mid: Int,
    _ hi: Int
) {
    var i = lo
    var j = mid
    var k = lo
    while i < mid && j < hi {
        if src[i] <= src[j] {
            dst[k] = src[i]
            i += 1
        } else {
            dst[k] = src[j]
            j += 1
        }
        k += 1
    }
    while i < mid {
        dst[k] = src[i]
        i += 1
        k += 1
    }
    while j < hi {
        dst[k] = src[j]
        j += 1
        k += 1
    }
}

/// True when four consecutive sorted blocks of length `block` are already ordered
/// across boundaries (skipping the merge is safe).
fileprivate func quad_blocks_ordered(_ a: UnsafeMutableBufferPointer<Int>, _ start: Int, _ block: Int) -> Bool {
    a[start + block - 1] <= a[start + block]
        && a[start + block * 2 - 1] <= a[start + block * 2]
        && a[start + block * 3 - 1] <= a[start + block * 3]
}

/// Ping-pong quad merge: two pairwise merges into swap, then one merge back into `a`.
fileprivate func quad_merge_four(
    _ a: UnsafeMutableBufferPointer<Int>,
    _ swap: UnsafeMutableBufferPointer<Int>,
    _ start: Int,
    _ block: Int
) {
    let mid1 = start + block
    let mid2 = start + block * 2
    let mid3 = start + block * 3
    let end = start + block * 4
    if quad_blocks_ordered(a, start, block) {
        return
    }
    quad_merge_two(a, swap, start, mid1, mid2)
    quad_merge_two(a, swap, mid2, mid3, end)
    quad_merge_two(swap, a, start, mid2, end)
}

/// Binary bottom-up merge for a partial span that is not a full group of four blocks.
fileprivate func quad_merge_remainder(
    _ a: UnsafeMutableBufferPointer<Int>,
    _ swap: UnsafeMutableBufferPointer<Int>,
    _ start: Int,
    _ n: Int,
    _ block: Int
) {
    var width = block
    while start + width < n {
        var lo = start
        while lo + width < n {
            let mid = lo + width
            let hi = min(lo + width * 2, n)
            if a[mid - 1] > a[mid] {
                quad_merge_two(a, swap, lo, mid, hi)
                for i in lo..<hi {
                    a[i] = swap[i]
                }
            }
            lo = hi
        }
        width *= 2
    }
}

func quad_sort(_ a: inout [Int]) {
    a.withUnsafeMutableBufferPointer { quad_sort($0) }
}

func quad_sort(_ a: UnsafeMutableBufferPointer<Int>) {
    let n = a.count
    if n <= 1 {
        return
    }
    if quad_is_sorted(a) {
        return
    }
    if quad_is_reverse_sorted(a) {
        quad_reverse(a)
        return
    }

    // Analyzer / quad-swap: leave sorted blocks of 4 (educational stand-in for 8).
    var i = 0
    while i + 4 <= n {
        quad_swap4(a, i, i + 1, i + 2, i + 3)
        i += 4
    }
    if i < n {
        for j in (i + 1)..<n {
            let key = a[j]
            var k = j
            while k > i && a[k - 1] > key {
                a[k] = a[k - 1]
                k -= 1
            }
            a[k] = key
        }
    }

    var swapStorage = [Int](repeating: 0, count: n)
    swapStorage.withUnsafeMutableBufferPointer { swap in
        var block = 4
        while block < n {
            let stride = block * 4
            var start = 0
            while start < n {
                let rem = n - start
                if rem <= block {
                    break
                }
                if rem >= stride {
                    quad_merge_four(a, swap, start, block)
                    start += stride
                } else {
                    quad_merge_remainder(a, swap, start, n, block)
                    break
                }
            }
            block *= 4
        }
    }
}


func benchmark_sort(_ array: inout [Int]) {

    quad_sort(&array)

}

func is_non_decreasing(_ a: [Int]) -> Bool {
    guard a.count >= 2 else { return true }
    for i in 1..<a.count {
        if a[i - 1] > a[i] { return false }
    }
    return true
}

func same_multiset(_ a: [Int], _ b: [Int]) -> Bool {
    if a.count != b.count {
        return false
    }

    var left = a
    var right = b
    left.sort()
    right.sort()
    return left == right
}

func check_correctness_case(_ label: String, _ input: [Int]) {
    var input = input
    let original = input

    benchmark_sort(&input)

    if !is_non_decreasing(input) {
        fatalError("correctness case \(label): output is not sorted")
    }

    if !same_multiset(input, original) {
        fatalError("correctness case \(label): elements were lost or added")
    }
}

// Skip cases larger than the algorithm's measured size cap (MAX_POWER). That
// cap exists because larger inputs are impractically slow; forcing them here
// would stall the published measurement script before any table rows print.
func check_correctness_case_within_limit(_ label: String, _ input: [Int]) {
    if input.count > (1 << MAX_POWER) {
        return
    }
    check_correctness_case(label, input)
}

func few_unique_values(_ size: Int, _ unique: Int, _ seed: UInt64) -> [Int] {
    var state = seed
    var result = [Int]()
    result.reserveCapacity(size)
    for _ in 0..<size {
        state ^= state << 13
        state ^= state >> 7
        state ^= state << 17
        result.append(Int(state % UInt64(unique)) + 1)
    }
    return result
}

func run_correctness_checks() {
    check_correctness_case("empty", [])
    check_correctness_case("single", [42])
    check_correctness_case("duplicates", [3, 1, 3, 2, 1, 2])
    check_correctness_case("sorted", [1, 2, 3, 4, 5])
    check_correctness_case("reverse", [5, 4, 3, 2, 1])
    check_correctness_case("all_equal", [7, 7, 7, 7])
    check_correctness_case("skewed_range", [1_000_000, 2, 1_000_001, 1, 999_999])
    // Static-buffer Grail skips the in-buffer build when key collection is sparse
    // (ideal_buffer = false). Exercising that path catches regressions in buffer gating.
    check_correctness_case(
        "few_keys_len16",
        [2, 2, 2, 2, 2, 2, 2, 2, 4, 3, 1, 2, 3, 4, 1, 4]
    )
    // Seed 0 is a fixed point of the xorshift below, so it would degenerate into
    // yet another all-equal case instead of a 4-value mix. Start at 1.
    for seed in 1...32 {
        check_correctness_case(
            "few_keys_len32_seed_\(seed)",
            few_unique_values(32, 4, UInt64(seed))
        )
    }
    // Small-input cutoffs (insertion sort below 32 elements, etc.) hide duplicate-key
    // bugs in the recursive path, so repeat the duplicate cases at the smallest
    // benchmark size, which every algorithm must handle within reasonable time.
    check_correctness_case("all_equal_len256", [Int](repeating: 7, count: 256))
    for seed in 1...4 {
        check_correctness_case(
            "few_keys_len256_seed_\(seed)",
            few_unique_values(256, 4, UInt64(seed))
        )
    }
    // Blit's equal-key second sweep used to copy the whole range into a fixed
    // 512-element swap; lengths above that must still sort without panicking.
    // Respect MAX_POWER so algorithms with a low measured-size cap (slow,
    // sleep) do not hang here for minutes or months.
    check_correctness_case_within_limit("all_equal_len600", [Int](repeating: 7, count: 600))
    for seed in 1...4 {
        check_correctness_case_within_limit(
            "few_keys_len2048_seed_\(seed)",
            few_unique_values(2048, 4, UInt64(seed))
        )
    }
}


func shuffled(_ size: Int, seed: UInt64) -> [Int] {
    guard size > 0 else { return [] }

    var v = Array(1...size)
    var state = seed

    if size > 1 {
        for i in stride(from: size - 1, through: 1, by: -1) {
            state ^= state << 13
            state ^= state >> 7
            state ^= state << 17

            let j = Int(state % UInt64(i + 1))
            v.swapAt(i, j)
        }
    }

    return v
}

func micros(_ d: Duration) -> UInt64 {
    let c = d.components
    let fromSeconds = UInt64(c.seconds) * 1_000_000
    let fromAttos = UInt64(max(0, c.attoseconds / 1_000_000_000_000))
    return fromSeconds + fromAttos
}

func padLeft(_ value: String, _ width: Int) -> String {
    if value.count >= width {
        return value
    }
    return String(repeating: " ", count: width - value.count) + value
}

func formatSeconds(_ micros: UInt64) -> String {
    let whole = micros / 1_000_000
    let frac = micros % 1_000_000
    let fracStr = padLeft(String(frac), 6).replacingOccurrences(of: " ", with: "0")
    return "\(whole).\(fracStr)"
}

func input_array(_ size: Int, seed: UInt64) -> [Int] {
    shuffled(size, seed: seed)
}

/// Peak heap growth during `benchmark_sort`, in bytes (explicit buffers such as swap).
/// Kept in bytes so the parent can average before rounding; converting to KiB here
/// would truncate sub-KiB buffers to 0 in every run and hide them from the average.
func run_once(size: Int, seed: Int) -> (UInt64, Int) {
    var array = input_array(size, seed: UInt64(seed))

    let baseBytes = alloc_track_live()
    alloc_track_reset_peak()

    let start = ContinuousClock.now

    benchmark_sort(&array)

    let elapsed = ContinuousClock.now - start
    let peakBytes = alloc_track_peak()
    let auxBytes = max(0, peakBytes - baseBytes)

    let expected: [Int] = size > 0 ? Array(1...size) : []
    if array != expected {
        fatalError("sort failed with seed \(seed) for size \(size)")
    }

    return (micros(elapsed), auxBytes)
}

func run_child(_ args: [String]) {
    let size = Int(args[2])!
    let seed = Int(args[3])!
    let (elapsedUs, mem) = run_once(size: size, seed: seed)
    print("\(elapsedUs) \(mem)")
}

let args = CommandLine.arguments
if args.count > 1 && args[1] == "--run-once" {
    run_child(args)
} else {
    run_correctness_checks()

    let tableHeader =
        "| \(padLeft("Size", 10)) | " +
        "\(padLeft("Average time (s)", 16)) | " +
        "\(padLeft("Maximum time (s)", 16)) | " +
        "\(padLeft("Average memory (KiB)", 20)) | " +
        "\(padLeft("Maximum memory (KiB)", 20)) |"
    print(tableHeader)
    print("|-----------:|-----------------:|-----------------:|---------------------:|---------------------:|")

    for power in MIN_POWER...MAX_POWER {
        let size = 1 << power

        var totalTime: UInt64 = 0
        var maxTime: UInt64 = 0

        var totalMem = 0
        var maxMem = 0

        for seed in 1...RUNS {
            let process = Process()
            process.executableURL = URL(fileURLWithPath: args[0])
            process.arguments = ["--run-once", "\(size)", "\(seed)"]
            let stdout = Pipe()
            let stderr = Pipe()
            process.standardOutput = stdout
            process.standardError = stderr

            do {
                try process.run()
            } catch {
                fatalError("failed to run benchmark child process: \(error)")
            }
            process.waitUntilExit()

            if process.terminationStatus != 0 {
                let err = String(data: stderr.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
                fatalError("benchmark child process failed: \(err)")
            }

            let data = stdout.fileHandleForReading.readDataToEndOfFile()
            let stdoutText = String(data: data, encoding: .utf8) ?? ""
            let fields = stdoutText.split(whereSeparator: \.isWhitespace)
            guard fields.count >= 2,
                  let elapsedUs = UInt64(fields[0]),
                  let auxMem = Int(fields[1]) else {
                fatalError("invalid child process output: \(stdoutText)")
            }

            totalTime += elapsedUs
            if elapsedUs > maxTime {
                maxTime = elapsedUs
            }

            totalMem += auxMem
            if auxMem > maxMem {
                maxMem = auxMem
            }
        }

        let avgTime = totalTime / UInt64(RUNS)
        // Memory is summed in bytes and converted to KiB once, after averaging.
        let avgMemKb = totalMem / RUNS / 1024
        let maxMemKb = maxMem / 1024

        let tableRow =
            "| \(padLeft(String(size), 10)) | " +
            "\(padLeft(formatSeconds(avgTime), 16)) | " +
            "\(padLeft(formatSeconds(maxTime), 16)) | " +
            "\(padLeft(String(avgMemKb), 20)) | " +
            "\(padLeft(String(maxMemKb), 20)) |"
        print(tableRow)
    }
}
SWIFT

RUN clang -O2 -fPIC -shared alloc_track.c -o liballoc_track.so -ldl

RUN swiftc -Ounchecked -whole-module-optimization \
    main.swift \
    -o swift-benchmark \
    -L. -lalloc_track \
    -Xlinker -rpath -Xlinker /app

ENV LD_PRELOAD=/app/liballoc_track.so
CMD ["./swift-benchmark"]
"""#
    try dockerfile.write(
        to: workdir.appendingPathComponent("Dockerfile"),
        atomically: true,
        encoding: .utf8
    )

    // Keeping build and run as separate child processes preserves Docker's
    // normal output and the original image tag used by the benchmark skill.
    try runCommand("docker", ["build", "-t", "swift-benchmark", workdir.path])
    try runCommand("docker", ["run", "--rm", "--init", "swift-benchmark"])
} catch {
    fputs("\(error)\n", stderr)
    exit(1)
}