码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2019-05-24 00:34:19      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:let   UNC   const   style   ons   push   func   res   nbsp   

function quickSort (arr) {
  if (arr.length === 0) {
    return [];
  }
  let left = [];
  let right = [];
  let pivot = arr[0];
  for (let index = 1; index < arr.length; index++) {
    const element = arr[index];
    if (element < pivot) {
      left.push(element);
    } else {
      right.push(element);
    }
  }
  return [...quickSort(left), pivot, ...quickSort(right)];
}

const arr = [2, 3, 43, 5, 1, 7, 6, 3, 22];
console.log(‘排序之前‘, arr);
const res = quickSort(arr);
console.log(‘排序之后‘, res);
排序之前 [
  2,  3, 43,
  5,  1,  7,
  6,  3, 22
]
排序之后 [
  1,  2,  3,
  3,  5,  6,
  7, 22, 43
]

 

快速排序

标签:let   UNC   const   style   ons   push   func   res   nbsp   

原文地址:https://www.cnblogs.com/xiaosongJiang/p/10915385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!