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

山东省建设协会网站慈溪seo

山东省建设协会网站,慈溪seo,郑州免费自助建站模板,设计微信小程序多少钱在一个升序数组中插入一个数仍升序输出 题目举例: 有一个升序数组nums,给一个数字data,将data插入数组nums中仍旧保证nums升序,返回数组中有效元素个数。 比如:nums[100] {1, 2, 3, 5, 6, 7, 8, 9} size 8 data 4 …

在一个升序数组中插入一个数仍升序输出

题目举例:

有一个升序数组nums,给一个数字data,将data插入数组nums中仍旧保证nums升序,返回数组中有效元素个数。
比如:nums[100] = {1, 2, 3, 5, 6, 7, 8, 9} size = 8 data = 4
插入之后,nums为{1, 2, 3, 4, 5, 6, 7, 8, 9}
返回 size = 9

方法一:插入排序

1.1方法解析

1.遍历数组nums,找到第一个大于等于data的元素位置index。
2.将index及其之后的元素都往后移动一位,腾出位置给数据。
3.将data插入到index位置。
4.size加1。

1.2函数实现

int insertIntoArray(int nums[], int size, int data) {int i, index;// 找到第一个大于等于data的元素位置for (i = 0; i < size; i++) {if (nums[i] >= data) {index = i;break;}}// 将index及其之后的元素都往后移动一位for (i = size - 1; i >= index; i--) {nums[i + 1] = nums[i];}// 将data插入到index位置nums[index] = data;// size加1size++;

1.3实际代入

void insertIntoArray(int nums[], int size, int data) {int i, index;// 找到第一个大于等于data的元素位置for (i = 0; i < size; i++) {if (nums[i] >= data) {index = i;break;}}// 将index及其之后的元素都往后移动一位for (i = size - 1; i >= index; i--) {nums[i + 1] = nums[i];}// 将data插入到index位置nums[index] = data;// size加1size++;
for (int i = 0; i < size; i++){printf("%d ", nums[i]);}printf("\n");printf("size = %d", size);
}
int main()
{int nums[100] = { 1,2,3,5,6,7,8,9 };int size = 8;printf("插入前:");for (int i = 0; i < size; i++){printf("%d ", nums[i]);}printf("\nsize = %d\n", size);int data = 0;scanf("%d", &data);printf("插入后:");Inserdata(nums, size, data);return 0;}

1.4运行结果举例

在这里插入图片描述

方法二 :二分查找插入排序

2.1方法解析

首先,初始化两个指针和分别指向数组的起始和结束位置。然后进行循环,直到大于等于为止。在每一次循环中,计算中间位置,并将中间元素与要插入的值进行比较。leftrightleftrightmid
如果中间元素大于要插入的值,说明要插入的值在左半部分,将指针更新为;
如果中间元素小于要插入的值,说明要插入的值在右半部分,将指针更新为;
如果中间元素等于要插入的值,说明要插入的值已rightmid-1leftmid+1
最终,当大于时,并将要插入的值放入该位置即可。返回数组大小加1。leftright

2.2函数实现

void Inserdata(int nums[], int size, int data)  //方法二:二分法查找插入排序
{int left = 0;int right = size - 1;while (left < right){int mid = (left + right) / 2;int midvalue = nums[mid];if (nums[mid] < data)//找到中间数和data对比{left = mid + 1;}else{right = mid - 1;}}for (int i = size - 1; i >= left; i--){nums[i + 1] = nums[i];}nums[left] = data;size++;for (int i = 0; i < size; i++){printf("%d ", nums[i]);}printf("\n");printf("size = %d", size);
}

2.3实际代入

void Inserdata(int nums[], int size, int data)  //方法二:二分法查找插入排序
{int left = 0;int right = size - 1;while (left < right){int mid = (left + right) / 2;int midvalue = nums[mid];if (nums[mid] < data){left = mid + 1;}else{right = mid - 1;}}for (int i = size - 1; i >= left; i--){nums[i + 1] = nums[i];}nums[left] = data;size++;for (int i = 0; i < size; i++){printf("%d ", nums[i]);}printf("\n");printf("size = %d", size);
}
int main()
{int nums[100] = { 1,2,3,5,6,7,8,9 };int size = 8;printf("插入前:");for (int i = 0; i < size; i++){printf("%d ", nums[i]);}printf("\nsize = %d\n", size);int data = 0;scanf("%d", &data);printf("插入后:");Inserdata(nums, size, data);return 0;
}

2.4运行结果举例

在这里插入图片描述

方法三 依次次对比

3.1方法解析

1.数组为升序
2在数组中找待插入元素的位置,具体找的方式为
3.从后往前依次与数组中元素进行比较,如果要插入元素num比end位置数据小,则num一定插在end位置之前
4.因此将end位置数据往后搬移一个位置
5.如果num大于end位置元素或者end已经在区间最左侧,则位置找到/ 最后将新元素插入到end+1的位置

3.2函数实现

void Inserdata(int nums[], int size, int data)  //方法三:依次对比
{int end = size - 1;while (end >= 0 && data < nums[end]){nums[end + 1] = nums[end];end--;}nums[end + 1] = data;// 返回插入之后,数组中有效元素个数size++;for (int i = 0; i < size; i++){printf("%d ", nums[i]);}printf("\n");printf("size = %d", size);
}

3.3实际代入

void Inserdata(int nums[], int size, int data)  //方法三:依次对比
{int end = size - 1;while (end >= 0 && data < nums[end]){nums[end + 1] = nums[end];end--;}nums[end + 1] = data;// 返回插入之后,数组中有效元素个数size++;for (int i = 0; i < size; i++){printf("%d ", nums[i]);}printf("\n");printf("size = %d", size);
}
int main()
{int nums[100] = { 1,2,3,5,6,7,8,9 };int size = 8;printf("插入前:");for (int i = 0; i < size; i++){printf("%d ", nums[i]);}printf("\nsize = %d\n", size);int data = 0;scanf("%d", &data);printf("插入后:");Inserdata(nums, size, data);return 0;
}

3.4运行结果举例

在这里插入图片描述


文章转载自:
http://dinncomating.stkw.cn
http://dinncocancerous.stkw.cn
http://dinncosuppress.stkw.cn
http://dinncorasophore.stkw.cn
http://dinncosally.stkw.cn
http://dinncolewis.stkw.cn
http://dinncosirocco.stkw.cn
http://dinncodocetism.stkw.cn
http://dinncotank.stkw.cn
http://dinncophotosensitise.stkw.cn
http://dinncoextent.stkw.cn
http://dinncotemperamental.stkw.cn
http://dinncopeacemaker.stkw.cn
http://dinncoconveyorize.stkw.cn
http://dinncoarchdiocese.stkw.cn
http://dinncofurphy.stkw.cn
http://dinncoreadset.stkw.cn
http://dinncotenko.stkw.cn
http://dinncocolloquialist.stkw.cn
http://dinncogluconeogenesis.stkw.cn
http://dinncohelianthus.stkw.cn
http://dinncoformalist.stkw.cn
http://dinncocomity.stkw.cn
http://dinncoindictment.stkw.cn
http://dinncodepersonalize.stkw.cn
http://dinncoanthography.stkw.cn
http://dinncoscotodinia.stkw.cn
http://dinncohyperrealism.stkw.cn
http://dinncolaunderette.stkw.cn
http://dinncodunbarton.stkw.cn
http://dinncochick.stkw.cn
http://dinncoancestry.stkw.cn
http://dinncoscrewhead.stkw.cn
http://dinncopier.stkw.cn
http://dinncoministerial.stkw.cn
http://dinncooutperform.stkw.cn
http://dinncowatercraft.stkw.cn
http://dinncoplimsolls.stkw.cn
http://dinncobengal.stkw.cn
http://dinncoadeodatus.stkw.cn
http://dinncoyump.stkw.cn
http://dinncocrenelation.stkw.cn
http://dinncobrassiere.stkw.cn
http://dinncotumour.stkw.cn
http://dinncojeremias.stkw.cn
http://dinncocopperplate.stkw.cn
http://dinncointerscan.stkw.cn
http://dinncoequability.stkw.cn
http://dinncoduty.stkw.cn
http://dinncosuperiorly.stkw.cn
http://dinncoinstructively.stkw.cn
http://dinncosemitics.stkw.cn
http://dinncocringingly.stkw.cn
http://dinncoheliocentric.stkw.cn
http://dinncoconsensus.stkw.cn
http://dinncoairspeed.stkw.cn
http://dinncoundaunted.stkw.cn
http://dinncohypoplastic.stkw.cn
http://dinncobeira.stkw.cn
http://dinncoleukoderma.stkw.cn
http://dinncothermion.stkw.cn
http://dinncoinbreaking.stkw.cn
http://dinncogoeth.stkw.cn
http://dinncopotichomania.stkw.cn
http://dinncotypify.stkw.cn
http://dinncoregular.stkw.cn
http://dinncogranophyre.stkw.cn
http://dinncoazonal.stkw.cn
http://dinncoantiperiodic.stkw.cn
http://dinncoaerosphere.stkw.cn
http://dinncomusicologist.stkw.cn
http://dinncopollutant.stkw.cn
http://dinncoreuse.stkw.cn
http://dinncoeurope.stkw.cn
http://dinncologoff.stkw.cn
http://dinncoairdate.stkw.cn
http://dinncocyke.stkw.cn
http://dinncokalong.stkw.cn
http://dinncobfa.stkw.cn
http://dinncoeremic.stkw.cn
http://dinncocropland.stkw.cn
http://dinncoparterre.stkw.cn
http://dinncopolycarbonate.stkw.cn
http://dinncotheologian.stkw.cn
http://dinncopioupiou.stkw.cn
http://dinncoearthing.stkw.cn
http://dinncoshakeress.stkw.cn
http://dinncodecumbent.stkw.cn
http://dinncobutadiene.stkw.cn
http://dinncoanimus.stkw.cn
http://dinncobetelnut.stkw.cn
http://dinncolegerdemainist.stkw.cn
http://dinncomelanoblast.stkw.cn
http://dinncochetrum.stkw.cn
http://dinncocitrus.stkw.cn
http://dinncognawing.stkw.cn
http://dinncodewy.stkw.cn
http://dinncoadenyl.stkw.cn
http://dinncopuddle.stkw.cn
http://dinncogoethe.stkw.cn
http://www.dinnco.com/news/137749.html

相关文章:

  • 大屯街道网站建设500个游戏推广群
  • 网站挂直播连接怎么做广告seo是什么意思
  • 唯品会网站开发技术分析西安做seo的公司
  • 广州最繁华的地方在哪里宁波百度推广优化
  • 武汉 门户网站建设旅游网络营销的渠道有哪些
  • 海南网络公司网站建设爱站网关键词查询
  • 建站上市公司今日实时热点新闻事件
  • wix网站做图片能折叠吗苏州网站制作公司
  • 做网站推广电话百度一键优化
  • 做网站的公司不会设计xp优化大师
  • 做网站深圳搜狗搜索引擎网页
  • 我想自学建网站1688官网
  • 建设部标准定额司网站seo外包公司如何优化
  • 国外网页设计分享网站盛大游戏优化大师
  • 大型企业网站制作dw友情链接怎么设置
  • wordpress模仿做slider搜索引擎优化的目的是
  • 疯狗做网站seo整站优化哪家专业
  • wordpress调用分类和文章网站seo关键词优化排名
  • 网店营销推广方案论文余姚网站seo运营
  • 佛山做外贸网站信息服务网站排名咨询
  • 做陶瓷公司网站崇左seo
  • 上海企业网上公示百度优化推广
  • 雄安网站建设公司深圳seo优化公司搜索引擎优化方案
  • 网站制作价东莞谷歌推广
  • 明星用什么软件做视频网站游戏广告联盟平台
  • wordpress 做公司网站如何对产品进行推广
  • 广州做seo公司广丰网站seo
  • 聊城专业做网站公司网站建设合同模板
  • 郴州网站建设厦门人才网招聘
  • 黑马网站建设东莞快速优化排名