Created
August 7, 2018 11:21
-
-
Save seven-lh/21b84ec0c1ba67b4861484515801f71a to your computer and use it in GitHub Desktop.
快速排序 + 每秒输出一个数 // source http://jsbin.com/subirov
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>快速排序 + 每秒输出一个数</title> | |
| </head> | |
| <body> | |
| <script id="jsbin-javascript"> | |
| "use strict"; | |
| function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
| function quickSort(arr) { | |
| var len = arr.length; | |
| if (len <= 1) { | |
| return arr; | |
| } | |
| var base = arr[0], | |
| left = [], | |
| right = [], | |
| repetBase = []; | |
| // for (let i = 0; i < len; i++) { | |
| // if (arr[i] > base) { | |
| // right.push(arr[i]); | |
| // } else if (arr[i] < base) { | |
| // left.push(arr[i]); | |
| // } else { | |
| // repetBase.push(arr[i]); | |
| // } | |
| // } | |
| //console.log(left, right); | |
| var _iteratorNormalCompletion = true; | |
| var _didIteratorError = false; | |
| var _iteratorError = undefined; | |
| try { | |
| for (var _iterator = arr[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
| var num = _step.value; | |
| if (num > base) { | |
| right.push(num); | |
| } else if (num < base) { | |
| left.push(num); | |
| } else { | |
| repetBase.push(num); | |
| } | |
| } | |
| } catch (err) { | |
| _didIteratorError = true; | |
| _iteratorError = err; | |
| } finally { | |
| try { | |
| if (!_iteratorNormalCompletion && _iterator["return"]) { | |
| _iterator["return"](); | |
| } | |
| } finally { | |
| if (_didIteratorError) { | |
| throw _iteratorError; | |
| } | |
| } | |
| } | |
| return [].concat(_toConsumableArray(quickSort(left)), repetBase, _toConsumableArray(quickSort(right))); | |
| } | |
| var result = quickSort([12, 8, 9, 87, 8, 9, 6, 8, 4, 98, 34, 90]); | |
| console.table(result); | |
| // 每一秒 输出一个数 | |
| // for (let i = 0; i < 3; i++) { | |
| // setTimeout(i => { | |
| // console.log(Math.random()); | |
| // }, i * 1000); | |
| // } | |
| for (var i = 0; i < 3; i++) { | |
| setTimeout(function (i) { | |
| console.log(Math.random()); | |
| }, i * 1000); | |
| } | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript">function quickSort(arr) { | |
| let len = arr.length; | |
| if (len <= 1) { | |
| return arr; | |
| } | |
| let base = arr[0], | |
| left = [], | |
| right = [], | |
| repetBase = []; | |
| // for (let i = 0; i < len; i++) { | |
| // if (arr[i] > base) { | |
| // right.push(arr[i]); | |
| // } else if (arr[i] < base) { | |
| // left.push(arr[i]); | |
| // } else { | |
| // repetBase.push(arr[i]); | |
| // } | |
| // } | |
| //console.log(left, right); | |
| for (let num of arr) { | |
| if (num > base) { | |
| right.push(num); | |
| } else if (num < base) { | |
| left.push(num); | |
| } else { | |
| repetBase.push(num); | |
| } | |
| } | |
| return [...quickSort(left), ...repetBase, ...quickSort(right)]; | |
| } | |
| const result = quickSort([12, 8, 9, 87, 8, 9, 6, 8, 4, 98, 34, 90]); | |
| console.table(result); | |
| // 每一秒 输出一个数 | |
| // for (let i = 0; i < 3; i++) { | |
| // setTimeout(i => { | |
| // console.log(Math.random()); | |
| // }, i * 1000); | |
| // } | |
| for (var i = 0; i < 3; i++) { | |
| setTimeout(function(i) { | |
| console.log(Math.random()); | |
| }, i * 1000); | |
| } | |
| </script></body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
| function quickSort(arr) { | |
| var len = arr.length; | |
| if (len <= 1) { | |
| return arr; | |
| } | |
| var base = arr[0], | |
| left = [], | |
| right = [], | |
| repetBase = []; | |
| // for (let i = 0; i < len; i++) { | |
| // if (arr[i] > base) { | |
| // right.push(arr[i]); | |
| // } else if (arr[i] < base) { | |
| // left.push(arr[i]); | |
| // } else { | |
| // repetBase.push(arr[i]); | |
| // } | |
| // } | |
| //console.log(left, right); | |
| var _iteratorNormalCompletion = true; | |
| var _didIteratorError = false; | |
| var _iteratorError = undefined; | |
| try { | |
| for (var _iterator = arr[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
| var num = _step.value; | |
| if (num > base) { | |
| right.push(num); | |
| } else if (num < base) { | |
| left.push(num); | |
| } else { | |
| repetBase.push(num); | |
| } | |
| } | |
| } catch (err) { | |
| _didIteratorError = true; | |
| _iteratorError = err; | |
| } finally { | |
| try { | |
| if (!_iteratorNormalCompletion && _iterator["return"]) { | |
| _iterator["return"](); | |
| } | |
| } finally { | |
| if (_didIteratorError) { | |
| throw _iteratorError; | |
| } | |
| } | |
| } | |
| return [].concat(_toConsumableArray(quickSort(left)), repetBase, _toConsumableArray(quickSort(right))); | |
| } | |
| var result = quickSort([12, 8, 9, 87, 8, 9, 6, 8, 4, 98, 34, 90]); | |
| console.table(result); | |
| // 每一秒 输出一个数 | |
| // for (let i = 0; i < 3; i++) { | |
| // setTimeout(i => { | |
| // console.log(Math.random()); | |
| // }, i * 1000); | |
| // } | |
| for (var i = 0; i < 3; i++) { | |
| setTimeout(function (i) { | |
| console.log(Math.random()); | |
| }, i * 1000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment