插入排序数组

编程入门 行业动态 更新时间:2024-10-13 04:23:15
本文介绍了插入排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将元素插入顺序在已排序列表中维护的正确位置。 我为数组分配了2 * n的大小,其余部分用999填充,因为它们当前未被使用。

I want to insert an element into the right place that order maintains in the sorted list. I allocated 2*n size for the array and filled the rest with 999 since they are not used currently.

ordered_insert(int number,int array[],int size){ int i=0; int temp1,temp2,index; while(eleman>array[i]){ i++;} //push the rest to right by one index=i; if(i<size){ temp1=array[i]; temp2= array[i+1]; array[i+1]=temp1; array[i+2]=temp2; i++; } array[index]=number; }

我不知道如何覆盖999,或者还有更好的方法吗?

I couldn't figure out how to overwrite 999s or is there a better way instead?

推荐答案

为了将所有后面的数组元素向前移动,您必须遍历向后排列数组,以免覆盖元素。

In order to move all the latter array elements one step ahead, you will have to traverse the array backwards so that you do not over-write the elements.

获得索引后,

int i = size; while ( i > index ) { array[i] = array[i-1]; i--; } array[i] = number; size++;

更多推荐

插入排序数组

本文发布于:2023-11-29 11:32:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!