我试图用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>

最佳答案

asyncpartition函数返回的是一个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方法也是如此。

07-25 20:49