内容简介:// 选择排序// 选择排序算法是一种原地址比较排序算法。// 选择排序大致的思路是找数据结构中的最小值并将其放置在第一位。找到第二小的的值放置的第二类,以此类推
// 选择排序
// 选择排序大致的思路是找数据结构中的最小值并将其放置在第一位。找到第二小的的值放置的第二类,以此类推
// 每一次内循环遍历寻找最小数,记录下minIndex,并在这次内循环后交换minIndex和i的位置
function swap(arr, indexA, indexB) {
return [arr[indexA], arr[indexB]] = [arr[indexB], arr[indexA]];
}
function selectionSort(arr) {
let len = arr.length; for (let i = 0; i < len; i++) { let minIndex = i; for (let j = i + 1; j < len; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } if (i != minIndex ) { swap(arr, i, minIndex); } } return arr;
}
const arr = [91, 60, 96, 7, 35, 65, 10, 65, 9, 30, 20, 31, 77, 81, 24];
console.log(selectionSort(arr));
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 推荐算法集锦(补充)——近邻选择与算法拓展
- 算法渣-排序-选择
- 算法图解阅读笔记-选择排序
- 【一起学习排序算法】3 选择排序
- 为你的回归问题选择最佳机器学习算法
- SAS首席科学家:如何选择机器学习算法?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
An Introduction to the Analysis of Algorithms
Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99
This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!