//选择
function selection(array) {
for (let i = 0; i < array.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < array.length; j++) {
minIndex = array[j] < array[minIndex] ? j : minIndex;
}
swap(array, i, minIndex);
}
return array;
}
function insertion(array) {
for (let i = 1; i < array.length; i++) {
for (let j = i - 1; j >= 0 && array[j] > array[j + 1]; j--) {
swap(array, j, j + 1)
}
}
return array;
}
//冒泡排序
function bubble(array) {
for (let i = array.length - 1; i > 0; i--) {
for (let j = 0; j < i; j++) {
if (array[j] > array[j + 1]) swap(array, j, j + 1)
}
}
}
function selection(array) {
for (let i = 0; i < array.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < array.length; j++) {
minIndex = array[j] < array[minIndex] ? j : minIndex;
}
swap(array, i, minIndex)
}
return array;
}
function insertion(array) {
for (let i = 1; i < array.length; i++) {
for (let j = i - 1; j >= 0 && array[j] > array[j + 1]; j--) {
swap(array, j, j + 1)
}
}
return array;
}
let create = function (o) {
var F = function () { };
F.prototype = o;
return new F();
}
// 快排
function quickSort(arr, i, j) {
if (i < j) {
let left = i;
let right = j;
let prvot = arr[letf];
while (i < j) {
while (arr[j] >= pivot && i < j) {
j--;
}
if (i < j) {
arr[i++] = arr[j];
}
while (arr[i] <= pivot && i < j) {
i++;
}
if (i < j) {
arr[j--] = arr[i]
}
}
arr[i] = pivot;
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
return arr;
}
}
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivotIndex = Math.floor(arr.length / 2);
let pivot = arr.slice(pivotIndex, 1)[0];
let left = [];
let right = [];
arr.forEach(item => {
if (item > pivot) {
right.push(item)
} else {
left.push(item)
}
})
return quickSort(left).concat([pivot], quickSort(right));
}