AI
src/heap_sort.js
DeepSeek Sandbox / 浏览器编辑器
文件浏览
工作区:deepseek-sandbox
护栏保存
src/heap_sort.js
UTF-8
保存
export function heapSort(values) { const arr = [...values]; const n = arr.length; // Build max heap for (let i = Math.floor(n / 2) - 1; i >= 0; i--) { heapify(arr, n, i); } // Extract elements one by one for (let i = n - 1; i > 0; i--) { [arr[0], arr[i]] = [arr[i], arr[0]]; heapify(arr, i, 0); } return arr; } function heapify(arr, size, root) { let largest = root; const left = 2 * root + 1; const right = 2 * root + 2; if (left < size && arr[left] > arr[largest]) { largest = left; } if (right < size && arr[right] > arr[largest]) { largest = right; } if (largest !== root) { [arr[root], arr[largest]] = [arr[largest], arr[root]]; heapify(arr, size, largest); } }
src/heap_sort.js
受护栏保护的 write_file