Skip to content

Instantly share code, notes, and snippets.

@L9m
Created July 3, 2019 04:36
Show Gist options
  • Save L9m/e2098462241c7877d55dd80bb22b4a52 to your computer and use it in GitHub Desktop.
Save L9m/e2098462241c7877d55dd80bb22b4a52 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/vurokef
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
let quickSort = function (array) {
quick(array, 0, array.length - 1)
}
let quick = function (array, left, right) {
var index
if (array.length > 1) {
var pivot = array[Math.floor((right + left)/2)],
i = left,
j = right;
for (;i <= j;) {
while (array[i] < pivot) {
i++
}
while (array[j] > pivot) {
j--
}
//当左指针指向的元素比主元大且右指针指向的元素比主元少,并且左指针索引没有右指针索引大
// 意思当左项比右项大
if (i <= j) {
[array[i], array[j]] = [array[j], array[i]]
i++
j--
}
}
index = i
if (left < index - 1) {
quick(array, left, index -1)
}
if (index < right) {
quick(array, index, right)
}
}
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">let quickSort = function (array) {
quick(array, 0, array.length - 1)
}
let quick = function (array, left, right) {
var index
if (array.length > 1) {
var pivot = array[Math.floor((right + left)/2)],
i = left,
j = right;
for (;i <= j;) {
while (array[i] < pivot) {
i++
}
while (array[j] > pivot) {
j--
}
//当左指针指向的元素比主元大且右指针指向的元素比主元少,并且左指针索引没有右指针索引大
// 意思当左项比右项大
if (i <= j) {
[array[i], array[j]] = [array[j], array[i]]
i++
j--
}
}
index = i
if (left < index - 1) {
quick(array, left, index -1)
}
if (index < right) {
quick(array, index, right)
}
}
}</script></body>
</html>
let quickSort = function (array) {
quick(array, 0, array.length - 1)
}
let quick = function (array, left, right) {
var index
if (array.length > 1) {
var pivot = array[Math.floor((right + left)/2)],
i = left,
j = right;
for (;i <= j;) {
while (array[i] < pivot) {
i++
}
while (array[j] > pivot) {
j--
}
//当左指针指向的元素比主元大且右指针指向的元素比主元少,并且左指针索引没有右指针索引大
// 意思当左项比右项大
if (i <= j) {
[array[i], array[j]] = [array[j], array[i]]
i++
j--
}
}
index = i
if (left < index - 1) {
quick(array, left, index -1)
}
if (index < right) {
quick(array, index, right)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment