Post Reply 
Shuffling arrays question
03-01-2015, 01:10 PM (This post was last modified: 03-01-2015 01:13 PM by Namir.)
Post: #4
RE: Shuffling arrays question
I was able to write the following un-sort function in Matlab. It does well in scrambling an ordered array. The function primes(m) returns an array of primes from 2 to m (or under it):

Code:
function x=unsort(x)
  n=length(x);
  m=fix(n/2);
  primeArr=primes(m);
  for j=1:195
    for k=1:length(primeArr)
      aprime=primeArr(k);
      for i=1:aprime:n-aprime
        t=x(i);
        x(i)=x(i+aprime);
        x(i+aprime)=t;
      end
    end
  end
end

The number 195 is optimum for sorting an array of 100000 integers in Matlab. While the above code is free of using random number generating functions or code, I feel that the effort needed to scramble an array is not minimal.

Namir
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Shuffling arrays question - Namir - 02-28-2015, 10:42 PM
RE: Shuffling arrays question - Paul Dale - 03-01-2015, 12:42 AM
RE: Shuffling arrays question - Namir - 03-01-2015 01:10 PM
RE: Shuffling arrays question - Namir - 03-01-2015, 06:48 PM
RE: Shuffling arrays question - BruceH - 03-07-2015, 05:56 AM



User(s) browsing this thread: 2 Guest(s)