使用 js 实现十大排序算法: 快速排序 All In One
使用 js 实现十大排序算法: 快速排序
QuickSort
快速排序
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description 快速排序 quicksort
* @difficulty Medium
* @complexity O(n*log(n))
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/13857663.html
* @link https://leetcode.com/problems/kth-largest-element-in-an-array/
* @link https://leetcode.cn/problems/kth-largest-element-in-an-array/
* @solutions
*
*/
const log = console.log;
function quickSort(arr) {
// 终止条件 ✅
if (arr.length <= 1) {
return arr;
}
// 中间 index
let pivotIndex = Math.floor(arr.length / 2);
// 截取中间值,✅ splice 改变原数组长度, ❌ slice 不改变原数组长度
const pivot = arr.splice(pivotIndex, 1)[0];
const left = [];
const right = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
// 递归
return quickSort(left).concat([pivot], quickSort(right));
};
const arr = [12, 7, 5, 23, 18, 37, 1, 9, 17];
const test = quickSort(arr);
log(`arr =\n`, arr)
log(`test =\n`, test)
TypeScript
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2023-01-01
* @modified
*
* @description
* @description
* @difficulty Easy Medium Hard
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/13947110.html
* @solutions
*
* @best_solutions
*
*/
// export {};
const log = console.log;
function quickSort(arr: number[]): number[] {
// 终止条件 ✅
if (arr.length <= 1) {
return arr;
}
// 中间值
let index = Math.floor(arr.length / 2);
let mid = arr[index];
// 新数组
arr = [...arr.slice(0, index), ...arr.slice(index+1)];
const left: number[] = [];
const right: Array<number> = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < mid) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
// 递归
return [...quickSort(left), mid, ...quickSort(right)];
};
// 测试用例 test cases
const testCases = [
{
input: [12, 7, 5, 23, 18, 37, 1, 9, 17],
result: [1, 5, 7, 9, 12, 17, 18, 23, 37],
desc: 'value equal to [1, 5, 7, 9, 12, 17, 18, 23, 37]',
},
{
input: [2, 1],
result: [1, 2],
desc: 'value equal to [1, 2]',
},
{
input: [1],
result: [1],
desc: 'value equal to [1]',
},
{
input: [],
result: [],
desc: 'value equal to []',
},
];
for (const [i, testCase] of testCases.entries()) {
const result = quickSort(testCase.input);
log(`test case ${i} result: `, JSON.stringify(result) === JSON.stringify(testCase.result) ? `✅ passed` : `❌ failed`, result);
}
// $ npx ts-node ./quick-sort.ts
Array.slice
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
demos
leetcode: 215. 数组中的第K个最大元素
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-08-0
* @modified
*
* @description 215. Kth Largest Element in an Array
* @description 215. 数组中的第K个最大元素
* @difficulty Medium
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://leetcode.com/problems/kth-largest-element-in-an-array/
* @link https://leetcode.cn/problems/kth-largest-element-in-an-array/
* @solutions
*
* @best_solutions
*
*/
export {};
const log = console.log;
// function findKthLargest(nums: number[], k: number): number {
// return nums.sort((a,b) => b - a)[k - 1];
// };
function findKthLargest(nums: number[], k: number): number {
// 快排
function quickSort(arr: number[]) {
// 终止条件 ✅
if(arr.length <= 1) {
return arr;
}
let index = Math.floor(arr.length / 2);
let mid = arr.splice(index, 1)[0];
let left: number[] = [];
let right: number[] = [];
for(const i of arr) {
// 降序
if(i >= mid) {
left.push(i);
} else {
right.push(i);
}
}
return [...quickSort(left), mid, ...quickSort(right)];
}
const arr = quickSort(nums);
return arr[k - 1];
};
type TestCase = {
inputs: [number[], number];
[key: string]: any;
}
// 测试用例 test cases
const testCases: TestCase[] = [
{
inputs: [[3,2,1,5,6,4], 2],
result: 5,
desc: 'value equal to 5',
},
{
inputs: [[3,2,3,1,2,4,5,5,6], 4],
result: 4,
desc: 'value equal to 4',
},
];
for (const [i, testCase] of testCases.entries()) {
// const [first, second] = testCase.inputs as [number[], number];
const [first, second] = testCase.inputs;
const result = findKthLargest(first, second);
log(`test case ${i} result: `, JSON.stringify(result) === JSON.stringify(testCase.result) ? `✅ passed` : `❌ failed`, result);
}
// $ npx ts-node ./215\ kth-largest-element-in-an-array.ts
/*
test case 0 result: ✅ passed 5
test case 1 result: ✅ passed 4
*/
refs
js 十大排序算法 All In One
https://www.cnblogs.com/xgqfrms/p/13947122.html
https://www.cnblogs.com/xgqfrms/p/13857663.html
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13947110.html
未经授权禁止转载,违者必究!