1
0
mirror of https://gitee.com/coder-xiaomo/algorithm-visualization synced 2025-01-10 11:48:18 +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 // 第一个元素的索引
let p = left + 1
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]) {
this.swap(array, i, p)
this.swapAnimation(customAttr.id, i, p)
@ -200,7 +200,7 @@ class BubbleSort extends Sort {
do {
swapped = false
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]) {
this.swap(array, i - 1, i)
this.swapAnimation(customAttr.id, i - 1, i)
@ -249,4 +249,19 @@ class SelectionSort extends Sort {
}
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",
oneUnit: oneUnit,
gsapTimeline: gsap.timeline({
onStart:function(){
consoleClear()
},
onComplete: function () {
consoleLog(`排序完成`)
console.log("all done")
@ -332,7 +335,7 @@ class VectorAnimation {
// 比较数组元素
compareLinkedListItems(id, index1, index2) {
highlightLinkedListItems(id, [index1, index2], function () {
this.highlightLinkedListItems(id, [index1, index2], function () {
consoleLog(`比较索引为 ${index1}${index2} 的两个元素`)
})
}

View File

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