From c24fe5cb8bb3e30a003a16103f1e2d646dbccccd Mon Sep 17 00:00:00 2001 From: Alexander Kimmig Date: Mon, 17 Nov 2025 09:56:56 +0000 Subject: [PATCH] SelectionSort --- 02 Sortieralgorithmen/01 SelectionSort.ipynb | 400 +++++++++++++++++++ 1 file changed, 400 insertions(+) create mode 100644 02 Sortieralgorithmen/01 SelectionSort.ipynb diff --git a/02 Sortieralgorithmen/01 SelectionSort.ipynb b/02 Sortieralgorithmen/01 SelectionSort.ipynb new file mode 100644 index 0000000..3b7a161 --- /dev/null +++ b/02 Sortieralgorithmen/01 SelectionSort.ipynb @@ -0,0 +1,400 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "4d0577ad-58c8-43b9-acf1-e1c29f455600", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "import { assertEquals } from \"jsr:@std/assert\";\n", + "import \"https://git.amgdhg.de/kg/tslib/raw/branch/main/logger.ts\"" + ] + }, + { + "cell_type": "markdown", + "id": "e6f8e242-3a62-41ce-a36f-fe47c7d49528", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "# Minimumsuche\n", + "\n", + "Bei der Minimumsuche sucht man das kleinste Element in einer Liste bzw. einem Array." + ] + }, + { + "cell_type": "markdown", + "id": "8699de81-f9a2-446a-8680-bb375e9a2f80", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "Beschreibe zunächst in eigenen Worten, wie die Minimumsuche funktioniert, also wie in einem gegebenen, unsortierten Array der kleinste Wert herausgefunden werden kann:" + ] + }, + { + "cell_type": "raw", + "id": "5ae9059b-e617-4b07-a1dd-4ba4e82bf09a", + "metadata": { + "editable": true, + "raw_mimetype": "", + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [] + }, + { + "cell_type": "markdown", + "id": "57cfea67-8905-4a13-a01a-089283cbcaf9", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "## Aufgabe 02.01.1: Zufallszahlengenerator\n", + "\n", + "Programmiere einen Funktion `zufall`, die\n", + "* einen Parameter `laenge` übergeben bekommt und\n", + "* ein Array der mit `laenge` zufällig erzeugten Zahlen zurückgibt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8525ccd-859d-44df-9806-a6ac11ccfa66", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28ec1840-3feb-4744-9893-573c0cc4f81b", + "metadata": { + "editable": false, + "jupyter": { + "source_hidden": true + }, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "let _nr = \"02.01.1\"\n", + "Deno.test(`${_nr}: function`, () => {\n", + " assertEquals(typeof zufall, 'function')\n", + "})\n", + "Deno.test(`${_nr}: Parameter`, () => {\n", + " assertEquals(zufall.length, 1)\n", + "})\n", + "Deno.test(`${_nr}: zufall(0)=[]`, () => {\n", + " assertEquals(zufall(0), [])\n", + "})\n", + "Deno.test(`${_nr}: zufall(1)`, () => {\n", + " let last = 0\n", + " for(let i = 0; i < 10; i++) {\n", + " let now = zufall(1)\n", + " assertEquals(now.length, 1)\n", + " assertEquals(now[0] >= 0, true)\n", + " assertEquals(now[0] <= 1, true)\n", + " assertEquals(now[0] === last, false)\n", + " last = now[0]\n", + " }\n", + "})\n", + "Deno.test(`${_nr}: zufall(10)`, () => {\n", + " let arr = zufall(10)\n", + " assertEquals(arr.length, 10)\n", + " for (let i = 1; i < arr.length; i++) {\n", + " assertEquals(arr[i] >= 0, true)\n", + " assertEquals(arr[i] <= 1, true)\n", + " if (i > 1) assertEquals(arr[i] === arr[i-1], false)\n", + " }\n", + "})\n", + "Deno.test(`${_nr}: zufall(1000)`, () => {\n", + " let arr = zufall(1000)\n", + " assertEquals(arr.length, 1000)\n", + " for (let i = 1; i < arr.length; i++) {\n", + " assertEquals(arr[i] >= 0, true)\n", + " assertEquals(arr[i] <= 1, true)\n", + " if (i > 1) assertEquals(arr[i] === arr[i-1], false)\n", + " }\n", + "})" + ] + }, + { + "cell_type": "markdown", + "id": "cb89cc31-62b7-439d-843f-b1cbf872bc80", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "## Aufgabe 02.01.2: Minimumsuche\n", + "\n", + "Programmiere eine Funktion `minimum`:\n", + "* die einen Parameter `liste` übergeben bekommt. Dieser Parameter enthält ein Array mit Zahlen.\n", + "* Zurückgegeben werden soll der kleinste Wert des Arrays.\n", + "\n", + "*Anmerkung: es gibt auch die `Math.min` Funktion, diese soll hier aber nicht benutzt werden!*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f746983-b432-45f3-b6a5-3b27764f69e3", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9611c7d1-a9ea-4332-b189-0df85955ed74", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "let liste = zufall(10) // Array erzeugen\n", + "console.log(liste) // Array ausgeben\n", + " // kleinster Wert suchen und ausgeben\n", + "console.log(\"kleinster Wert: \" + minimum(liste))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43c999ec-2a4e-4f42-bab1-314b077420d4", + "metadata": { + "editable": false, + "jupyter": { + "source_hidden": true + }, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "let _nr = \"02.01.2\"\n", + "Deno.test(`${_nr}: function`, () => {\n", + " assertEquals(typeof minimum, 'function')\n", + "})\n", + "Deno.test(`${_nr}: Parameter`, () => {\n", + " assertEquals(minimum.length, 1)\n", + "})\n", + "Deno.test(`${_nr}: minimum`, () => {\n", + " let arr = zufall(20)\n", + " assertEquals(arr.length, 20)\n", + " assertEquals(minimum(arr), Math.min(...arr))\n", + "})" + ] + }, + { + "cell_type": "markdown", + "id": "5582abff-8ada-422d-ac2b-130f0d715ace", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "## Aufgabe 02.01.3: Sortierte Ausgabe\n", + "\n", + "Überlege dir, wie man diese Minimumsuche dazu verwenden könnte, alle Einträge des Arrays sortiert auszugeben.\n", + "\n", + "Programmiere eine Funktion `sortiert`, die ein Array als Parameter bekommt und in sortierter Reihenfolge jeden einzelne Zahl ausgibt.\n", + "\n", + "#### Tipp:\n", + "Mit `liste.splice(i,c)` lassen sich aus dem Array `liste` von der Position `i` aus insgesamt `c` Einträge löschen.\n", + "\n", + "*Hinweis: es gibt auch die `sort`-Funktion, die soll hier aber nicht benutzt werden!*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29b54eef-7b73-4c5c-91fd-90332451b4fb", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a853f29b-4f3b-4032-9b56-e9f3dbe61e60", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "sortiert(liste)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4e1c39ce-e9f3-44f9-b1ec-47e597b9fcaa", + "metadata": { + "editable": false, + "jupyter": { + "source_hidden": true + }, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "let _nr = \"02.01.3\"\n", + "Deno.test(`${_nr}: function`, () => {\n", + " assertEquals(typeof sortiert, 'function')\n", + "})\n", + "Deno.test(`${_nr}: Parameter`, () => {\n", + " assertEquals(sortiert.length, 1)\n", + "})\n", + "Deno.test(`${_nr}: sortiert`, () => {\n", + " let arr = zufall(4)\n", + " console.start()\n", + " sortiert(arr)\n", + " let res = console.end().split('\\n')\n", + " assertEquals(res, arr.sort().map(e => `${e}`))\n", + "})" + ] + }, + { + "cell_type": "markdown", + "id": "fbded430-a6a0-485e-bff7-375e9053008c", + "metadata": { + "editable": false, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "source": [ + "## Aufgabe 02.01.4: Funktion `sort`\n", + "\n", + "Programmiere eine Funktion `sort`, die ein unsortiertes Array als Parameter bekommt und als Ergebnis die Werte in sortierter Reihenfolge zurückgibt." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92580ecc-329b-4778-b92a-7c54626796ea", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebda8fca-b380-41e9-8ce0-f7e8232c0a4f", + "metadata": { + "editable": false, + "jupyter": { + "source_hidden": true + }, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "let _nr = \"02.01.4\"\n", + "Deno.test(`${_nr}: function`, () => {\n", + " assertEquals(typeof sort, 'function')\n", + "})\n", + "Deno.test(`${_nr}: Parameter`, () => {\n", + " assertEquals(sort.length, 1)\n", + "})\n", + "Deno.test(`${_nr}: sort`, () => {\n", + " let arr = zufall(10)\n", + " let tmp = arr.slice().sort()\n", + " let res = sort(arr)\n", + " assertEquals(tmp, res)\n", + "})" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Deno", + "language": "typescript", + "name": "deno" + }, + "language_info": { + "codemirror_mode": "typescript", + "file_extension": ".ts", + "mimetype": "text/x.typescript", + "name": "typescript", + "nbconvert_exporter": "script", + "pygments_lexer": "typescript", + "version": "5.8.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}