mirror of
https://gitee.com/coder-xiaomo/algorithm-visualization
synced 2025-01-10 11:48:18 +08:00
添加快速排序算法
This commit is contained in:
parent
82ccecddaa
commit
4a852b0672
114
algorithm-visualization/assets/js/algorithm/sort/quicksort.js
Normal file
114
algorithm-visualization/assets/js/algorithm/sort/quicksort.js
Normal file
@ -0,0 +1,114 @@
|
||||
class Sort {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
sort() {
|
||||
console.log("this method was not implement.")
|
||||
}
|
||||
|
||||
/**
|
||||
* 交换数组中的两个元素
|
||||
* @param {*} array 数组
|
||||
* @param {*} index1 元素1索引
|
||||
* @param {*} index2 元素2索引
|
||||
*/
|
||||
swap(array, index1, index2) {
|
||||
if (index1 < 0 || index1 >= array.length)
|
||||
console.log("index1索引超限")
|
||||
if (index2 < 0 || index2 >= array.length)
|
||||
console.log("index2索引超限")
|
||||
if (index1 == index2)
|
||||
return
|
||||
|
||||
let tmp = array[index1]
|
||||
array[index1] = array[index2]
|
||||
array[index2] = tmp
|
||||
|
||||
// only for test
|
||||
// console.log(Object.keys(array))
|
||||
// console.log(array, index1, index2)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 快速排序算法 Qucik Sort v0.1.0
|
||||
*
|
||||
* @author coder-xiaomo
|
||||
* @date 2022-05-16
|
||||
*/
|
||||
|
||||
/**
|
||||
* step
|
||||
*
|
||||
* 1. 首先设定一个分界值,通过该分界值将数组分成左右两部分
|
||||
* 2. 将大于或等于分界值的数据集中到数组右边,小于的集中到左边
|
||||
* 3. 左右两侧的数组分别重复步骤 2.
|
||||
* 4. 完成排序
|
||||
*/
|
||||
|
||||
class QuickSort extends Sort {
|
||||
// static arr
|
||||
// constructor(array) {
|
||||
// this.arr = array
|
||||
// }
|
||||
|
||||
// sort(array) {
|
||||
// if (array.length <= 1)
|
||||
// return array
|
||||
|
||||
// const num = array[0]
|
||||
// let left = [], right = []
|
||||
|
||||
// for (let i = 1; i < array.length; i++) {
|
||||
// const element = array[i];
|
||||
// if (element <= num)
|
||||
// left.push(element)
|
||||
// else
|
||||
// right.push(element)
|
||||
// }
|
||||
|
||||
// console.log(num, left, right)
|
||||
// return this.sort(left).concat(num, this.sort(right))
|
||||
// }
|
||||
|
||||
/*
|
||||
每个(未排序)的部分
|
||||
将第一个元素设为 pivot
|
||||
存储索引 = pivot索引 +1
|
||||
从 i=pivot指数 +1 到 最右索引 的遍历
|
||||
如果 a[i] < a[pivot]
|
||||
交换 (i, 存储索引); 存储索引++;
|
||||
交换(pivot, 存储索引 - 1)
|
||||
*/
|
||||
sort(array, left, right) {
|
||||
if (typeof (left) === "undefined") left = 0
|
||||
if (typeof (right) === "undefined") right = array.length - 1
|
||||
|
||||
if (right <= left)
|
||||
return array // 递归出口
|
||||
|
||||
const pivot = left // 第一个元素的索引
|
||||
let p = left + 1;
|
||||
for (let i = left + 1; i <= right; i++) {
|
||||
if (array[i] < array[pivot]) {
|
||||
this.swap(array, i, p)
|
||||
p++
|
||||
}
|
||||
}
|
||||
this.swap(array, pivot, p - 1)
|
||||
|
||||
// 左、右分别排序(索引超限在 sort 内部进行处理,此处无需判断)
|
||||
this.sort(array, pivot, p - 2)
|
||||
this.sort(array, p, right)
|
||||
return array
|
||||
}
|
||||
}
|
||||
|
||||
const quicksort = new QuickSort()
|
||||
var result = quicksort.sort([5, 8, 7, 4, 3, 1, 6, 2, 6, 5])
|
||||
console.log("result", result)
|
||||
|
||||
console.log()
|
||||
var data = []
|
||||
for (let i = 0; i < 20; i++) // 生成随机数
|
||||
data.push(Math.ceil(Math.random() * 100 - 50))
|
||||
console.log("result", quicksort.sort(data))
|
@ -58,6 +58,8 @@
|
||||
<script src="./assets/js/class.js"></script>
|
||||
|
||||
<script src="./assets/js/index.js"></script>
|
||||
|
||||
<script src="./assets/js/algorithm/sort.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user