From 6d55f8ae8a1742b8c7e97dc3714da0e99697ebad 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 12:56:11 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=86=92=E6=B3=A1=E6=8E=92?=
=?UTF-8?q?=E5=BA=8F=E7=AE=97=E6=B3=95=EF=BC=8C=E6=8E=92=E5=BA=8F=E7=AE=97?=
=?UTF-8?q?=E6=B3=95=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../algorithm/{sort/quicksort.js => sort.js} | 107 ++++++++++++++----
.../assets/js/algorithm/test.js | 69 +++++++++++
algorithm-visualization/index.html | 3 +
3 files changed, 155 insertions(+), 24 deletions(-)
rename algorithm-visualization/assets/js/algorithm/{sort/quicksort.js => sort.js} (55%)
create mode 100644 algorithm-visualization/assets/js/algorithm/test.js
diff --git a/algorithm-visualization/assets/js/algorithm/sort/quicksort.js b/algorithm-visualization/assets/js/algorithm/sort.js
similarity index 55%
rename from algorithm-visualization/assets/js/algorithm/sort/quicksort.js
rename to algorithm-visualization/assets/js/algorithm/sort.js
index 3c720d2..8855c61 100644
--- a/algorithm-visualization/assets/js/algorithm/sort/quicksort.js
+++ b/algorithm-visualization/assets/js/algorithm/sort.js
@@ -1,7 +1,19 @@
+/**
+ * 排序算法父类 Sort v0.1.0
+ *
+ * @author coder-xiaomo
+ * @date 2022-05-16
+ */
class Sort {
constructor() {
}
+ info() {
+ return {
+ name: "未命名算法"
+ }
+ }
+
sort() {
console.log("this method was not implement.")
}
@@ -29,27 +41,23 @@ class Sort {
// console.log(array, index1, index2)
}
}
+
+
/**
* 快速排序算法 Qucik Sort v0.1.0
*
* @author coder-xiaomo
* @date 2022-05-16
*/
-
-/**
- * step
- *
- * 1. 首先设定一个分界值,通过该分界值将数组分成左右两部分
- * 2. 将大于或等于分界值的数据集中到数组右边,小于的集中到左边
- * 3. 左右两侧的数组分别重复步骤 2.
- * 4. 完成排序
- */
-
class QuickSort extends Sort {
- // static arr
- // constructor(array) {
- // this.arr = array
- // }
+ /**
+ * step
+ *
+ * 1. 首先设定一个分界值,通过该分界值将数组分成左右两部分
+ * 2. 将大于或等于分界值的数据集中到数组右边,小于的集中到左边
+ * 3. 左右两侧的数组分别重复步骤 2.
+ * 4. 完成排序
+ */
// sort(array) {
// if (array.length <= 1)
@@ -59,7 +67,7 @@ class QuickSort extends Sort {
// let left = [], right = []
// for (let i = 1; i < array.length; i++) {
- // const element = array[i];
+ // const element = array[i]
// if (element <= num)
// left.push(element)
// else
@@ -70,6 +78,12 @@ class QuickSort extends Sort {
// return this.sort(left).concat(num, this.sort(right))
// }
+ info() {
+ return {
+ name: "快速排序算法"
+ }
+ }
+
/*
每个(未排序)的部分
将第一个元素设为 pivot
@@ -87,7 +101,7 @@ class QuickSort extends Sort {
return array // 递归出口
const pivot = left // 第一个元素的索引
- let p = left + 1;
+ let p = left + 1
for (let i = left + 1; i <= right; i++) {
if (array[i] < array[pivot]) {
this.swap(array, i, p)
@@ -103,12 +117,57 @@ class QuickSort extends Sort {
}
}
-const quicksort = new QuickSort()
-var result = quicksort.sort([5, 8, 7, 4, 3, 1, 6, 2, 6, 5])
-console.log("result", result)
-console.log()
-var data = []
-for (let i = 0; i < 20; i++) // 生成随机数
- data.push(Math.ceil(Math.random() * 100 - 50))
-console.log("result", quicksort.sort(data))
\ No newline at end of file
+/**
+ * 冒泡排序算法 Qucik Sort v0.1.0
+ *
+ * @author coder-xiaomo
+ * @date 2022-05-16
+ */
+class BubbleSort extends Sort {
+
+ info() {
+ return {
+ name: "冒泡排序算法"
+ }
+ }
+
+ /*
+ 做
+ swapped = false
+ 从 i = 1 到 最后一个没有排序过元素的索引-1
+ 如果 左边元素 > 右边元素
+ 交换(左边元素,右边元素)
+ swapped = true; ++swapCounter(交换计数器)
+ while swapped
+ */
+ sort(array) {
+ let sortTime = 0;
+ let swapped
+ do {
+ swapped = false
+ for (let i = 1; i < array.length - sortTime; i++) {
+ if (array[i - 1] > array[i]) {
+ this.swap(array, i - 1, i)
+ swapped = true
+ }
+ }
+ 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
+ }
+}
+
+
+
diff --git a/algorithm-visualization/assets/js/algorithm/test.js b/algorithm-visualization/assets/js/algorithm/test.js
new file mode 100644
index 0000000..5e70fba
--- /dev/null
+++ b/algorithm-visualization/assets/js/algorithm/test.js
@@ -0,0 +1,69 @@
+
+/**
+ * 排序算法测试
+ */
+
+/**
+ * 创建一个随机数数组
+ * @returns
+ */
+function getRandomData(count = 20) {
+ var randomData = []
+ for (let i = 0; i < count; i++) // 生成随机数
+ randomData.push(Math.ceil(Math.random() * 100 - 50))
+ return randomData
+}
+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),
+]
+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
+]
+
+var sortAlgorithm = {
+ quicksort: new QuickSort(),
+ bubblesort: new BubbleSort(),
+}
+
+// 遍历每一种算法
+Object.values(sortAlgorithm).forEach(sortAlgo => {
+ // 输出调试信息
+ console.log("算法信息", sortAlgo.info())
+ data.forEach(element => {
+ // 将数组元素进行深拷贝
+ var input = JSON.parse(JSON.stringify(element))
+ // 进行排序
+ var result = sortAlgo.sort(input)
+ console.log(input, result)
+ // 与结果进行对比,判断是否正确
+ if (data.indexOf(element) > -1) {
+ var rightSortResult = check[data.indexOf(element)];
+ if (rightSortResult) {
+ if (JSON.stringify(rightSortResult) !== JSON.stringify(result)) {
+ console.warn("👆结果不匹配!正确结果为", rightSortResult)
+ } else {
+ console.info("👆正确")
+ }
+ } else {
+ console.log("👆缺少正确答案,跳过")
+ }
+ }
+ });
+ // 输出一个空行,便于观察
+ console.log("-----------------------------------------------")
+});
diff --git a/algorithm-visualization/index.html b/algorithm-visualization/index.html
index 65a721f..7271a03 100644
--- a/algorithm-visualization/index.html
+++ b/algorithm-visualization/index.html
@@ -59,7 +59,10 @@
+
+
+