1
0
mirror of https://gitee.com/coder-xiaomo/algorithm-visualization synced 2025-09-06 12:51:39 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

添加ConsoleClear方法

This commit is contained in:
2022-05-18 19:30:15 +08:00
parent 1d0c253db4
commit e3f47b5bc3
3 changed files with 34 additions and 11 deletions

View File

@@ -136,7 +136,7 @@ class QuickSort extends Sort {
const pivot = left // 第一个元素的索引 const pivot = left // 第一个元素的索引
let p = left + 1 let p = left + 1
for (let i = left + 1; i <= right; i++) { for (let i = left + 1; i <= right; i++) {
animation.highlightLinkedListItems(customAttr.id, [i, pivot]) animation.compareLinkedListItems(customAttr.id, i, pivot)
if (array[i] < array[pivot]) { if (array[i] < array[pivot]) {
this.swap(array, i, p) this.swap(array, i, p)
this.swapAnimation(customAttr.id, i, p) this.swapAnimation(customAttr.id, i, p)
@@ -200,7 +200,7 @@ class BubbleSort extends Sort {
do { do {
swapped = false swapped = false
for (let i = 1; i < array.length - sortTime; i++) { for (let i = 1; i < array.length - sortTime; i++) {
animation.highlightLinkedListItems(customAttr.id, [i - 1, i]) animation.compareLinkedListItems(customAttr.id, i - 1, i)
if (array[i - 1] > array[i]) { if (array[i - 1] > array[i]) {
this.swap(array, i - 1, i) this.swap(array, i - 1, i)
this.swapAnimation(customAttr.id, i - 1, i) this.swapAnimation(customAttr.id, i - 1, i)
@@ -249,4 +249,19 @@ class SelectionSort extends Sort {
} }
return array return array
} }
sortWithAnimation(array) {
let minIndex
for (let i = 0; i < array.length; i++) {
minIndex = i
for (let j = i + 1; j < array.length; j++) {
animation.compareLinkedListItems(customAttr.id, minIndex, j)
if (array[minIndex] > array[j]) {
this.swap(array, minIndex, j)
this.swapAnimation(customAttr.id, minIndex, j)
}
}
}
return array
}
} }

View File

@@ -162,6 +162,9 @@ class Shape {
type: "linkedList", type: "linkedList",
oneUnit: oneUnit, oneUnit: oneUnit,
gsapTimeline: gsap.timeline({ gsapTimeline: gsap.timeline({
onStart:function(){
consoleClear()
},
onComplete: function () { onComplete: function () {
consoleLog(`排序完成`) consoleLog(`排序完成`)
console.log("all done") console.log("all done")
@@ -332,7 +335,7 @@ class VectorAnimation {
// 比较数组元素 // 比较数组元素
compareLinkedListItems(id, index1, index2) { compareLinkedListItems(id, index1, index2) {
highlightLinkedListItems(id, [index1, index2], function () { this.highlightLinkedListItems(id, [index1, index2], function () {
consoleLog(`比较索引为 ${index1}${index2} 的两个元素`) consoleLog(`比较索引为 ${index1}${index2} 的两个元素`)
}) })
} }

View File

@@ -88,16 +88,21 @@ initialize()
// 临时输出 // 临时输出
function consoleLog(data) { function consoleLog(data) {
console.log(data)
let consoleList = settings.console.consoleList let consoleList = settings.console.consoleList
consoleList.push(data) if (data) {
if (consoleList.length > settings.console.maxLine + 1) { consoleList.push(data)
consoleList.shift() if (consoleList.length > settings.console.maxLine + 1) {
consoleList[0] = "<small>更早的记录已经被丢掉啦</small>" consoleList.shift()
consoleList[0] = "<small>更早的记录已经被丢掉啦</small>"
}
} }
let container = document.getElementById("console-logs") let consoleContainer = document.getElementById("console-logs")
container.innerHTML = consoleList.join("<br>") consoleContainer.innerHTML = consoleList.join("<br>")
container.scrollTop = container.scrollHeight consoleContainer.scrollTop = container.scrollHeight
}
function consoleClear() {
settings.console.consoleList = []
consoleLog()
} }
function displayCurrentArray(array) { function displayCurrentArray(array) {