javascript - 快速排序算法javascript
问题描述
我很难理解在分区函数中没有返回数组的情况下传递和操作数组的方式。分区函数只返回 partitionIndex 但数组无论如何都在更改。谁能解释
function QuickSort(arr,start,end,type){
console.log("_____________________________ "+type+" "+start+" "+end+" arr: "+arr);
if(start < end){
var pivotIndex = Partition(arr,start,end);
console.log("pivotIndex: ",pivotIndex);
QuickSort(arr,start,pivotIndex-1,'inner1');
QuickSort(arr,pivotIndex+1,end,'inner2');
}
return arr;
}
function Partition(arr,start,end){
console.log("Partition");
var pivot = arr[end];
var partitionIndex = start;
for(var x = start; x < end; x++ ){
if(arr[x] <= pivot){
swap(arr,x,partitionIndex)
partitionIndex++;
}
}
console.log("#####",arr)
swap(arr,partitionIndex,end)
console.log("@@@@@",arr)
return partitionIndex;
}
function swap(arr,firstIndex,secondIndex){
var temp = arr[firstIndex];
arr[firstIndex] = arr[secondIndex];
arr[secondIndex] = temp;
}
console.log(QuickSort(arr,start,end,'main'));
解决方案
推荐阅读
- android - com.google.firebase.storage.StorageException:用户无权访问此对象
- excel - 基于一周内小时数的多个类别的前 3 个值
- php - 如果文件没有扩展名,如何检查 PHP 文件上传?
- python - 无法将元组转换为字符串我该怎么办?
- java - DiscountSavings() 方法使用 0 而不是赋值
- node.js - 为什么“npm start”命令在成功构建后不运行 TypeScript 应用程序
- docker - 当应用程序位于单独的 docker 容器中时将它们连接到数据库
- javascript - 在 div 中激活元素的侦听器而不激活 div 的侦听器
- java - 将复杂的 JSON 转换为 JAVA 对象,其中不同的键将转换为相同类型的对象
- mysql - 检查模板中的变量条件 [or,and]