Author |
Topic: riffle shuffling an array (Read 508 times) |
|
inexorable
Full Member
Posts: 211
|
|
riffle shuffling an array
« on: Jul 15th, 2007, 9:46pm » |
Quote Modify
|
Given an array with 2n elements a[1], a[2], a[3] .....a[n], ..a[2n]. give an algorithm to rearrange it as a[1], a[n+1], a[2], a[n+2], a[3], a[n+3]....a[n], a[2n] without using extra space in O(n) time?
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: riffle shuffling an array
« Reply #1 on: Jul 15th, 2007, 11:54pm » |
Quote Modify
|
It helps a lot if n is a power of 2. in which case you can swap [n/2+1..n] with [n+1..3n/2], then repeat the process for both halves of the array until you reach n=1
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
|