function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
let current = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > current) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = current;
}
return arr;
}
// 示例用法
const arr = [5, 3, 8, 4, 2];
console.log(insertionSort(arr)); // 输出 [2, 3, 4, 5, 8]