Algorithm 理想的跳过列表?O(n)运行时?
我正在努力找到最佳的算法Algorithm 理想的跳过列表?O(n)运行时?,algorithm,linked-list,skip-lists,Algorithm,Linked List,Skip Lists,我正在努力找到最佳的算法 converting an "ordinary" linked list into an `ideal skip list` 其中,理想跳过列表的定义是,在第一级中,我们将拥有所有 元素,在一半以上的级别,四分之一之后的级别。。。等等 我正在考虑运行时的O(n)O(n)O(n)O(n)O(n)O(n)运行时涉及为每个节点投掷硬币 原始链接列表,无论是否针对特定节点,是否应该向上移动,并为当前节点创建另一个重复节点。。。 最终这个算法会产生O(n),有更好的算法吗
converting an "ordinary" linked list
into an `ideal skip list`
其中,理想跳过列表的定义是,在第一级中,我们将拥有所有
元素,在一半以上的级别,四分之一之后的级别。。。等等
我正在考虑运行时的O(n)
O(n)
O(n)O(n)O(n)O(n)运行时涉及为每个节点投掷硬币
原始链接列表,无论是否针对特定节点,是否应该向上移动,并为当前节点创建另一个重复节点。。。
最终这个算法会产生O(n),有更好的算法吗
关于我假设链表已排序-否则无法使用基于比较的算法进行排序,因为您需要在
Omega(nlogn)
- 迭代列表的“最高级别”,每隔一秒添加一个“链接节点”
- 重复此操作,直到最高级别只有一个节点
最终,您将看到大小为1,2,4,…,n/2的列表相互链接 伪代码:
makeSkipList(list):
if (list == null || list.next == null): //stop clause - a list of size 1
return
//root is the next level list, which will have n/2 elements.
root <- new link node
root.linkedNode <- list //linked node is linking "down" in the skip list.
root.next <- null //next is linking "right" in the skip list.
lastLinkNode <- root
i <- 1
//we create a link every second element
for each node in list, exlude the first element:
if (i++ %2 == 0): //for every 2nd element, create a link node.
lastLinkNode.next <- new link node
lastLinkNode <- lastLinkNode.next //setting the "down" field to the element in the list
lastLinkNode.linkedNode <- node
lastLinkNode.next <- null
makeSkipList(root) //recursively invoke on the new list, which is of size n/2.
makeSkipList(列表):
if(list==null | | list.next==null)://stop子句-大小为1的列表
返回
//root是下一级列表,它将有n/2个元素。
root我假设链表已排序-否则无法使用基于比较的算法进行排序,因为您需要在Omega(nlogn)
- 迭代列表的“最高级别”,每隔一秒添加一个“链接节点”
- 重复此操作,直到最高级别只有一个节点
其思想是生成一个新列表,其大小为原始列表的一半,每第二个链接中链接一个原始列表,然后递归调用较小的列表,直到达到大小为1的列表。
最终,您将看到大小为1,2,4,…,n/2的列表相互链接
伪代码:
makeSkipList(list):
if (list == null || list.next == null): //stop clause - a list of size 1
return
//root is the next level list, which will have n/2 elements.
root <- new link node
root.linkedNode <- list //linked node is linking "down" in the skip list.
root.next <- null //next is linking "right" in the skip list.
lastLinkNode <- root
i <- 1
//we create a link every second element
for each node in list, exlude the first element:
if (i++ %2 == 0): //for every 2nd element, create a link node.
lastLinkNode.next <- new link node
lastLinkNode <- lastLinkNode.next //setting the "down" field to the element in the list
lastLinkNode.linkedNode <- node
lastLinkNode.next <- null
makeSkipList(root) //recursively invoke on the new list, which is of size n/2.
makeSkipList(列表):
if(list==null | | list.next==null)://stop子句-大小为1的列表
返回
//root是下一级列表,它将有n/2个元素。
根请参见您提到的随机算法请参见您提到的随机算法请参见“最高级别”是什么意思?我从0级开始(完整的原始链表),然后在第一个节点之后取一个,即在“头”之后取和节点,然后上楼为it放置一个新节点,楼上?你的算法对我来说不太清楚……你能解释一下吗?@ron:我编辑了答案,并添加了一个描述递归函数的注释伪代码,其思想是——为大小为n/2的跳过列表创建一个链接,然后递归调用较小的列表,直到得到大小为1的列表,最终得到大小为1、2、4的列表, .... , n/2,n您所说的“最高级别”是什么意思?我从0级开始(完整的原始链表),然后在第一个节点之后取一个,即在“头”之后取和节点,然后上楼为it放置一个新节点,楼上?你的算法对我来说不太清楚……你能解释一下吗?@ron:我编辑了答案,并添加了一个描述递归函数的注释伪代码,其思想是——为大小为n/2的跳过列表创建一个链接,然后递归调用较小的列表,直到得到大小为1的列表,最终得到大小为1、2、4的列表, .... , n/2,n