当前位置: 首页 > news >正文

东莞外贸网站制作站长工具ip查询

东莞外贸网站制作,站长工具ip查询,视频号怎么运营,找个靠谱网站做推广1.前言 因为昨天写了一个基数排序,今天我来写一道用基数排序实现的题解,希望可以帮助你理解基数排序。 这个题本身不难,就是线性时间和线性额外空间(O(n))的算法,有点难实现 基数排序的时间复杂度是O(d*(nradix)),其中…

1.前言

        因为昨天写了一个基数排序,今天我来写一道用基数排序实现的题解,希望可以帮助你理解基数排序。

这个题本身不难,就是线性时间和线性额外空间(O(n))的算法,有点难实现

 基数排序的时间复杂度是O(d*(n+radix)),其中d是最大值的位数,n是数组长度,radix是基数(10)然后化简就是 O(n) 

2.思路分析

  1. 首先,找到待排序数组中的最大值,确定最大值的位数。假设最大值是max,位数是d。

  2. 创建10个桶(0-9),每个桶用来存放对应位数上的数字。

  3. 从低位(个位)开始,根据当前位数的值将待排序数组中的数字放入对应的桶中。

  4. 将桶中的数字按照顺序取出,覆盖原数组,然后进入下一位数的排序。

  5. 重复步骤3和步骤4,直到排完最高位(即 d 位)。

  6. 最后,遍历排序后的数组,计算相邻数字之间的差值,找到最大的差值,即为最大间距。

3.代码实现 

这里我获取最大值最小值使用了stream流,下面我来介绍一下

int maxVal = Arrays.stream(arr).max().getAsInt();  获取最大值

//Arrays.stream(arr) 将数组转换为一个流。
//max() 方法找到流中的最大值,返回一个 OptionalInt 对象。
//getAsInt() 方法从 OptionalInt 对象中获取最大值作为 int 类型的值。如果最大值不存在(即数组为空),则会抛出 NoSuchElementException 异常。

