資料結構 02
Algorithms 和 Data Structure 的關係 Algorithm → Pseudocode → Program Data Structure 的角色?透過程式語言實作 Pseudocode,為演算法提供資料的組織方式。 Recursive Algorithms 在 資料結構 01 中,我們介紹了 Binary Search 的迴圈寫法: // Time Complexity: O(log n) // Space Complexity: O(1) function binarySearch(arr, target) { let left = 0; let right = arr.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); if (arr[mid] === target) { return mid; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } Recursive Binary Search 同樣的邏輯,改用遞迴寫法:...