Re: [問題] array shuffle
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 9 篇):
Programming 近期熱門文章
PTT數位生活區 即時熱門文章