gpt4 book ai didi

javascript - 使用Vue.js可视化快速排序编程算法(无法处理Promises)

转载 作者:搜寻专家 更新时间:2023-10-30 22:51:15 25 4
gpt4 key购买 nike

我只是出于自学目的尝试使用 Vue.js 可视化快速排序算法,我无法处理 async/await。填充和洗牌数组工作正常(虽然它可能实现错误,我不知道。)

排序工作很奇怪。我想这是因为递归中的异步/等待。但我实际上不是程序员,只了解一些基础知识,因此将不胜感激。片段中的代码对您来说可能看起来很难看:)

// noprotect

var app = new Vue({
el: '#app',
data: {
numArray: []
},
methods: {
fillArray: function(event) {
for (var i = 0; i<30; i++) {
this.numArray.push(i);
}
},
shuffleArray: function(event) {
var tempArray = shuffleArray(this.numArray);
this.numArray = tempArray;
},
sortArray: function(event) {
this.numArray = quickSort(this.numArray,0,this.numArray.length-1)
console.log(this.numArray);
}
}
})
Vue.config.devtools = false;

function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms));
};

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

function shuffleArray(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = array[currentIndex];
Vue.set(array,currentIndex,array[randomIndex]);
Vue.set(array,randomIndex,temporaryValue);
}

return array;
}


function quickSort(arr, left, right) {
var len = arr.length,
pivot,
partitionIndex;


if (left < right) {
pivot = right;
partitionIndex = partition(arr, pivot, left, right);

quickSort(arr, left, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, right);
}
return arr;
}

async function partition(arr, pivot, left, right) {
var pivotValue = arr[pivot],
partitionIndex = left;

for (var i = left; i < right; i++) {
if (arr[i] < pivotValue) {
swap(arr, i, partitionIndex);
await sleep(100);
partitionIndex++;
}
}
swap(arr, right, partitionIndex);
await sleep(100);
return partitionIndex;
}

function swap(arr, i, j) {
var temporaryValue = arr[i];

Vue.set(arr,i,arr[j]);
Vue.set(arr,j,temporaryValue);
}
body {
font-family: sans-serif;
font-size: 12px;
font-weight: 700;
}

.container {
display: block;
clear: both;
overflow: hidden;
}

.el {
border: 1px solid lightgray;
float: left;
width: 2%;
text-align: center;
padding: 10px 0;
width: 25px;
margin: 0 2px;
}


.btn {
display: block;
margin-top: 20px;
clear: both;
border: 2px solid orange;
text-align: center;
padding-top: 10px;
padding-bottom: 10px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="app">
<div class="container" id="scene1">
<div class="el" v-for="item in numArray">{{ item }}</div>
</div>
<a href="#" v-on:click.prevent="fillArray" class="btn">Fill array</a>
<a href="#" v-on:click.prevent="shuffleArray" class="btn">Shuffle array</a>
<a href="#" v-on:click.prevent="sortArray" class="btn">Sort array</a>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
</body>
</html>

最佳答案

async partition 函数返回一个 promise,而不是一个整数。您需要等待它,同时使 quicksort 异步。

async function quickSort(arr, left, right) {
const len = arr.length;

if (left < right) {
const pivot = right;
const partitionIndex = await partition(arr, pivot, left, right);

await quickSort(arr, left, partitionIndex - 1);
await quickSort(arr, partitionIndex + 1, right);
}
return arr;
}

应用的 sortArray 方法也是如此。

关于javascript - 使用Vue.js可视化快速排序编程算法(无法处理Promises),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com