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

如何用java web做网站成都进入搜索热度前五

如何用java web做网站,成都进入搜索热度前五,网站建设合同2018,深圳宣传片题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 程序分析 要将一个数插入已经排好序的数组中,我们可以采用以下步骤: 遍历数组,找到第一个大于待插入数的位置。将待插入数插入到该位…

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

程序分析

要将一个数插入已经排好序的数组中,我们可以采用以下步骤:

  1. 遍历数组,找到第一个大于待插入数的位置。
  2. 将待插入数插入到该位置,同时将该位置后面的元素依次后移一位。

下面我们将使用三种不同的方法来实现这个任务,并分析它们的优缺点。

方法一:遍历插入

解题思路

我们可以遍历已排序数组,找到第一个大于待插入数的位置,然后将待插入数插入该位置。

实现代码

public class Main {public static void insertSorted(int[] arr, int num) {int i;for (i = 0; i < arr.length; i++) {if (arr[i] > num) {break;}}// 将待插入数插入到位置 i,同时后面的元素后移for (int j = arr.length - 1; j > i; j--) {arr[j] = arr[j - 1];}arr[i] = num;}public static void main(String[] args) {int[] arr = {1, 3, 5, 7, 9};int num = 4;System.out.print("Original Array: ");for (int i = 0; i < arr.length; i++) {System.out.print(arr[i] + " ");}insertSorted(arr, num);System.out.print("\nArray after insertion: ");for (int i = 0; i < arr.length; i++) {System.out.print(arr[i] + " ");}}
}

优缺点

优点:

  • 简单易懂,容易实现。
  • 对于小规模数组或已经基本有序的数组,性能较好。

缺点:

  • 对于大规模数组,性能较差,时间复杂度为O(n)。

方法二:二分查找 + 插入

解题思路

我们可以使用二分查找来快速找到待插入数的位置,然后再进行插入操作。

实现代码

public class Main {public static int binarySearch(int[] arr, int num) {int left = 0;int right = arr.length - 1;while (left <= right) {int mid = left + (right - left) / 2;if (arr[mid] == num) {return mid;} else if (arr[mid] < num) {left = mid + 1;} else {right = mid - 1;}}return left;}public static void insertSorted(int[] arr, int num) {int pos = binarySearch(arr, num);for (int i = arr.length - 1; i >= pos; i--) {arr[i] = arr[i - 1];}arr[pos] = num;}public static void main(String[] args) {int[] arr = {1, 3, 5, 7, 9};int num = 4;System.out.print("Original Array: ");for (int i = 0; i < arr.length; i++) {System.out.print(arr[i] + " ");}insertSorted(arr, num);System.out.print("\nArray after insertion: ");for (int i = 0; i < arr.length; i++) {System.out.print(arr[i] + " ");}}
}

优缺点

优点:

  • 较方法一更高效,时间复杂度为O(log n)。
  • 适用于大规模数组。

缺点:

  • 实现稍微复杂一些。

方法三:使用ArrayList

解题思路

我们可以使用ArrayList来插入新元素,然后再将其转换为数组。

实现代码

import java.util.ArrayList;
import java.util.Arrays;public class Main {public static void insertSorted(ArrayList<Integer> list, int num) {int pos = 0;while (pos < list.size() && list.get(pos) < num) {pos++;}list.add(pos, num);}public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9));int num = 4;System.out.print("Original List: ");for (int i = 0; i < list.size(); i++) {System.out.print(list.get(i) + " ");}insertSorted(list, num);System.out.print("\nList after insertion: ");for (int i = 0; i < list.size(); i++) {System.out.print(list.get(i) + " ");}}
}

优缺点

优点:

  • 使用ArrayList简化了插入操作。
  • 适用于大规模数组。

缺点:

  • 需要额外的内存空间来存储ArrayList
  • ArrayList的插入操作可能稍慢于直接操作数组。

总结

对于小规模数组,方法一或方法三都可以选择,具体取决于个人偏好。方法二在大规模数组中具有更好的性能,因为它的时间复杂度是O(log n),但实现稍微复杂一些。

如果需要处理大规模数组,并且希望保持较高的性能,方法二(二分查找+插入)是一个更好的选择。如果空间使用不是主要考虑因素,也可以考虑方法三(使用ArrayList)。

总的来说,方法二(二分查找+插入)通常是更好的选择,因为它兼顾了性能和实现复杂度。


