[Algorithms] Quicksort algorithm using TypeScript
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的更多相关文章
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- [Algorithms] Insertion sort algorithm using TypeScript
Insertion sort is a very intuitive algorithm as humans use this pattern naturally when sorting cards ...
- Algorithms - Quicksort - 快速排序算法
相关概念 快速排序法 Quicksort 也是一个分治思想的算法. 对一个子数组 A[p: r] 进行快速排序的三步分治过程: 1, 分解. 将数组 A[p : r] 被划分为两个子数组(可能为空) ...
- [算法导论]quicksort algorithm @ Python
算法导论上面快速排序的实现. 代码: def partition(array, left, right): i = left-1 for j in range(left, right): if arr ...
- 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 ...
- quickSort算法导论版实现
本文主要实践一下算法导论上的快排算法,活动活动. 伪代码图来源于 http://www.cnblogs.com/dongkuo/p/4827281.html // imp the quicksort ...
- Foundation Sorting: Quicksort
/* Quick Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ /* [Quicksort ...
- [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 ...
- 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.UI组件过度封装. 微信小程序的组件是模仿react.js或vue.js的web组件设计的,并且封装了weui.css样式. P ...
- selenium2.53用45以下的火狐别太高
selenium2.53用45以下的火狐别太高在高的火狐需要selenium3
- 聊聊、Jstack 解决生产问题
最近项目很多,所在公司是一家金融企业.从 APP 端到 基金公司,整个体系涉及到很多系统.而我所负责的,正好是整个体系尾部,业务核心.前段时间,隔几天总会有用户购买理财产品失败,但是日志里面没有任何异 ...
- Django模板(filter过滤器{{ }}与tag标签{% %}应用)
模板里面过滤器与标签的应用 templates模板里面的应用参考(主要应用在这里面) <!DOCTYPE html> <html lang="en"> & ...
- LibreOJ β Round #4
A游戏 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: qmqmqm 提交提交记录统计讨论测试数据 题目描述 qmqmqm和subline ...
- rabbitmq exchange type
This is the fourth installment to the series: RabbitMQ for Windows. In thelast installment, we revi ...
- Java面试题之Array和ArrayList的区别
Array和ArrayList的区别: 1.Array类型的变量在声明的同时必须进行实例化(至少得初花数组的大小),而ArrayList可以只是先声明: 2.Array始终是连续存放的:而ArrayL ...
- POJ3625 Building Roads
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10803 Accepted: 3062 Description Fa ...
- 前端居中模板(常用HTML模板)
0.效果:
- 视频流传输协议RTP/RTCP/RTSP/HTTP的区别 (转)
用一句简单的话总结:RTSP发起/终结流媒体.RTP传输流媒体数据 .RTCP对RTP进行控制,同步.之所以以前对这几个有点分不清,是因为CTC标准里没有对RTCP进行要求,因此在标准RTSP的代码中 ...