Bubble Sort
Traversal in Binary Tree:
在遍歷一數列的時候,依據特定運算條件(ex.a > b, a < b),
來比對或進行交換,並完成運算後的排序。
Input: nums = [1, 2, 3, 2, 1, 6]
let bubbleSort = (nums) => {
for (let i = nums.length; i >= 0; i--) {
for (let j = nums.length; j >= 0; j--) {
if (nums[j] < nums[j - 1]) {
let box = nums[j]
nums[j] = nums[j - 1]
nums[j - 1] = box
}
}
}
return nums
};
Output: newNums = [1, 2, 2, 3, 1, 6]
Flow Chart:
[ 1, 2, 3, 2, 5, 6 ]
[ 1, 2, 3, 2, 5, 6 ]
[ 1, 2, 3, 2, 5, 6 ]
[ 1, 2, 2, 3, 5, 6 ]
.
.
.
[ 1, 2, 2, 3, 5, 6 ]
Blog:http://52.198.119.162/關於bubble-sort排序方法與示意圖/
LeetCode:http://52.198.119.162/leetcode-js-88-merge-sorted-array/