mirror of
https://gitee.com/coder-xiaomo/algorithm-visualization
synced 2025-01-10 11:48:18 +08:00
添加选择排序算法;调整测试用例
This commit is contained in:
parent
6d55f8ae8a
commit
fc9fed1ecb
@ -14,8 +14,9 @@ class Sort {
|
||||
}
|
||||
}
|
||||
|
||||
sort() {
|
||||
sort(array) {
|
||||
console.log("this method was not implement.")
|
||||
return array
|
||||
}
|
||||
|
||||
/**
|
||||
@ -87,11 +88,11 @@ class QuickSort extends Sort {
|
||||
/*
|
||||
每个(未排序)的部分
|
||||
将第一个元素设为 pivot
|
||||
存储索引 = pivot索引 +1
|
||||
从 i=pivot指数 +1 到 最右索引 的遍历
|
||||
如果 a[i] < a[pivot]
|
||||
交换 (i, 存储索引); 存储索引++;
|
||||
交换(pivot, 存储索引 - 1)
|
||||
存储索引 = pivot索引 +1
|
||||
从 i=pivot指数 +1 到 最右索引 的遍历
|
||||
如果 a[i] < a[pivot]
|
||||
交换 (i, 存储索引); 存储索引++;
|
||||
交换(pivot, 存储索引 - 1)
|
||||
*/
|
||||
sort(array, left, right) {
|
||||
if (typeof (left) === "undefined") left = 0
|
||||
@ -119,7 +120,7 @@ class QuickSort extends Sort {
|
||||
|
||||
|
||||
/**
|
||||
* 冒泡排序算法 Qucik Sort v0.1.0
|
||||
* 冒泡排序算法 Bubble Sort v0.1.0
|
||||
*
|
||||
* @author coder-xiaomo
|
||||
* @date 2022-05-16
|
||||
@ -142,7 +143,7 @@ class BubbleSort extends Sort {
|
||||
while swapped
|
||||
*/
|
||||
sort(array) {
|
||||
let sortTime = 0;
|
||||
let sortTime = 0
|
||||
let swapped
|
||||
do {
|
||||
swapped = false
|
||||
@ -154,20 +155,44 @@ class BubbleSort extends Sort {
|
||||
}
|
||||
sortTime++
|
||||
} while (swapped)
|
||||
// let swapCounter = array.length - 1;
|
||||
// while (swapCounter > 0) {
|
||||
// console.log(swapCounter)
|
||||
// for (let i = 1; i < array.length - swapCounter; i++) {
|
||||
// if (array[i - 1] > array[i]) {
|
||||
// this.swap(array, i - 1, i)
|
||||
// }
|
||||
// }
|
||||
// swapCounter--;
|
||||
// }
|
||||
|
||||
return array
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 选择排序算法 Selection Sort v0.1.0
|
||||
*
|
||||
* @author coder-xiaomo
|
||||
* @date 2022-05-16
|
||||
*/
|
||||
class SelectionSort extends Sort {
|
||||
|
||||
info() {
|
||||
return {
|
||||
name: "选择排序算法"
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
重复(元素个数-1)次
|
||||
把第一个没有排序过的元素设置为最小值
|
||||
遍历每个没有排序过的元素
|
||||
如果元素 < 现在的最小值
|
||||
将此元素设置成为新的最小值
|
||||
将最小值和第一个没有排序过的位置交换
|
||||
*/
|
||||
sort(array) {
|
||||
let minIndex
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
minIndex = i
|
||||
for (let j = i + 1; j < array.length; j++) {
|
||||
if (array[minIndex] > array[j]) {
|
||||
this.swap(array, minIndex, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
return array
|
||||
}
|
||||
}
|
||||
|
@ -16,28 +16,30 @@ function getRandomData(count = 20) {
|
||||
var data = [
|
||||
[],
|
||||
[-1],
|
||||
[1, 1, 1, 1, 1, 1, 1],
|
||||
[5, 8, 7, 4, 3, 1, 6, 2, 6, 5],
|
||||
[1, 2],
|
||||
[1, 2, 3, 4, 5, 6, 7, 8],
|
||||
[8, 7, 6, 5, 4, 3, 2, 1],
|
||||
getRandomData(6),
|
||||
[1, 1, 1, 1, 1, 1, 1],
|
||||
[5, 8, 7, 4, 3, 1, 6, 2, 6, 5],
|
||||
[47, 11, 50, 13, 16, 49, 8, 9, 38, 27, 20],
|
||||
// getRandomData(6),
|
||||
]
|
||||
var check = [
|
||||
[],
|
||||
[-1],
|
||||
[1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 2, 3, 4, 5, 5, 6, 6, 7, 8],
|
||||
[1, 2],
|
||||
[1, 2, 3, 4, 5, 6, 7, 8],
|
||||
[1, 2, 3, 4, 5, 6, 7, 8],
|
||||
null,
|
||||
null
|
||||
[1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 2, 3, 4, 5, 5, 6, 6, 7, 8],
|
||||
[8, 9, 11, 13, 16, 20, 27, 38, 47, 49, 50],
|
||||
// null
|
||||
]
|
||||
|
||||
var sortAlgorithm = {
|
||||
quicksort: new QuickSort(),
|
||||
bubblesort: new BubbleSort(),
|
||||
selectionSort: new SelectionSort(),
|
||||
}
|
||||
|
||||
// 遍历每一种算法
|
||||
|
Loading…
Reference in New Issue
Block a user