Re: array random sort



The problem with Fred's solution is that while it appears to be an O(n)
algorithm, RemoveAt itself is O(n), making the overall algorithm O(n^2). If
the input array get big, it's going to get very slow.

The trick is to avoid deleting anything. We're starting with an array
of N, and we finish with an array of N, so there's no reason to delete
anything. This version is really O(n):

// Shuffles inplace.
public static List<T> shuffleList(List<T> listToShuffle)
{
for (int k = listToShuffle.Count-1; k > 1; --k)
{
int randIndx = Common.rand.Next(k); //
T temp = listToShuffle[k];
listToShuffle[k] = listToShuffle[randIndx]; // move random num to
end of list.
listToShuffle[randIndx] = temp;
}
return listToShuffle;
}

So, say, for example, listToShuffle has 10 elements (0-9). First we pick a
random number between 0 & 8, and we swap the element at the position with
the element at position 9. Then we pick an number between 0 & 7, and swap
that element with [8]. And so on until we're pick a number between 0 & 1 and
swapping it with listToShuffle[2]
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Fred Mellender" <nospamPlease_fredm@xxxxxxxxxxxxxxx> wrote in message
news:3h4ff.13497$cg.7381@xxxxxxxxxxxxxxxx
>
> "gl" <gl@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:7E0D71F9-962F-4A97-9770-EC71E6659A33@xxxxxxxxxxxxxxxx
> > How do I take an array or arraylist, and sort it randomly? Like suppose
> > the
> > items in it are (1,2,3,4,5) and I want to get it to be in a random order
> > (2,3,1,4,5). How do you do that? I think it's a call to the array or
array
> > lists sort method, but i'm not exactly sure how you do it.
>
>
> public static List<T> shuffledList(List<T> listToShuffle)
> {
> /*
> * Make a new list of elements picked from listToShuffle
> * in a random order.
> */
>
> List<int> ints = new List<int>(listToShuffle.Count); //0, 1,
> 2, ...
> for (int i = 0; i < listToShuffle.Count; i++)
> ints.Add(i);
>
> List<T> randList = new List<T>(listToShuffle.Capacity);
>
> for (int k = 0; k < listToShuffle.Count; k++)
> {
> int randIndx = Common.rand.Next(ints.Count); //random
> from 0, 2, .. not already picked
> int randK = ints[randIndx];
> randList.Add(listToShuffle[randK]);
> ints.RemoveAt(randIndx);
> }
>
> return randList;
> }
>
>


.



Relevant Pages

  • Re: regex question - trying to find ".mp3" in a SELECT box
    ... But `Array' is a Function object reference. ... Establish a new execution context using F's FormalParameterList, ... Since is not a primitive value, the exception should be thrown. ... | 5.2 Algorithm Conventions ...
    (comp.lang.javascript)
  • Re: Mergesort Vs Quicksort
    ... I might not have correctly remembered my algorithm of months ago ... for sorting records in an array using one auxilary of the ... on how things turn out from lower levels of recursion, ... whether the number of records in the array segment to be sorted is ...
    (comp.programming)
  • Re: Reference to derived type element by index?
    ... as a set of distinctively-named scalars. ... In another scope, the same common block ... would be a single array. ... algorithm and a specific application of the algorithm, ...
    (comp.lang.fortran)
  • Re: counting subsets of S so that sum(S_n) = N
    ... and make my algorithm generate only unique subsets? ... and the array of boolean at one step of the algorithm is ... implementation without needing recursion. ... subsequence in W, process it and return. ...
    (comp.programming)
  • [SUMMARY] Maximum Sub-Array (#131)
    ... # sum the integer values of array contents ... algorithm, ... all possible lengths, to check each subarray. ... $ time ruby -r max_sub_array ...
    (comp.lang.ruby)