Quicksort (also called partition sort and pivot sort) is arguably the most used sorting algorithm. It is the one commonly implemented internally in language runtimes. In this lesson we cover the quick sort algorithm, why is it called quick and how to implement it using TypeScript / JavaScript.

export function quickSort(array) {
array = [...array];
partition(array, 0, array.length);
return array;
} function partition(array, start, end) {
const length = end - start;
if (length <= 1) return; // select the pivot
const pivotIndex = start + Math.floor(Math.random() * length);
// move the pivot to the beginning of the array
[array[start], array[pivotIndex]] = [array[pivotIndex], array[start]];
// get the pivot value
const pivot = array[start];
// get the pivot index
let pivotRank = start;
// loop thought the array, swap every number each is smaller
// than the pivor
for (let index = start + 1; index < end; index++) {
if (array[index] < pivot) {
// increase the rank poisition first
pivotRank++;
// swap the current number and rand poisition
[array[index], array[pivotRank]] = [array[pivotRank], array[index]];
}
}
// move the pivot to the pivotRank position
if (pivotRank !== start) {
[array[start], array[pivotRank]] = [array[pivotRank], array[start]];
} partition(array, start, pivotRank);
partition(array, pivotRank + 1, end);
} const test = [5, 1, 8, 7, 4, 3, 6, 9];
const res = quickSort(test); document.write(res);

Simpfily way:

function quickSort (array) {

    if (array.length <= 1) {
return array;
} let pivotIndex = 0;
let pivot = array[pivotIndex]; let less = []
let greater = [] for (let i in array) {
if (i != pivotIndex) {
array[i] > pivot ? greater.push(array[i]): less.push(array[i]);
}
} return [
...quickSort(less),
pivot,
...quickSort(greater)
]
} console.log(quickSort([6, 5, 4, 3, 2, 1, 7,9, 8]))

[Algorithms] Quicksort algorithm using TypeScript的更多相关文章

  1. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

  2. [Algorithms] Insertion sort algorithm using TypeScript

    Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...

  3. Algorithms - Quicksort - 快速排序算法

    相关概念 快速排序法 Quicksort 也是一个分治思想的算法. 对一个子数组 A[p: r] 进行快速排序的三步分治过程: 1, 分解. 将数组 A[p : r] 被划分为两个子数组(可能为空) ...

  4. [算法导论]quicksort algorithm @ Python

    算法导论上面快速排序的实现. 代码: def partition(array, left, right): i = left-1 for j in range(left, right): if arr ...

  5. Top 10 Algorithms of 20th and 21st Century

    Top 10 Algorithms of 20th and 21st Century MATH 595 (Section TTA) Fall 2014 TR 2:00 pm - 3:20 pm, Ro ...

  6. quickSort算法导论版实现

    本文主要实践一下算法导论上的快排算法,活动活动. 伪代码图来源于 http://www.cnblogs.com/dongkuo/p/4827281.html // imp the quicksort ...

  7. Foundation Sorting: Quicksort

    /* Quick Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ /* [Quicksort ...

  8. [Paper] Selection and replacement algorithm for memory performance improvement in Spark

    Summary Spark does not have a good mechanism to select reasonable RDDs to cache their partitions in ...

  9. Effective STL 43: Prefer algorithm calls to hand-written loops

    Effective STL 43: Prefer algorithm calls to hand-written loops */--> div.org-src-container { font ...

随机推荐

  1. mysql 索引和查询优化

    对于任何DBMS,索引都是进行优化的最主要的因素.对于少量的数据,没有合适的索引影响不是很大,但是,当随着数据量的增加,性能会急剧下降.如果对多列进行索引(组合索引),列的顺序非常重要,MySQL仅能 ...

  2. 编译安装solr

    1, 获取安装包 wget http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-linux-x64.rpm wget htt ...

  3. javascript学习笔记 - 引用类型 RegExp

    四 RegExp 格式: var expression = / pattern / flags; 1.flags 为标志.分别为g.i.m. g:表示全局模式.即模式将匹配所有的字符串,而不是在发现第 ...

  4. queue 类

    一:普通队列 1.队列特征:先进先出,它只允许在一端(队尾)进行插入元素操作,在另一端(队头)进行删除元素操作 2. 存取类函数 front():用来取出queue中的队头元素,对应于front()函 ...

  5. AC自动机详解 (P3808 模板)

    AC自动机笔记 0.0 前言 哇,好久之前就看了 KMP 和 Trie 树,但是似乎一直没看懂 AC自动机?? 今天灵光一闪,加上之前看到一些博客和视频,瞬间秒懂啊... 其实这个玩意还是蛮好理解的. ...

  6. jsp生成war

    安装java 环境,进入jsp所在目录,使用如下命令可将当前目录中所有文件打成到xss.war包中,正常的war包中还包含另外两个文件meta-inf,web-inf,在生成的时候,需要把这两个文件加 ...

  7. bzoj 3143 [Hnoi2013]游走 期望dp+高斯消元

    [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3394  Solved: 1493[Submit][Status][Disc ...

  8. How to resolve mysql problem when you get code 2003(10061) and 1130

    When I use Navicate to connect to mysql on Ubuntu, I got message 2003(10061) firstly. To resovle thi ...

  9. bzoj 1069 凸包+旋转卡壳

    题目大意 在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成 的多边形面积最大. 分析 枚举对角线的一个端点 另一个端点开始转 转的时候求出对角线左边面积 ...

  10. 【BZOJ4555】【TJOI2016】【HEOI2016】求和 (第二类斯特林数+NTT卷积)

    Description 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: $$f(n)=\sum_{i=0}^n\sum_{j=0}^i S(i,j)\tim ...