Re: array random sort




"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: "Sorting" assignment
    ... And many others prefer to call partition exchange because "quicksort" ... bin B depending on whether it is greater than, ... If the array is already sorted, this means that you end up ... attempt to sort them. ...
    (comp.programming)
  • Re: A Fast sorting algorithm for almost sorted data
    ... far my compressor has potential but is nowhere near ready. ... It does however make heavy use of sorting. ... which I am currently calling Run sort. ... entire selected run can be added to the sorted output array. ...
    (comp.compression)
  • Re: Save & Sort
    ... You can copy your array to a scratch ... "Heap" sort. ... Dim lst As Long ... Dim tmp As String ...
    (microsoft.public.excel.programming)
  • Re: fast stable sort
    ... if you have an existing array, you can simply arrange an array of ... pointers to the items, and sort that. ... hence require swapping pages of virtual memory, ... each merge run instead of accessing them in sequential RAM ...
    (comp.programming)
  • A Fast sorting algorithm for almost sorted data
    ... which I am currently calling Run sort. ... entire selected run can be added to the sorted output array. ... public class RunSort implements Comparator ... public static void sort(Comparable a, int start,int end) ...
    (comp.compression)