[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 ...
随机推荐
- hdu3613 Best Reward
先manacher.然后前缀和价值,枚举切点,O(1)判断切后是否回文 #include <iostream> #include <cstring> #include < ...
- 如何用treap写luogu P3391
treap大法好!!! splay什么的都是异端 --XZZ 先%FHQ为敬 (fhq)treap也是可以搞区间翻转的 每次把它成(1~L-1)(L~R)(R+1~n)三块然后打标记拼回去 对于有标记 ...
- python练手系列-分布式监控
如果我们要写一个监控系统,要注意哪些问题和需求? [1] agent收集数据的时候需要通过系统调用少的方法收集到我们需要数据,一般来说我们优先使用python自带的系统方法,然后是读取/proc 文件 ...
- ibatis 动态SQL
直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的字符串连接.对于这个问题,I ...
- hdu1595 最短路问题(dijkstra&&spfa)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- Codeforces Round #345 (Div. 2)——A. Joysticks(模拟+特判)
A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- POJ3349 Snowflake Snow Snowflakes 【哈希表】
题目 很简单,给一堆6元组,可以从任意位置开始往任意方向读,问有没有两个相同的6元组 题解 hash表入门题 先把一个六元组的积 + 和取模作为hash值,然后查表即可 期望\(O(n)\) #inc ...
- NOIP2017整数 【线段树】
题目 题目背景 在人类智慧的山巅,有着一台字长为10485761048576 位(此数字与解题无关)的超级计算机,著名理论计算机科 学家P博士正用它进行各种研究.不幸的是,这天台风切断了电力系统,超级 ...
- 大杂烩 Classpath / Build path / Debug关联源码 / JDK&JRE区别
Classpath的理解及其使用方式 原文地址:http://blog.csdn.net/wk1134314305/article/details/77940147?from=bdhd_site 摘要 ...
- iOS-Core Data基础
Core Data基础 Core Data是一个API集合,被设计用来简化数据对象的持久存储. 在此先不普及概念,先通过一个简单的案例使用来感受一下Core Data的精妙之处. 在创建工程的时候勾选 ...