发表更新1 分钟读完 (大约117个字)0次访问
JS 直接插入排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| let arr = [49, 38, 65, 97, 76, 13, 27, 49]; console.log('arr:' + arr); insertionSort(arr); console.log('sortArr:' + arr); function insertionSort(arr) { let preIndex, current; for (let i = 1; i < arr.length; i++) { preIndex = i - 1; current = arr[i]; while(preIndex >= 0 && arr[preIndex] > current) { arr[preIndex+1] = arr[preIndex]; preIndex--; } arr[preIndex+1] = current; } }
|