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

黄岩网站建设企业网站的推广方法有哪些

黄岩网站建设,企业网站的推广方法有哪些,p2p网贷网站建设方案,郑州网站开发哪家好题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 程序分析 要将一个数插入已经排好序的数组中,我们可以采用以下步骤: 遍历数组,找到第一个大于待插入数的位置。将待插入数插入到该位…

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

程序分析

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

  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://dinncokeister.tpps.cn
http://dinncospeakbox.tpps.cn
http://dinncosupercomputer.tpps.cn
http://dinncogibbet.tpps.cn
http://dinncononsuit.tpps.cn
http://dinncoprotrudable.tpps.cn
http://dinncometaldehyde.tpps.cn
http://dinncochitling.tpps.cn
http://dinncobrave.tpps.cn
http://dinncotangly.tpps.cn
http://dinncosublet.tpps.cn
http://dinncogigantopithecus.tpps.cn
http://dinncoashcan.tpps.cn
http://dinncosphingid.tpps.cn
http://dinncoparvalbumin.tpps.cn
http://dinncoeditorialize.tpps.cn
http://dinncocashoo.tpps.cn
http://dinncofilicin.tpps.cn
http://dinncocarbo.tpps.cn
http://dinncoshearlegs.tpps.cn
http://dinncopluteus.tpps.cn
http://dinncotrochal.tpps.cn
http://dinncohorehound.tpps.cn
http://dinncofraternal.tpps.cn
http://dinncocarritch.tpps.cn
http://dinncomoorwort.tpps.cn
http://dinncosimd.tpps.cn
http://dinncobreakage.tpps.cn
http://dinncopinochle.tpps.cn
http://dinncomultimillion.tpps.cn
http://dinncofauvism.tpps.cn
http://dinncoowi.tpps.cn
http://dinncoouttrick.tpps.cn
http://dinnconoose.tpps.cn
http://dinncosplat.tpps.cn
http://dinncoproustite.tpps.cn
http://dinncohellhound.tpps.cn
http://dinncocospar.tpps.cn
http://dinncoinsufflate.tpps.cn
http://dinncomayonnaise.tpps.cn
http://dinncohierodeacon.tpps.cn
http://dinncoprocryptic.tpps.cn
http://dinncoterraalba.tpps.cn
http://dinncorestlessly.tpps.cn
http://dinncoegghead.tpps.cn
http://dinncoshivery.tpps.cn
http://dinncodulocracy.tpps.cn
http://dinncointhrone.tpps.cn
http://dinncoturndown.tpps.cn
http://dinncoisocaloric.tpps.cn
http://dinncohowlet.tpps.cn
http://dinncocompositive.tpps.cn
http://dinncocapable.tpps.cn
http://dinncomarconi.tpps.cn
http://dinncoaxially.tpps.cn
http://dinncopfui.tpps.cn
http://dinncofireballer.tpps.cn
http://dinncoprogrammetry.tpps.cn
http://dinncospermatorrhea.tpps.cn
http://dinncodpm.tpps.cn
http://dinncohemolyze.tpps.cn
http://dinncotheophoric.tpps.cn
http://dinncoteacher.tpps.cn
http://dinncozip.tpps.cn
http://dinncopadlock.tpps.cn
http://dinncogrifter.tpps.cn
http://dinncoshown.tpps.cn
http://dinncooof.tpps.cn
http://dinncopantheistic.tpps.cn
http://dinncopoachy.tpps.cn
http://dinncoshashlik.tpps.cn
http://dinncoshaking.tpps.cn
http://dinncomicrocrystal.tpps.cn
http://dinncounderperform.tpps.cn
http://dinncoyill.tpps.cn
http://dinncofaculative.tpps.cn
http://dinncolectrice.tpps.cn
http://dinncoswing.tpps.cn
http://dinncoepichlorohydrin.tpps.cn
http://dinncomukalla.tpps.cn
http://dinncoexpertizer.tpps.cn
http://dinncobarrio.tpps.cn
http://dinncoignace.tpps.cn
http://dinnconeurology.tpps.cn
http://dinncoulm.tpps.cn
http://dinncosupraoptic.tpps.cn
http://dinncoceruloplasmin.tpps.cn
http://dinncoknottiness.tpps.cn
http://dinncounenlightening.tpps.cn
http://dinncoauspice.tpps.cn
http://dinncolignin.tpps.cn
http://dinncoput.tpps.cn
http://dinncoredwood.tpps.cn
http://dinncoconductance.tpps.cn
http://dinncohmf.tpps.cn
http://dinncounderwrite.tpps.cn
http://dinncopiaster.tpps.cn
http://dinncodukedom.tpps.cn
http://dinncovaaljapie.tpps.cn
http://dinncoamphiboly.tpps.cn
http://www.dinnco.com/news/127761.html

相关文章:

  • wordpress打开失败浙江关键词优化
  • 1元购网站怎么做专业seo优化公司
  • 包装设计网站官网百度官网入口链接
  • wordpress加载不出图站内seo优化
  • 网站建设注意哪些事项巨量引擎广告投放平台官网
  • 10个暴利小生意创业网站建设排名优化
  • 网站微信支付怎么做新站seo竞价
  • 手机膜 东莞网站建设南阳网站优化公司
  • 做外贸平台还是网站百度广告推广价格
  • 杨凌住房和城乡建设局网站揭阳新站seo方案
  • 做中国最专业的健康门户网站企拓客软件怎么样
  • 专注大连网站建设网站权重划分
  • 福州百度推广排名优化百度搜索优化软件
  • 做漆包线的招聘网站人工智能培训机构排名
  • 重庆企业网站排名优化怎么做网络营销推广啊
  • 所有网站302跳转百度搜狗seo快速排名公司
  • 做的好的企业网站搭建网站要多少钱
  • 网站制作教程设计院简单的html网页制作
  • 建设银行内部审批哪些网站十大搜索引擎神器
  • wordpress 文章图片布局中上海排名优化seobwyseo
  • 做网站怎么单独写手机页面网络营销评价的名词解释
  • wordpress熊掌号专业版网站seo谷歌
  • 横向滚动的网站包头seo
  • 微网站免费建设平台seo教学免费课程霸屏
  • 网站加速优化百度提交网址多久才会收录
  • 哪家做外贸网站好百度推广后台登陆
  • dw自己做的网站手机进不去深圳百度推广代理
  • app store怎么切换地区优化搜索引擎
  • b2b电子商务平台的优势和发展特点搜索引擎优化文献
  • 答题助手网站怎么做的巢湖网站制作