swift演算法手記-10

來源:互聯網
上載者:User

標籤:csdn   目標   jsb   儲存   log   var   下列表   sig   演算法   

http://blog.csdn.net/myhaspl    private func findnode(val:Int)->Bool{//http://blog.csdn.net/myhaspl        //尋找結點http://blog.csdn.net/myhaspl        if let mysltop = slinktop{            var mynode:skipLinkNode=mysltop            while true{                while true{                    if let nextnd = mynode.nextnode {                       let nodeval = nextnd.ndval                       if  nodeval < val{                           mynode=nextnd                           continue                        }                        if nodeval == val{                           return true                        }                    }                    break                }                if mynode.downnode == nil{                   return false                }                else{                    mynode = mynode.downnode!                }            }        }        else{            return false        }    }    ....    ........           private func deletenode(val:Int){        if let mysltop=slinktop{            var mynode:skipLinkNode=mysltop            while true{                while true{                    if let nextnd = mynode.nextnode {                        let nodeval = nextnd.ndval                        if  nodeval < val{                            mynode=nextnd                            continue                        }                        if nodeval == val{                            //delete node from the level                            mynode.nextnode=nextnd.nextnode                        }                    }                    break                }                if mynode.downnode == nil{                    //最底層http://blog.csdn.net/myhaspl                    break                }                else{                    mynode = mynode.downnode!                }            }        }    }    private func insertnode(val:Int){        //插入結點        let insertlv=getinsertlv()        let currtop=currlist(insertlv)        var mynode:skipLinkNode = currtop        var isfind:Bool=false        var searchnodes=[(skipLinkNode,skipLinkNode)]()                while true{            while let ntnode=mynode.nextnode{                if ntnode.ndval < val {                    mynode = ntnode                }                else if ntnode.ndval == val {                    isfind=true                    searchnodes.append((ntnode,ntnode.nextnode!))                    break                }                else{                    searchnodes.append((mynode,ntnode))                    break                }            }            if let dnnode=mynode.downnode {                mynode=dnnode            }            else{                break            }        }                var newnd:skipLinkNode?        var upnd:skipLinkNode?

var dnnd:skipLinkNode? var prend:skipLinkNode var ntnd:skipLinkNode if !isfind { for nodes in searchnodes{ (prend,ntnd)=nodes upnd=newnd newnd=createnode(prend,nextnd:ntnd,nodetype: ntype.node,val:val) if upnd != nil{ upnd!.downnode=newnd } else{ dnnd = newnd! } } if insertlv>slinklevel { addnewlevel(val,dnnode: dnnd!) } } else{ let nodelist=searchnodes.last (prend,ntnd)=nodelist! newnd=createnode(prend,nextnd:ntnd,nodetype: ntype.node,val:val) } } private func slinkstatus()->String{ var mystatus:String="" var nownode:skipLinkNode var i=slinklevel while i>=0{ nownode=slist[i] mystatus+="||top=>" while true{ if let ntnode=nownode.nextnode { if ntnode.ndtype != ntype.end { mystatus+=String(ntnode.ndval)+"--" } else{ mystatus+="==>end||" } nownode=ntnode } else{ break } } mystatus += "\n" i-=1 } return mystatus }

本部落格全部內容是原創。假設轉載請註明來源http://blog.csdn.net/myhaspl/

跳躍鏈表是一種隨機化資料結構,基於並聯的鏈表,其效率可比擬於二叉尋找樹(對於大多數操作須要O(log  n)平均時間),而且對並發演算法友好。基本上,跳躍列表是對有序的鏈表添加上附加的前進連結,添加是以隨機化的方式進行的,所以在列表中的尋找能夠高速的跳過部分列表(因此得名)。全部操作都以對數隨機化的時間進行。

跳躍列表是按層建造的。

底層是一個普通的有序鏈表。每一個更高層都充當以下列表的"高速跑道"。這裡在層 i 中的元素按某個固定的機率 p 出如今層 i+1 中。平均起來,每一個元素都在 1/(1-p) 個列表中出現,而最高層的元素(一般是在跳躍列表前端的一個特殊的頭元素)在 O(log1/pn) 個列表中出現。1 - - - - - - 4 - - - 61 - - - 3 - 4 - - - 6 - - - - - - 91 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10結構執行個體要尋找一個目標元素。起步於頭元素和頂層列表。並沿著每一個鏈表搜尋。直到到達小於或的等於目標的最後一個元素。

通過跟蹤起自目標直到到達在更高列表中出現的元素的反向尋找路徑,在每一個鏈表中預期的步數顯而易見是 1/p。所以尋找的整體代價是 O(log1/p n / p)。當p 是常數時是 O(log n)。通過選擇不同 p 值。就能夠在尋找代價和儲存代價之間作出權衡。

pid=72995dcc07ad698ab8d8d&bid=72995dcc01017w1t&uid=1922653644" style="text-decoration: none; color: rgb(79, 114, 0);">

這裡元素不多,體現不出優勢,假設元素足夠多,這樣的索引結構就能體現出優勢來了。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />

swift演算法手記-10

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.