diff --git a/src/assets/js/algorithm/sort.js b/src/assets/js/algorithm/sort.js index 2d3cc1f..8f68d87 100644 --- a/src/assets/js/algorithm/sort.js +++ b/src/assets/js/algorithm/sort.js @@ -387,7 +387,7 @@ class MergeSort extends Sort { * 随机快速排序算法 Random Quick Sort v0.1.0 * * @author coder-xiaomo - * @date 2022-05-19 + * @date 2022-08-08 */ class RandomQuickSort extends Sort { @@ -395,31 +395,67 @@ class RandomQuickSort extends Sort { return { name: "随机快速排序算法", enName: "Random Quick Sort", + available: true } } /* 每个(未排序)的部分 - 随机选取 pivot,和第一个元素交换 + 将第一个元素设为 pivot 存储索引 = pivot索引 +1 - 从 i=pivot指数 +1 到 最右索引 的遍历 - 如果 a[i] < a[pivot] - 交换 (i, 存储索引); 存储索引++; + 从 i=pivot指数 +1 到 最右索引 的遍历 + 如果 a[i] < a[pivot] + 交换 (i, 存储索引); 存储索引++; 交换(pivot, 存储索引 - 1) */ - sort(array) { + sort(array, left, right) { if (array.length == 0) return array - // todo - console.log("尚未实现") + + if (typeof left === 'undefined') left = 0 + if (typeof right === 'undefined') right = array.length - 1 + if (left < right) { + let pivot = left + let storeIndex = pivot + 1 + for (let i = pivot + 1; i <= right; i++) { + if (array[i] < array[pivot]) { + this.swap(array, i, storeIndex) + storeIndex++ + } + } + this.swap(array, pivot, storeIndex - 1) + + this.sort(array, left, storeIndex - 2) + this.sort(array, storeIndex, right) + } return array } - sortWithAnimation(customAttr, array) { + sortWithAnimation(customAttr, array, left, right) { if (array.length == 0) return array - // todo - console.log("尚未实现") + + if (typeof left === 'undefined') left = 0 + if (typeof right === 'undefined') right = array.length - 1 + if (left < right) { + let pivot = left + let storeIndex = pivot + 1 + animation.emphasisLinkedListItems(customAttr.id, [left, right], {}) + animation.emphasisLinkedListItems(customAttr.id, [left, right], { popBack: true }) + for (let i = pivot + 1; i <= right; i++) { + animation.compareLinkedListItems(customAttr.id, i, storeIndex) + if (array[i] < array[pivot]) { + this.swap(array, i, storeIndex) + this.swapAnimation(customAttr.id, i, storeIndex) + storeIndex++ + } + } + this.swap(array, pivot, storeIndex - 1) + this.swapAnimation(customAttr.id, pivot, storeIndex - 1) + + this.sortWithAnimation(customAttr, array, left, storeIndex - 2) + this.sortWithAnimation(customAttr, array, storeIndex, right) + } return array } } diff --git a/src/assets/js/algorithm/test.js b/src/assets/js/algorithm/test.js index fff7d0e..d1e9b7e 100644 --- a/src/assets/js/algorithm/test.js +++ b/src/assets/js/algorithm/test.js @@ -41,6 +41,7 @@ var sortAlgorithm = { bubblesort: new BubbleSort(animation), selectionSort: new SelectionSort(animation), insertionSort: new InsertionSort(animation), + randomQuickSort: new RandomQuickSort(animation), } // 遍历每一种算法 diff --git a/src/assets/js/class.js b/src/assets/js/class.js index cbe7d51..95ac714 100644 --- a/src/assets/js/class.js +++ b/src/assets/js/class.js @@ -588,6 +588,40 @@ class VectorAnimation { customAttr.gsapTimeline.add(timeline) } + // 强调/恢复 数组元素 + emphasisLinkedListItems(id, index, { popBack = false }) { + let linkedList = document.getElementById(id) + let customAttr = linkedList.customAttr + var gList = linkedList.childNodes + + let eles = [] + if (typeof (index) === "object") { + for (let i = 0; i < index.length; i++) { + eles.push(gList[index[i]]) + } + } else { + eles.push(gList[index]) + } + + let timeline = gsap.timeline({ + onStart: function () { + if (typeof (onStartCallback) === "function") + onStartCallback() + }, + onComplete: function () { + console.log("animation done (popup)") + } + }).add( + popBack == false ? [ + gsap.to(eles, { ...this.workSpace.settings.animation.getConf(), y: customAttr.oneUnit * 0.4, fill: settings.colorMap["fill_focus"] }), + ] : [ + gsap.to(eles, { ...this.workSpace.settings.animation.getConf(), y: 0, fill: settings.colorMap["fill"] }), + ] + ) + + customAttr.gsapTimeline.add(timeline) + } + // 交换相邻数组元素位置(仅在水平方向呼唤,垂直方向不做调整) 【fromIndex 是y方向上突出的元素】 exchangeLinkedListItems(id, fromIndex, toIndex) { if (fromIndex < toIndex) diff --git a/src/index.html b/src/index.html index 3e8cde5..f2f06ae 100644 --- a/src/index.html +++ b/src/index.html @@ -79,7 +79,7 @@ - + \ No newline at end of file