From fc9fed1ecb289e7bac64900ba663b35f354dc679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Mon, 16 May 2022 13:12:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=89=E6=8B=A9=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E7=AE=97=E6=B3=95=EF=BC=9B=E8=B0=83=E6=95=B4=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../assets/js/algorithm/sort.js | 61 +++++++++++++------ .../assets/js/algorithm/test.js | 16 ++--- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/algorithm-visualization/assets/js/algorithm/sort.js b/algorithm-visualization/assets/js/algorithm/sort.js index 8855c61..6d02b16 100644 --- a/algorithm-visualization/assets/js/algorithm/sort.js +++ b/algorithm-visualization/assets/js/algorithm/sort.js @@ -14,8 +14,9 @@ class Sort { } } - sort() { + sort(array) { console.log("this method was not implement.") + return array } /** @@ -87,11 +88,11 @@ class QuickSort extends Sort { /* 每个(未排序)的部分 将第一个元素设为 pivot - 存储索引 = pivot索引 +1 - 从 i=pivot指数 +1 到 最右索引 的遍历 - 如果 a[i] < a[pivot] - 交换 (i, 存储索引); 存储索引++; - 交换(pivot, 存储索引 - 1) + 存储索引 = pivot索引 +1 + 从 i=pivot指数 +1 到 最右索引 的遍历 + 如果 a[i] < a[pivot] + 交换 (i, 存储索引); 存储索引++; + 交换(pivot, 存储索引 - 1) */ sort(array, left, right) { if (typeof (left) === "undefined") left = 0 @@ -119,7 +120,7 @@ class QuickSort extends Sort { /** - * 冒泡排序算法 Qucik Sort v0.1.0 + * 冒泡排序算法 Bubble Sort v0.1.0 * * @author coder-xiaomo * @date 2022-05-16 @@ -142,7 +143,7 @@ class BubbleSort extends Sort { while swapped */ sort(array) { - let sortTime = 0; + let sortTime = 0 let swapped do { swapped = false @@ -154,20 +155,44 @@ class BubbleSort extends Sort { } sortTime++ } while (swapped) - // let swapCounter = array.length - 1; - // while (swapCounter > 0) { - // console.log(swapCounter) - // for (let i = 1; i < array.length - swapCounter; i++) { - // if (array[i - 1] > array[i]) { - // this.swap(array, i - 1, i) - // } - // } - // swapCounter--; - // } return array } } +/** + * 选择排序算法 Selection Sort v0.1.0 + * + * @author coder-xiaomo + * @date 2022-05-16 + */ +class SelectionSort extends Sort { + info() { + return { + name: "选择排序算法" + } + } + + /* + 重复(元素个数-1)次 + 把第一个没有排序过的元素设置为最小值 + 遍历每个没有排序过的元素 + 如果元素 < 现在的最小值 + 将此元素设置成为新的最小值 + 将最小值和第一个没有排序过的位置交换 + */ + sort(array) { + let minIndex + for (let i = 0; i < array.length; i++) { + minIndex = i + for (let j = i + 1; j < array.length; j++) { + if (array[minIndex] > array[j]) { + this.swap(array, minIndex, j) + } + } + } + return array + } +} diff --git a/algorithm-visualization/assets/js/algorithm/test.js b/algorithm-visualization/assets/js/algorithm/test.js index 5e70fba..5692c73 100644 --- a/algorithm-visualization/assets/js/algorithm/test.js +++ b/algorithm-visualization/assets/js/algorithm/test.js @@ -16,28 +16,30 @@ function getRandomData(count = 20) { var data = [ [], [-1], - [1, 1, 1, 1, 1, 1, 1], - [5, 8, 7, 4, 3, 1, 6, 2, 6, 5], [1, 2], [1, 2, 3, 4, 5, 6, 7, 8], [8, 7, 6, 5, 4, 3, 2, 1], - getRandomData(6), + [1, 1, 1, 1, 1, 1, 1], + [5, 8, 7, 4, 3, 1, 6, 2, 6, 5], + [47, 11, 50, 13, 16, 49, 8, 9, 38, 27, 20], + // getRandomData(6), ] var check = [ [], [-1], - [1, 1, 1, 1, 1, 1, 1], - [1, 2, 3, 4, 5, 5, 6, 6, 7, 8], [1, 2], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8], - null, - null + [1, 1, 1, 1, 1, 1, 1], + [1, 2, 3, 4, 5, 5, 6, 6, 7, 8], + [8, 9, 11, 13, 16, 20, 27, 38, 47, 49, 50], + // null ] var sortAlgorithm = { quicksort: new QuickSort(), bubblesort: new BubbleSort(), + selectionSort: new SelectionSort(), } // 遍历每一种算法