iT邦幫忙

2022 iThome 鐵人賽

DAY 1
0
自我挑戰組

JavaScript - 30天 - 自學挑戰系列 第 1

關於Bubble Sort排序方法與示意圖

  • 分享至 

  • xImage
  •  

Bubble Sort
Traversal in Binary Tree:

在遍歷一數列的時候,依據特定運算條件(ex.a > b, a < b),
來比對或進行交換,並完成運算後的排序。

https://ithelp.ithome.com.tw/upload/images/20220901/20152092hbvcctijrc.png

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/


下一篇
關於Selection Sort排序方法與示意圖
系列文
JavaScript - 30天 - 自學挑戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言