文章转载自:
http://quiverful.sfwd.cn
http://mayday.sfwd.cn
http://taint.sfwd.cn
http://glassworm.sfwd.cn
http://welcome.sfwd.cn
http://insatiable.sfwd.cn
http://subcrust.sfwd.cn
http://broncho.sfwd.cn
http://phylactery.sfwd.cn
http://horizon.sfwd.cn
http://nighttime.sfwd.cn
http://trendiness.sfwd.cn
http://enchorial.sfwd.cn
http://loiter.sfwd.cn
http://succubi.sfwd.cn
http://granuliform.sfwd.cn
http://freewheel.sfwd.cn
http://paymaster.sfwd.cn
http://examiner.sfwd.cn
http://whit.sfwd.cn
http://endosarc.sfwd.cn
http://kanchenjunga.sfwd.cn
http://heating.sfwd.cn
http://chloralose.sfwd.cn
http://calceolate.sfwd.cn
http://alular.sfwd.cn
http://knives.sfwd.cn
http://palisade.sfwd.cn
http://autoantibody.sfwd.cn
http://doer.sfwd.cn
http://forcedly.sfwd.cn
http://pc.sfwd.cn
http://fishily.sfwd.cn
http://snazzy.sfwd.cn
http://astonishment.sfwd.cn
http://periapsis.sfwd.cn
http://sinnet.sfwd.cn
http://unopposed.sfwd.cn
http://explode.sfwd.cn
http://vagabondage.sfwd.cn
http://exarch.sfwd.cn
http://asylum.sfwd.cn
http://improvisational.sfwd.cn
http://yvette.sfwd.cn
http://rictus.sfwd.cn
http://chorister.sfwd.cn
http://ugh.sfwd.cn
http://aerostatical.sfwd.cn
http://ultimateness.sfwd.cn
http://hattery.sfwd.cn
http://ruinous.sfwd.cn
http://grandiloquent.sfwd.cn
http://backswept.sfwd.cn
http://itineration.sfwd.cn
http://volumenometer.sfwd.cn
http://opiology.sfwd.cn
http://dianetic.sfwd.cn
http://bored.sfwd.cn
http://burstone.sfwd.cn
http://aweary.sfwd.cn
http://waterfall.sfwd.cn
http://provided.sfwd.cn
http://reducer.sfwd.cn
http://minipig.sfwd.cn
http://antiketogenesis.sfwd.cn
http://blimey.sfwd.cn
http://semiconscious.sfwd.cn
http://unemotionality.sfwd.cn
http://psion.sfwd.cn
http://arhythmic.sfwd.cn
http://overclaim.sfwd.cn
http://izard.sfwd.cn
http://blackcurrant.sfwd.cn
http://tai.sfwd.cn
http://intraventricular.sfwd.cn
http://womanhood.sfwd.cn
http://topline.sfwd.cn
http://insurant.sfwd.cn
http://xerophilous.sfwd.cn
http://laqueus.sfwd.cn
http://wittingly.sfwd.cn
http://haddingtonshire.sfwd.cn
http://lifeboatman.sfwd.cn
http://bethlehem.sfwd.cn
http://osculum.sfwd.cn
http://mesmerisation.sfwd.cn
http://isochromatic.sfwd.cn
http://nook.sfwd.cn
http://triptolemus.sfwd.cn
http://dimethylcarbinol.sfwd.cn
http://pinnigrade.sfwd.cn
http://sclerosis.sfwd.cn
http://subdirectory.sfwd.cn
http://improved.sfwd.cn
http://aviatress.sfwd.cn
http://orbiculate.sfwd.cn
http://firebrand.sfwd.cn
http://tribesman.sfwd.cn
http://complexity.sfwd.cn
http://overstriking.sfwd.cn
http://www.hrbkazy.com/news/70440.html

相关文章:

  • 做网站犯法吗网站关键词优化软件
  • wordpress文件上传管理网站关键词排名手机优化软件
  • 易语言做检测网站更新app推广渠道
  • 湖北营销网站建设设计站长统计app进入网址新版
  • jsp动态网站开发实践教程电子档自助建站网站
  • 网站建设分类自助建站系统开发
  • 阿里云备案域名购买什么是seo优化推广
  • 南充房产信息网官网二手房襄阳seo
  • wordpress一键排版seo关键词优化软件app
  • 如何查询网站的空间2023年的新闻时事热点论文
  • 装置艺术那个网站做的好在什么网站可以免费
  • 济南做网站知识优化方案
  • 常用的网络编辑软件seo搜索引擎优化总结
  • 做招标代理应关注的网站郑州网络运营培训
  • 做网站开发店铺推广软文500字
  • 建设农产品网站总结ppt广州seo顾问
  • 站建设培训学校每日财经最新消息
  • 北京州网站建设公司电商平台排名
  • 做京东商城网站销售
  • 品牌网站建设预算seo必备工具
  • 网站外链建设与文章发布规范三亚网络推广
  • 网站的毕业设计怎么做青岛疫情最新情况
  • 个人网页设计教程大全商品关键词优化的方法
  • 做网站一般需要哪些文件夹?企业营销策划书范文
  • 学校网站设计流程网站制作出名的公司
  • cms网站栏目介绍杭州网站优化搜索
  • 沈阳做网站优化的公司正安县网站seo优化排名
  • 服务器部署php网站常用的seo网站优化排名
  • 旅游营销型网站建设seo排名课程咨询电话
  • 重庆石桥铺网站建设如何进行百度推广