Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Tuesday, August 13, 2013

Java program to shuffle the contents of array uniquely without repetition..

Java Code : Shuffling of Arrays uniquely

Shuffling of arrays elements can be done easily using the math.rand() function everyone thinks that...
But when you come to randomly shuffle the array's elements you will encounter problems like the arrays is not perfectly shuffled or it has repeated numbers in it.You can also come under the stituation that it takes more time than estimated due to its complexity.Here the provided java code not only suffles the the array elements uniquely but also reduces the time complexity of the method ..
In this code the swap is mainly used to swap the elements arbitarliy to the desired place and then it it reduces the array size thus coverning the other arrays elements and leaving that used elements at its place untill changes are made to access it.this algorithm is developed by one of the geeks so i tried to show you the way to apply into the java programming language .You can also shuffle any array of objects wgich is a plus point based on it.
/* Program to shuffle the contents of the array elements
 *
 * @author Code Author
 */
import java.util.*;
public class Array_shuffle {

    //method to shuffle the array uniquely
public static void shuffle(int a[])
   {
        int n = a.length;
        Random rand = new Random();  //using random function so as to randomly shuffle the contents of array
      
        for(int i=0;i<n;i++)
        {
            int change = i + rand.nextInt(n - i);
            swap(a,change,i); //calling swap method
            
        }
      
   }
    
//swap method swaps the chaged value with the positional value of i of the array
public static void swap(int[] a,int change,int i)
    {
    int temp = a[i];  //intially stored in temporary variable then swaping
    a[i] = a[change];
    a[change] = temp;
    }
    

//method to display the contents of the array --no need of explaination
public static void display(int[] a)
    {
        for(int na:a)
           System.out.print(na+" ");
    }
    

public static void main(String[] args) {
       Scanner console = new Scanner(System.in);
       int it=1;
       
       //create the array using specified number of the elements
       System.out.print("Enter the number of elements in the array : ");
       int n = console.nextInt();
       int[] a= new int[n];       //array created of size n
       
       
       //now adding the contents to the array
       System.out.println("\nFill the contents of the array : ");
        for(int j=0;j<a.length;j++ )
            a[j] = console.nextInt();
        
         //getting number of times to be shuffled
       System.out.println("Enter number of times array should be shuffled : ");
       int t =console.nextInt();
       
        
        //displaying the current array
        System.out.println("\nCurrent Array : ");
        display(a);
        
        while (t>0)
        {
            //calling the shuffle function to shuffle the array contents 
             shuffle(a);
        
            //Displaying the shuffled array
             System.out.printf("\nShuffled Array %dst Time : ",it);
             display(a);
             it++;
             t--;
        }

    }

}

The output for the shuffled arrays ca be seen here--
Enter the number of elements in the array : 10
Fill the contents of the array : 2 6 8 10 12 1 3 7 20 25
Enter number of times array should be shuffled : 5

Current Array : 2 6 8 10 12 1 3 7 20 25 

Shuffled Array 1st Time : 1 12 7 20 3 2 6 8 25 10 
Shuffled Array 2st Time : 7 25 1 2 3 12 6 20 8 10 
Shuffled Array 3st Time : 2 20 1 25 12 3 8 10 6 7 
Shuffled Array 4st Time : 1 10 7 2 25 3 12 6 8 20 
Shuffled Array 5st Time : 25 12 6 3 10 20 8 7 2 1 

No comments :

Post a Comment

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.