JavaScript - Ein Array von Zahlen mit dem Bubblesort-Algorithmus sortieren
/* Sortiert ein Array von Zahlen mit dem Bubblesort-Algorithmus. */ const bubbleSort = (array) => { let swapped = false; const a = [...array]; for (let i = 1; i < a.length; i++) { swapped = false; for (let j = 0; j < a.length - i; j++) { if (a[j + 1] < a[j]) { [a[j], a[j + 1]] = [a[j + 1], a[j]]; swapped = true; } } if (!swapped) return a; } return a; }; /* Beispiele: bubbleSort([2, 1, 4, 3]); // [1, 2, 3, 4] */
Bausteine Alle Anzeigen
Eine zufällige Auswahl von Codeschnipseln
aus den Bereichen
HTML, CSS,
PHP, JavaScript und
MySQL.
<form> Formular
CSS - Display-Flex mit Tabellen
PHP - Suchen und hervorheben mit preg_replace_callback()
JavaScript - Automatische Weiterleitung nach einigen Sekunden
MySQL - Abrufen eines zweispaltigen Ergebnisses in ein Array