xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js Quick Sort Algorithm All In One

js Quick Sort Algorithm All In One

js 快速排序算法 All In One

快速排序 / Quick Sort

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-08-01
 * @modified
 *
 * @description 快速排序 quicksort
 * @difficulty Medium
 * @complexity O(n*log(n))
 * @augments
 * @example
 * @link https://github.com/xgqfrms/leetcode/issues/7#issuecomment-669991209
 * @solutions
 *
 */

const log = console.log;

function quickSort(arr) {
  // 递归终止条件
  if (arr.length <= 1) {
    return arr;
  }
  // 中间index
  var pivotIndex = Math.floor(arr.length / 2);
  // 中间值,参考值
  var pivot = arr.splice(pivotIndex, 1)[0];
  var left = [];
  var right = [];
  for (var 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)

/*

arr =
 [
  12, 7, 5, 23,
  37, 1, 9, 17
]
test =
 [
   1,  5,  7,  9, 12,
  17, 18, 23, 37
]

*/



/**
 *
 * @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
 * @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)

refs

https://www.ruanyifeng.com/blog/2011/04/quicksort_in_javascript.html

https://github.com/xgqfrms/leetcode/issues/7#issuecomment-669991209



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(119)  评论(5编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2019-10-22 Typescript & React & optional parameters & default parameters
2019-10-22 dva.js & redux & store & state & select
2019-10-22 antd col & number
2018-10-22 IE & table & border & border-collapse & bug
2018-10-22 add favorite & 收藏夹
点击右上角即可分享
微信分享提示