IT袋

当前位置:主页 > 经验教程 > 建站编程 >

js将一个数组分割成多个数组的数据

js将一个数组分割成多个数组的数据 关于js在数组中查找指定元素(2)

更新:2023-08-20 06:48:09 来源:IT袋 作者:苏晓敏
导读:js将一个数组分割成多个数组的数据,function findMaxValue(array) { let max = array[0]; for (let index = 0; index array.length; index++) { if (array[index] max) { max = array[index] } } return max; } function countSort(array) { if (array

js将一个数组分割成多个数组的数据

js将一个数组分割成多个数组的数据

function findMaxValue(array) { let max = array[0]; for (let index = 0; index < array.length; index++) { if (array[index] > max) { max = array[index] } } return max; } function countSort(array) { if (array.length < 2) { return array } const maxValue = findMaxValue(array); const counts = new Array(maxValue + 1); array.forEach(item => { if (!counts[item]) { counts[item] = 0; } counts[item]++; }); let sortIndex = 0; counts.forEach((item, i) => { while (item > 0) { array[sortIndex++] = i; item--; } }) return array }

桶排序

也是将元素分解为多个较小的数组,然后再使用一个简单的排序算法对每一个桶进行排序,最后合并所有的桶

js将一个数组分割成多个数组的数据

/** * 桶排序 */import { insertSort} from "./insertSort.js";function bucketSort(array, size = 5) { if (array.length < 2) { return array; } const buckets = createBuckets(array, size); return sortBuckets(buckets);}// 创建桶const createBuckets = (arr, size) => { let minValue = arr[0]; let maxValue = arr[0]; arr.forEach((item, key) => { if (arr[key] < minValue) { minValue = arr[key]; } else if (arr[key] > maxValue) { maxValue = arr[key] } }); // 桶的基数 const bucketCount = Math.floor((maxValue - minValue) / size) + 1; const buckets = []; for (let index = 0; index < bucketCount; index++) { buckets[index] = []; } arr.forEach((item, index) => { const bucketIndex = Math.floor((arr[index] - minValue) / size); buckets[bucketIndex].push(arr[index]); }) return buckets}// 对桶进行排序const sortBuckets = (buckets) => { const sortArray = []; buckets.forEach((item, index) => { if (buckets[index] != null) { insertSort(buckets[index]); sortArray.push(...buckets[index]) } }) return sortArray}console.log(bucketSort([43,65,2,1,678,3,54,0]))

基数排序

基数排序是依据数字的有效位,将整数分布到桶中去。

js将一个数组分割成多个数组的数据

/** * 基数排序 */function radixSort(arr, radixBase = 10) { if (arr.length < 2) { return arr; } const minValue = findMinValue(arr); const maxValue = findMaxValue(arr); let significantDigit = 1; while ((maxValue - minValue) / significantDigit >= 1) { arr = countSortForRadix(arr, radixBase, significantDigit, minValue); significantDigit *= radixBase; } return arr;}// 有效位【基数】排序function countSortForRadix(arr, radixBase, significantDigit, minValue) { let bucketsIndex; const buckets = []; const aux = []; for (let index = 0; index < radixBase; index++) { buckets[index] = 0; } for (let index = 0; index < arr.length; index++) { bucketsIndex = Math.floor(((arr[index] - minValue) / significantDigit) % radixBase); buckets[bucketsIndex]++; } for (let i = 1; i < radixBase; i++) { buckets[i] += buckets[i - 1] } for (let index = arr.length - 1; index >= 0; index--) { bucketsIndex = Math.floor(((arr[index] - minValue) / significantDigit) % radixBase); aux[--buckets[bucketsIndex]] = arr[index]; } for (let index = 0; index < arr.length; index++) { arr[index] = aux[index]; } return arr}function findMaxValue(array) { let max = array[0]; for (let index = 0; index < array.length; index++) { if (array[index] > max) { max = array[index] } } return max;}function findMinValue(array) { let min = array[0]; for (let index = 0; index < array.length; index++) { if (array[index] < min) { min = array[index] } } return min;}console.log(radixSort([324,32,0,21,6,343,256,23,10]))

现在使用的基数是10,在排序中将使用10个桶来分布元素,并且首先基于个位数进行排序,然后是十位数,然后是百位数,这样往后推。

以上是IT袋网关于js将一个数组分割成多个数组的数据 和 关于js在数组中查找指定元素的IT小经验,希望能为您在生活中带来帮助!

相关阅读

  • 创建免费网站的方法 免费建自己的网址步骤

    创建免费网站的方法 免费建自己的网址步骤

    今天小编介绍创建免费网站的方法和免费建自己的网址步骤的电脑方面的小经验,下面为详细的介绍。 学习网页编程,制作了几个网页,必须要服务器才能展示吗? 只要gitee或者github就可以了

  • 服务器地址在哪里看 获取外网ip地址的方法

    服务器地址在哪里看 获取外网ip地址的方法

    跟大家说一说服务器地址在哪里看和获取外网ip地址的方法方面的内容,一起跟随小编看看吧! 说到DNS服务器地址,那么就要先科普一下什么是DNS? DNS:是域名系统 (Domain Name System)的缩写,是

  • 一键搭建网站工具怎么用 免费一键搭建网站的方法

    一键搭建网站工具怎么用 免费一键搭建网站的方法

    您可能不了解一键搭建网站工具怎么用和免费一键搭建网站的方法的话题,一起跟随小编看看吧! 想自己创业一个电商网站但不知道要怎么开?下面IT袋网小编就教你如何使用在线搭建工具乔

  • 如何自己搭建服务器主机 个人搭建云服务器的步骤

    如何自己搭建服务器主机 个人搭建云服务器的步骤

    本文核心内容:如何自己搭建服务器主机和个人搭建云服务器的步骤的电脑方面的小经验,具体详情如下: 1、首先windows+r键打开运行窗口输入OptionalFeatures打开windows功能开启与关闭。 2、找到