j s选择算法

栏目: 编程工具 · 发布时间: 6年前

内容简介:// 选择排序// 选择排序算法是一种原地址比较排序算法。// 选择排序大致的思路是找数据结构中的最小值并将其放置在第一位。找到第二小的的值放置的第二类,以此类推

// 选择排序

// 选择 排序算法 是一种原地址比较 排序 算法。

// 选择排序大致的思路是找数据结构中的最小值并将其放置在第一位。找到第二小的的值放置的第二类,以此类推

// 每一次内循环遍历寻找最小数,记录下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));


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to the Analysis of Algorithms

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》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具