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. 02_ThreadLocal语法与源码分析

    文章导读: 早在JDK 1.2的版本中就提供Java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程 ...

  2. Html开发小结

    html部分 1.html标签 标签不区分大小写. 如:<!doctype html>与<!DOCTYPE html > <div></div>与< ...

  3. python - 路径处理 和 模块导入

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_模块导入.py@ide: PyCharm Community E ...

  4. [Uiautomator篇][2] UiDeviceAPI介绍

    1 https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html  http://www.cnb ...

  5. [错误解决]UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

    python2内容无法写入csv,报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordin ...

  6. iOS开发 UILabel实现自适应高宽

    UILabel是iOS开发常用的控件.UILabel的属性需要了解,UILabel的特殊显示效果也需要我们掌握.UILabel自适应高宽度是很多初学者遇到的技术性难题.比如段文字,要让他完全地分行显示 ...

  7. Linux硬件资源管理与外设设备使用、系统运行机制及用户管理

    Linux硬件资源管理 PCI设备         显卡            $>>dmesg |grep -i vga[    0.000000] Console: colour VG ...

  8. jquery工具方法总结

    $.extend 对象合并,支持深拷贝 $.each 相当于array.each或object.each,可以遍历数组和对象 $.grep 相当于array.filter $.map 相当于array ...

  9. CentOS 7.4升级Linux内核

    CentOS 7.4升级Linux内核 [日期:2018-01-15] 来源:Linux社区  作者:straycats [字体:大 中 小] 由于最近intel出了Meltdown和Spectre两 ...

  10. Distinct Substrings(spoj 694)

    题意:要求不同子串的个数 /* 先求出height数组,不难看出height之和就是重复的字符串个数,用总的减去它就行了. */ #include<cstdio> #include< ...