!1 实现 快速排序算法

Merge pull request !1 from 程序员小墨/feature-random-quicksort
This commit is contained in:
程序员小墨 2022-08-08 07:47:41 +00:00 committed by Gitee
commit 7c62e215c5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 83 additions and 12 deletions

View File

@ -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
}
}

View File

@ -41,6 +41,7 @@ var sortAlgorithm = {
bubblesort: new BubbleSort(animation),
selectionSort: new SelectionSort(animation),
insertionSort: new InsertionSort(animation),
randomQuickSort: new RandomQuickSort(animation),
}
// 遍历每一种算法

View File

@ -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)

View File

@ -79,7 +79,7 @@
<script defer src="./assets/js/index.js"></script>
<!-- 算法测试 -->
<!-- <script src="./assets/js/algorithm/test.js"></script> -->
<!-- <script defer src="./assets/js/algorithm/test.js"></script> -->
</body>
</html>