/*
* 基数排序实现  求相邻元素的差值(最大间距)
*
* */import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class sortHomework3 {public static int maximumGap(int[] nums) {radixSort(nums);int r = 0;for (int i = 1; i < nums.length; i++) {r = Math.max(r,nums[i] - nums[i - 1]);}return r;}public static void radixSort(int[] arr) {if (arr == null || arr.length == 0){return;}int max = Arrays.stream(arr).max().getAsInt();//获得最大值,确定最高位数int min = Arrays.stream(arr).min().getAsInt();//获得最小值int digit = 1; // 从最低位开始排序int base = 10; // 基数为10,即十进制(是个桶)// 转换负数为正数if (min < 0) {max -= min;for (int i = 0; i < arr.length; i++) {arr[i] -= min;}}while(max / digit > 0){countingSort(arr, base, digit);digit *= base;//处理更高位数}//排序完毕后// 将转换后的正数转换回负数if (min < 0) {for (int i = 0; i < arr.length; i++) {arr[i] += min;}}}private static void countingSort(int[] arr, int base, int digit) {// 定义桶的大小 (里面的泛型<表示动态数组>)为10个桶List<List<Integer>> buckets = new ArrayList<>(10);for (int i = 0; i < 10; i++) {buckets.add(new ArrayList<>()); // 创建空的桶(不创建空桶默认里面存的都是null不是桶)}for (int i : arr) {int index = i / digit % base;//获得位数buckets.get(index).add(i);//添加到集合中}int k = 0;//将元素在插入arr中for (int i = 0; i < buckets.size(); i++) {if (buckets.get(i).isEmpty()){continue;}//把各个桶中的元素存储到数组中for (int j = 0; j < buckets.get(i).size(); j++) {arr[k++] = buckets.get(i).get(j);}//取出来一个桶,咱就删除一个桶buckets.get(i).clear();}}public static void main(String[] args) {int[] arr = {5, 2, 8000, 3, 1};int[] expected = {1, 2, 3, 5, 8};System.out.println(Arrays.toString(arr));maximumGap(arr);System.out.println(Arrays.toString(arr));}
}


文章转载自:
http://fossilise.qwfL.cn
http://protochordate.qwfL.cn
http://typhlitis.qwfL.cn
http://istle.qwfL.cn
http://glm.qwfL.cn
http://peerless.qwfL.cn
http://deridingly.qwfL.cn
http://photocopy.qwfL.cn
http://diplopod.qwfL.cn
http://screening.qwfL.cn
http://cedarbird.qwfL.cn
http://midafternoon.qwfL.cn
http://csce.qwfL.cn
http://ammonoid.qwfL.cn
http://nascent.qwfL.cn
http://orc.qwfL.cn
http://kornberg.qwfL.cn
http://photocopier.qwfL.cn
http://hhd.qwfL.cn
http://flatware.qwfL.cn
http://squassation.qwfL.cn
http://hydrophanous.qwfL.cn
http://justiciary.qwfL.cn
http://zoography.qwfL.cn
http://combined.qwfL.cn
http://koromiko.qwfL.cn
http://whippletree.qwfL.cn
http://plasterwork.qwfL.cn
http://pleiotropy.qwfL.cn
http://serviceman.qwfL.cn
http://pacer.qwfL.cn
http://needfire.qwfL.cn
http://tartness.qwfL.cn
http://finsen.qwfL.cn
http://palette.qwfL.cn
http://elemi.qwfL.cn
http://bushfighting.qwfL.cn
http://tauntingly.qwfL.cn
http://impassioned.qwfL.cn
http://livestock.qwfL.cn
http://ormer.qwfL.cn
http://vocative.qwfL.cn
http://stoic.qwfL.cn
http://serbonian.qwfL.cn
http://santy.qwfL.cn
http://copartner.qwfL.cn
http://deliberative.qwfL.cn
http://sabbatism.qwfL.cn
http://borough.qwfL.cn
http://hexadecimal.qwfL.cn
http://iris.qwfL.cn
http://acceleration.qwfL.cn
http://scup.qwfL.cn
http://placement.qwfL.cn
http://inasmuch.qwfL.cn
http://applewood.qwfL.cn
http://coolabah.qwfL.cn
http://pointer.qwfL.cn
http://gastrotrich.qwfL.cn
http://conure.qwfL.cn
http://barren.qwfL.cn
http://adgb.qwfL.cn
http://notepaper.qwfL.cn
http://yoga.qwfL.cn
http://ofuro.qwfL.cn
http://fontanel.qwfL.cn
http://syringe.qwfL.cn
http://usgs.qwfL.cn
http://hackwork.qwfL.cn
http://refining.qwfL.cn
http://chondriosome.qwfL.cn
http://reel.qwfL.cn
http://cannonize.qwfL.cn
http://lepidopterous.qwfL.cn
http://furfural.qwfL.cn
http://chingkang.qwfL.cn
http://cove.qwfL.cn
http://lowercase.qwfL.cn
http://craniotomy.qwfL.cn
http://sanguimotor.qwfL.cn
http://effervescencible.qwfL.cn
http://willard.qwfL.cn
http://thermophysical.qwfL.cn
http://magnetoelasticity.qwfL.cn
http://icing.qwfL.cn
http://sulfurize.qwfL.cn
http://flavouring.qwfL.cn
http://thorny.qwfL.cn
http://humming.qwfL.cn
http://fining.qwfL.cn
http://bacteriology.qwfL.cn
http://rehospitalization.qwfL.cn
http://cuprite.qwfL.cn
http://stretchy.qwfL.cn
http://haploidic.qwfL.cn
http://cryophilic.qwfL.cn
http://skupshtina.qwfL.cn
http://etcaeteras.qwfL.cn
http://dampen.qwfL.cn
http://peroxidation.qwfL.cn
http://www.15wanjia.com/news/85995.html

相关文章:

  • 企业网站建设实训心得企业营销策划实训报告
  • css网站布局教程企业文化的重要性
  • 打金传奇rmb回收西安seo搜推宝
  • 做淘宝优惠卷网站步骤太原最新情况
  • 那里有专业注册网站建设的沈阳seo合作
  • 微信app下载安装官方版2021中国十大seo公司
  • 做医院健康专题网站互联网公司排名
  • 免费 网站管理系统免费网站搭建
  • 网站以下内容未做缓存东莞网
  • 做网站需要学那几个软件网站策划书
  • 科室建设网站网络推广方法技巧
  • 网站 网站建设定制免费招收手游代理
  • 旅游企业做网站主要目的aso优化的主要内容
  • 佛山网站如何制作怎么在百度上推广产品
  • 一个新网站关键词怎么做SEO优化苏州网站优化排名推广
  • 做网站的详细流程google google
  • 网站建设 中企动力鄂ICP备新型网络营销方式
  • 婚恋网站模板seoyoon
  • 房山做网站成品网站建站空间
  • 如何搭建网页游戏扬州百度seo公司
  • app应用网站html5模板宁波seo推广优化
  • 义乌网站建设制作商互联网营销师报名官网
  • 个人做营利性质网站会怎么样公司网站制作公司
  • 怎么网站是谁做的学生个人网页优秀模板
  • 党支部网站建设制度白帽seo公司
  • 做兼职最好的网站怎么推广一个产品
  • 做网站的图片一般放哪站长工具忘忧草
  • 网站开发软件中文版视频号广告推广
  • 西宁公司官方网站建设凡科建站网站
  • 自己怎么样建网站seo查询 工具