<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dev Corner &#187; java recipe</title>
	<atom:link href="http://devcorner.georgievi.net/tag/java-recipe/feed" rel="self" type="application/rss+xml" />
	<link>http://devcorner.georgievi.net</link>
	<description>Software Developer's Notepad</description>
	<lastBuildDate>Fri, 12 Aug 2011 20:03:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Shuffle 2D Array Java Algorithm</title>
		<link>http://devcorner.georgievi.net/pages/programming/java/shuffle-2d-array-java-algorithm</link>
		<comments>http://devcorner.georgievi.net/pages/programming/java/shuffle-2d-array-java-algorithm#comments</comments>
		<pubDate>Tue, 31 Mar 2009 11:01:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java array]]></category>
		<category><![CDATA[java recipe]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=7</guid>
		<description><![CDATA[Problem
You are given a Java array. You need Java Code to shuffle array elements in random order.
Solution
To shuffle array elements, traverse the array and swap the current array element with another element, selected randomly.

/**
 * Shuffle array Java class
 *
 * @author: Ivan Georgiev
 */
public class ArrayShuffler
&#123;
	public static void shuffle&#40;int&#91;&#93; targetArray&#41;
	&#123;
		int targetIndex;
		int swapBuffer;
		int maxInd = [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong></p>
<p>You are given a Java array. You need Java Code to shuffle array elements in random order.<span id="more-7"></span></p>
<p><strong>Solution</strong></p>
<p>To shuffle array elements, traverse the array and swap the current array element with another element, selected randomly.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Shuffle array Java class
 *
 * @author: Ivan Georgiev
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ArrayShuffler
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> shuffle<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> targetArray<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> targetIndex<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> swapBuffer<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> maxInd <span style="color: #339933;">=</span> targetArray.<span style="color: #006633;">length</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> curIndex<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> curIndex <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> targetArray.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> curIndex<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			targetIndex <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">round</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> maxInd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			swapBuffer <span style="color: #339933;">=</span> targetArray<span style="color: #009900;">&#91;</span>curIndex<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			targetArray<span style="color: #009900;">&#91;</span>curIndex<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> targetArray<span style="color: #009900;">&#91;</span>targetIndex<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			targetArray<span style="color: #009900;">&#91;</span>targetIndex<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> swapBuffer<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Discussion</strong></p>
<p>The array is traversed position-by-position. For each position we swap the element into that position with randomly selected element from the array. The random selection is done using the <code>Math.random</code> method. By traversing all the array elements we ensure that it is &#8220;well&#8221; shuffled.</p>
<p>Usually we shuffle an array to produce a random selection of array elements. If the number of elements in an array is big the time necessary for shuffling the array would be significant. It would also be impossible or at least undesirable to load all the elements into memory. For that purpose it would be better to generate a list of array indexes to represent array elements.</p>
<p><strong>Sample client code</strong></p>
<p>Here is a simple Java application to demonstrate how the random array shuffler works.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ArrayShufflerTest
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Define and initialize array of integers 0, 1, ..., 99</span>
		<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>arr.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Display initial array</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Initial array: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		printArray<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Invoke the shuffler method to shuffle the array.</span>
		ArrayShuffler.<span style="color: #006633;">shuffle</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Display shuffled array</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Shuffled array: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		printArray<span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> printArray<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arr<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> el<span style="color: #339933;">:</span> arr<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>el <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/programming/java/shuffle-2d-array-java-algorithm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

