#排序 #列表 #二分查找 #元素 #搜索算法 #索引 #算法

rusted_list

为 Rust 实现的一个始终排序的列表,可以在使用列表/数组时对这些需要进行排序的数字进行操作。

2 个稳定版本

1.1.0 2019年12月26日
1.0.0 2019年6月28日
0.1.1 2019年5月31日
0.1.0 2019年5月31日

#2112 in 算法

MIT/Apache

8KB
148

锈蚀列表

Crates.io

一个快速且始终排序的列表,可用于在列表/数组中使用时对这些需要进行排序的数字进行操作。

如何

rusted_list 使用二分搜索算法来确保列表是有序的。这仅因为我们可以保证列表始终是有序的,因为我们只允许开发者使用这种方法插入,而不是其他方式。

插入

let listing = rusted::Rusted::new();
listing.insert(3);

Because the size of the list is 0, we just insert the element into the vector.



listing.insert(4);

Because the differenz of max => 1 and min => 0 is 1, we check if the element is bigger or smaller than the current element at index 1.
If element < vec[0]
	insert(0);
Else
	push(element);



listing ==> [3,4]
listing.insert(10);


max => 1
min => 0

if max - min == 1
	if element < vec[max]
		insert(max);	
	else
		insert(max - 1);
Else
	if element > vec[max + min / 2]
		repeat(middle,max)
	else
		repeat(min,middle)

本质上,列表使用向量作为其基础。对于插入,它通过递归迭代并应用二分搜索来找到插入元素的位置。

运行时复杂度

由于列表在插入元素时使用二分搜索,因此其运行时复杂度为
O(log(N)),即大O表示法。

无运行时依赖