Add Fisher-Yates Shuffle (algorithmus + shuffle button)

This commit is contained in:
Dieter Schlüter 2026-04-06 20:50:12 +02:00
commit 4db41a147c
2 changed files with 99 additions and 4 deletions

View file

@ -234,7 +234,7 @@ const ALGORITHMS = [
'merge', 'heap', 'quick', 'quick3way', 'dualpivot', 'introsort',
'shell', 'tree', 'timsort',
'counting', 'radix', 'bucket',
'pancake', 'cycle', 'bogo',
'pancake', 'cycle', 'bogo', 'shuffle',
];
const Bogo_MAX_SIZE = 6;
@ -331,6 +331,7 @@ console.log('═'.repeat(50));
const NORMAL_SIZES = [10, 50];
for (const algo of ALGORITHMS) {
if (algo === 'shuffle') continue;
if (verbose) console.log(`\n📦 ${algo}:`);
for (const preset of PRESETS) {
for (const size of NORMAL_SIZES) {
@ -340,6 +341,44 @@ for (const algo of ALGORITHMS) {
}
}
// ── Shuffle-Tests (speziell: nicht sortiert, nur permutiert) ──
if (verbose) console.log('\n📦 shuffle:');
for (const preset of PRESETS) {
for (const size of NORMAL_SIZES) {
totalTests++;
const arr = generatePresetArray(preset, size);
const sortedArr = arr.slice().sort((a, b) => a - b);
context.array = arr.slice();
try {
const steps = buildSteps('shuffle');
const lastStep = steps[steps.length - 1];
if (lastStep.type !== 'done') {
throw new Error(`Letzter Step type '${lastStep.type}', erwartet 'done'`);
}
if (lastStep.array.length !== arr.length) {
throw new Error(`Array-Länge geändert: ${lastStep.array.length} statt ${arr.length}`);
}
const sortedResult = lastStep.array.slice().sort((a, b) => a - b);
if (!arraysEqual(sortedResult, sortedArr)) {
throw new Error(`Werte stimmen nicht überein nach Shuffle`);
}
passed++;
if (verbose) {
console.log(` ✅ shuffle + ${preset} (${size} Elemente, ${steps.length} Steps)`);
}
} catch (e) {
failed++;
failures.push({ algo: 'shuffle', preset, size, error: e.message });
console.log(` ❌ shuffle + ${preset} (${size} Elemente): ${e.message}`);
}
}
}
// Edge Cases
if (verbose) console.log('\n📦 Edge Cases:');