6 Replies - 2146 Views - Last Post: 19 December 2010 - 06:03 PM Rate Topic: -----

#1 BioElectro   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 19-December 10

Randomly Sorting an Array Without Repeating Elements

Posted 19 December 2010 - 04:20 PM

Hi,

I realize this is an old thread, but I do not understand how this works. Specifically, I don't understand how an element avoids being selected more than once when the same index value reoccurs. This seems like magic to me. Clearly, I am missing something important here. How does
int pos = order[index]
know to select the
order[--orderLen]
value rather than the value from the random calculation?


View Postbaavgai, on 13 March 2009 - 07:23 AM, said:

Awww, that's cheating! :P

Here's how you'd do it just another array, if you're interested:
import java.util.*; 

public class Shuffle3 { 
	public static void main(String[] arguments) { 
		String names[] = { "Peter", "Patricia", "Hunter", "Sarah", 
			"Gabe", "Gina", "Rob", "John", "Zoey", "Tammy", "Robert", 
			"Sean", "Paschal", "Kathy", "Neleh", "Vecepia" };
		
		int [] order = new int[names.length];
			
		System.out.println("The original order:"); 
		for (int i = 0; i < names.length; i++){
			order[i] = i;
			System.out.println(i + ": " + names[i]); 
		}
		
		System.out.println("The new order:"); 
		int orderLen = names.length;
		for (int i = 0; i < names.length; i++){
			int index = (int) (Math.random() * orderLen);
			int pos = order[index];
			System.out.println(pos + ": " + names[pos]); 
			order[index] = order[--orderLen];
		}
	}
} 



Is This A Good Question/Topic? 1
  • +

Replies To: Randomly Sorting an Array Without Repeating Elements

#2 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Randomly Sorting an Array Without Repeating Elements

Posted 19 December 2010 - 04:24 PM

Create a new thread and post your question on there. Please don't bring up threads that are nearly 2 years old.
Was This Post Helpful? -1
  • +
  • -

#3 BioElectro   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 19-December 10

Re: Randomly Sorting an Array Without Repeating Elements

Posted 19 December 2010 - 04:32 PM

View Postm-e-g-a-z, on 19 December 2010 - 03:24 PM, said:

Create a new thread and post your question on there. Please don't bring up threads that are nearly 2 years old.


Umm, no. The thread is short and has important context. Is there anyone else who would actually like to be helpful who would like to respond to this? Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Randomly Sorting an Array Without Repeating Elements

Posted 19 December 2010 - 04:55 PM

So, the array order is a list of indexes. One index is chosen at random and this line stores the index in pos:

int pos = order[index]


To ensure the same index isn't chosen again, we need to delete it from the array... except in Java you can't delete from arrays. What baavgai did was to overwrite the chosen value with the last value in the array, and at the same time decrement the variable storing the length of the array.


order[index] = order[--orderLen];


Does this help?

This post has been edited by cfoley: 19 December 2010 - 04:56 PM

Was This Post Helpful? 1
  • +
  • -

#5 BioElectro   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 19-December 10

Re: Randomly Sorting an Array Without Repeating Elements

Posted 19 December 2010 - 05:03 PM

View Postcfoley, on 19 December 2010 - 03:55 PM, said:

So, the array order is a list of indexes. One index is chosen at random and this line stores the index in pos:

int pos = order[index]


To ensure the same index isn't chosen again, we need to delete it from the array... except in Java you can't delete from arrays. What baavgai did was to overwrite the chosen value with the last value in the array, and at the same time decrement the variable storing the length of the array.


order[index] = order[--orderLen];


Does this help?


Yes, that is awesome, thanks!
Was This Post Helpful? 0
  • +
  • -

#6 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Randomly Sorting an Array Without Repeating Elements

Posted 19 December 2010 - 05:37 PM

No problem. :)
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Randomly Sorting an Array Without Repeating Elements

Posted 19 December 2010 - 06:03 PM

Topic split. Please avoid necroposting. You are more than welcome to copy the code into a new thread. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1