Re: [問題] array shuffle

看板Programming作者 (ha(ruhi|yate)ism)時間18年前 (2007/07/15 23:00), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/9 (看更多)
※ 引述《mingchieh.bbs@bbs.cis.nctu.edu.tw (Bug J.)》之銘言: : ==> 在 FRAXIS.bbs@ptt.cc (喔喔) 的文章中提到: : > 這是在網路上看到的一個面試問題,不過我一直想不出來解法。 : > 給你一個長度為2n的陣列,其中元素為a1, a2, ..., an, b1, b2, ..., bn : > 寫一個程式把這個陣列轉換成a1, b1, a2, b2, ..., an, bn : > 時間限制是O(n),空間限制是O(1)。 : > 我嘗試用in-place rearrangement的方法去做,但是沒辦法成功。 : > 有什麼好的辦法嘛? : kunth shuffle 不對吧? 根據維基百科 http://en.wikipedia.org/wiki/Shuffling#Shuffling_algorithms The second, generally known as the Knuth shuffle or Fisher-Yates shuffle, is a linear-time algorithm which involves moving through the pack from top to bottom, swapping each card in turn with another card from a random position in the part of the pack that has not yet been passed through (including itself). Providing that the random numbers are unbiased, this will always generate a random permutation. 這並不是原PO要的shuffle... (而且你還把人家Knuth的名字拼錯了= =) -- 'Oh, Harry, dont't you see?' Hermione breathed. 'If she could have done one thing to make absolutely sure that every single person in this school will read your interview, it was banning it!' ---'Harry Potter and the order of the phoenix', P513 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.84.44.21
文章代碼(AID): #16cZQcyX (Programming)
文章代碼(AID): #16cZQcyX (Programming)