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. Tensorflow 自适应学习速率

    Tensorflow 自适应学习速率 在模型的初期的时候,往往设置为较大的学习速率比较好,因为距离极值点比较远,较大的学习速率可以快速靠近极值点:而,后期,由于已经靠近极值点,模型快收敛了,此时,采用 ...

  2. [uiautomator篇] 如何获取apk的包名 博客模板

    Android自动化学习笔记:获取APK包名的几种方法 ------------------------------------------------------------------------ ...

  3. Game on Tree

    D - Game on Tree Time limit : 2sec / Memory limit : 256MB Score : 1100 points Problem Statement Ther ...

  4. 台州学院we are without brain 训练 计算几何

    A - View Angle Flatland has recently introduced a new type of an eye check for the driver's licence. ...

  5. C# TypeDescriptor获取类型转换器,实现泛型转换

    需求背景 平时的coding过程中,经常性遇到string类型转换成其他的基本类型,如 int double bool等,那我们正常的方式就是下面的方式进行类型转换 int.Parse("1 ...

  6. Response.End报错

    以下摘抄自博问:https://q.cnblogs.com/q/31506/       try catch中使用Response.End() 我在WebForm中用ajax发送请求到页面index. ...

  7. 【SCOI2003】【BZOJ1092】蜘蛛难题

    有一堆管道,还有一个蜘蛛Willy,如下图所示.所有管道的是上端开口,下端封底,直径都是1cm,连接两个管道的连接容量无限,但体积可以忽略不计. 在第一个管道上方有一个水源,从中有水不断往下流,速度为 ...

  8. phpMyAdmin操作之改管理员密码

    1.登录phpMyAdmin 默认地址:http://localhost/phpmyadmin 2.点击用户按钮 3.往下拉找到修改密码 点执行就修改了 注意: 如果再次登录时报错提示: #1045 ...

  9. 关于vue-router路径配置的问题

    "/" 表示路由根目录 "/AdminPage" 表示一级路由 如果在一级路由下面配置子路由 "User",表示的意思是 "/Ad ...

  10. C语言实验设计

    一.实验题目,设计思路,实现方法 7-4 计算分段函数[2](10 分) 本题目要求计算下列分段函数f(x)的值: 注:可在头文件中包含math.h,并调用sqrt函数求平方根,调用pow函数求幂. ...