插入一个数字排序后的数组! [英] Inserting a number into a sorted array!

查看:108
本文介绍了插入一个数字排序后的数组!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一段code为在适当位置插入一个数字排序后的数组(即数组仍应保持插入后排序)

I would like to write a piece of code for inserting a number into a sorted array at the appropriate position (i.e. the array should still remain sorted after insertion)

我的数据结构不允许重复。

My data structure doesn't allow duplicates.

我打算做这样的事情:


  1. 找到我应该使用二进制搜索中把这个元素右手食指

  2. 此元素创造空间,从该指数向下移动的所有元素。

  3. 将这个元素存在。

有没有其他更好的办法?

Is there any other better way?

推荐答案

如果你真的有一个数组,而不是一个更好的数据结构,这是最理想的。如果在执行是灵活的,看看 AA树 - 他们是相当快,易于实现。显然,需要比数组更多的空间,而如果比指针魔法元素的数量不够大,注意blit操作的缓慢是不值得的。

If you really have an array and not a better data structure, that's optimal. If you're flexible on the implementation, take a look at AA Trees - They're rather fast and easy to implement. Obviously, takes more space than array, and it's not worth it if the number of elements is not big enough to notice the slowness of the blit as compared to pointer magic.

这篇关于插入一个数字排序后的数组!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