# Shuffle the array elements

Shuffling is a procedure used to randomize an array element. Similar way suppose we are given an array which contains n elements. Our goal is to randomize the elements of this array in efficient manner. Lets see few examples.

``````Example 1
arr = [1 , 0 , -3 , 8 , 7 , 3 , 9 , 4 , 2 , 5 , 10 , 6]
// Shuffle array
Output
[6,  -3,  5,  9,  2,  7,  0,  4,  3,  8,  1,  10]
[10,  7,  1,  2,  9,  -3,  3,  5,  0,  6,  8,  4]
[4,  2,  5,  6,  1,  -3,  7,  3,  0,  10,  8,  9]
etc
-------------------------

Example 2
arr = ['a',  'b',  'c',  'x',  'y',  'z']
output
['z',  'y',  'b',  'x',  'a',  'c']
['z',  'b',  'x',  'y',  'a',  'c']
['x',  'b',  'a',  'c',  'y',  'z']
etc``````

Result can be any order in given array and result are modified (reflect) to actual array. There can be many ways to do this work, but the effective way is to swap elements and change the order. In the example below, the approach of finding a random index and swapping its element is implementing.

``````// C Program
// Shuffle the array elements
#include <stdio.h>

#include <time.h>

#include <stdlib.h>

//Function which is swapping two array elements of given location
void swap_element(int arr[], int i, int j)
{
//Get i location element
int temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", arr[i]);
}
printf("\n");
}
//returns the random location of array elements
int random_location(int first, int last)
{
//Calculate random number between given range
return rand() % ((last + 1) - first) + first;
}
//Function which is Shuffle given array elements
void shuffle_element(int arr[], int size)
{
//[i[ and [j] indicate locations
int j = 0;
int i = 0;
//Variable which is controlling the execution process of loop
int counter = 0;
//Loop which is shuffling random elements in array
while (counter < size)
{
// Get random location of array index
i = random_location(0, size - 1);
j = random_location(0, size - 1);
if (i != j)
{
//swap array elements
swap_element(arr, i, j);
counter++;
}
}
printf("\n");
}
int main()
{
//impotant sets the seed based on the current time
srand(time(NULL));
//Define array elements
int arr[] = {
1,
0,
-3,
8,
7,
3,
9,
4,
2,
5,
10,
6
};
//Get the size
int size = sizeof(arr) / sizeof(arr[0]);
//Before shuffling  array elements
printf("Initial array elements\n");
display(arr, size);
printf("\nAfter Shuffle array elements\n");
shuffle_element(arr, size);
display(arr, size);
shuffle_element(arr, size);
display(arr, size);
shuffle_element(arr, size);
display(arr, size);
return 0;
}``````

#### Output

``````Initial array elements
1 0 -3 8 7 3 9 4 2 5 10 6

After Shuffle array elements

1 9 7 3 6 5 10 0 -3 2 8 4

10 -3 9 3 1 5 0 7 2 6 4 8

-3 3 9 1 10 6 8 4 2 5 7 0``````
``````/*
Java Program
Shuffle the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
public void swap_element(int[] arr, int i, int j)
{
//Get i location element
int temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i] + " ");
}
System.out.print("\n");
}
//returns the random location of array elements
public int random_location(int min, int max)
{
//Calculate random number between given range
return min + (int)(Math.random() * ((max - min) + 1));
}
//Function which is Shuffle given array elements
public void shuffle_element(int[] arr, int size)
{
//[i[ and [j] indicate locations
int j = 0;
int i = 0;
//Variable which is controlling the execution process of loop
int counter = 0;
//Loop which is shuffling random elements in array
while (counter < size)
{
// Get random location of array index
i = random_location(0, size - 1);
j = random_location(0, size - 1);
if (i != j)
{
//swap array elements
swap_element(arr, i, j);
counter++;
}
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyArray obj = new MyArray();
//Define array elements
int[] arr = {
1,
0,
-3,
8,
7,
3,
9,
4,
2,
5,
10,
6
};
int size = arr.length;
//Before shuffling  array elements
System.out.print("Initial array elements\n");
obj.display(arr, size);
System.out.print("\nAfter Shuffle array elements\n");
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
}
}``````

#### Output

``````Initial array elements
1   0   -3   8   7   3   9   4   2   5   10   6

After Shuffle array elements

10   7   8   -3   0   4   1   5   2   6   3   9

9   4   6   -3   0   7   5   3   8   10   2   1

9   1   6   8   4   2   10   0   3   5   -3   7``````

The result can be different result because it is based on random shuffling. Let's look at an example in the image below.

``````/*
C++ Program
Shuffle the array elements
*/
#include<iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
class MyArray
{
public:
//Function which is swapping two array elements of given location
void swap_element(int arr[], int i, int j)
{
//Get i location element
int temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i] << " ";
}
cout << "\n";
}
//returns the random location of array elements
int random_location(int min, int max)
{

//Calculate random number between given range
return rand() % ((max + 1) - min) + min;
}
//Function which is Shuffle given array elements
void shuffle_element(int arr[], int size)
{
//[i[ and [j] indicate locations
int j = 0;
int i = 0;
//Variable which is controlling the execution process of loop
int counter = 0;
//Loop which is shuffling random elements in array
while (counter < size)
{
// Get random location of array index
i = this->random_location(0, size - 1);
j = this->random_location(0, size - 1);
if (i != j)
{
//swap array elements
this->swap_element(arr, i, j);
counter++;
}
}
cout << "\n";
}
};
int main()
{
MyArray obj;
int arr[] = {
1,
0,
-3,
8,
7,
3,
9,
4,
2,
5,
10,
6
};
int size = sizeof(arr) / sizeof(arr[0]);
srand(time(NULL));
cout << "Initial array elements\n";
obj.display(arr, size);
cout << "\nAfter Shuffle array elements\n";
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
return 0;
}``````

#### Output

``````Initial array elements
1   0   -3   8   7   3   9   4   2   5   10   6

After Shuffle array elements

-3   7   8   9   2   0   3   4   10   5   1   6

9   -3   5   7   2   3   1   8   0   10   4   6

7   -3   5   6   2   9   1   8   4   10   0   3``````
``````/*
C# Program
Shuffle the array elements
*/
using System;
class MyArray
{
//Function which is swapping two array elements of given location
public void swap_element(int[] arr, int i, int j)
{
//Get i location element
int temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; i++)
{
Console.Write("  " + arr[i] + " ");
}
Console.Write("\n");
}
//returns the random location of array elements
public int random_location(int min, int max)
{
Random rand = new Random();
//Calculate random number
return rand.Next(min, max);;
}
//Function which is Shuffle given array elements
public void shuffle_element(int[] arr, int size)
{
//[i[ and [j] indicate locations
int j = 0;
int i = 0;
//Variable which is controlling the execution process of loop
int counter = 0;
//Loop which is shuffling random elements in array
while (counter < size)
{
// Get random location of array index
i = random_location(0, size - 1);
j = random_location(0, size - 1);
if (i != j)
{
//swap array elements
swap_element(arr, i, j);
counter++;
}
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyArray obj = new MyArray();
int[] arr = {
1,
0,
-3,
8,
7,
3,
9,
4,
2,
5,
10,
6
};
int size = arr.Length;
Console.Write("Initial array elements\n");
obj.display(arr, size);
Console.Write("\nAfter Shuffle array elements\n");
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
}
}``````

#### Output

``````Initial array elements
1   0   -3   8   7   3   9   4   2   5   10   6

After Shuffle array elements

-3   5   3   10   4   2   7   8   1   0   9   6

3   0   7   8   9   2   4   5   -3   10   1   6

3   1   7   9   4   10   2   5   -3   0   8   6``````
``````<?php
/*
Php Program
Shuffle the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
function swap_element( & \$arr, \$i, \$j)
{
//Get i location element
\$temp = \$arr[\$i];
//set new values
\$arr[\$i] = \$arr[\$j];
\$arr[\$j] = \$temp;
}
//Function which is display array elements
function display( \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i] ." ";
}
echo "\n";
}
//returns the random location of array elements
function random_location(\$min, \$max)
{
return rand(\$min, \$max);
}
//Function which is Shuffle given array elements
function shuffle_element( & \$arr, \$size)
{
//[i[ and [j] indicate locations
\$j = 0;
\$i = 0;
//Variable which is controlling the execution process of loop
\$counter = 0;
//Loop which is shuffling random elements in array
while (\$counter < \$size)
{
// Get random location of array index
\$i = \$this->random_location(0, \$size - 1);
\$j = \$this->random_location(0, \$size - 1);
if (\$i != \$j)
{
//swap array elements
\$this->swap_element(\$arr, \$i, \$j);
\$counter++;
}
}
echo "\n";
}
}

function main()
{
\$obj = new MyArray();
//Define array elements
\$arr = array(1, 0, -3, 8, 7, 3, 9, 4, 2, 5, 10, 6);
\$size = count(\$arr);
echo "Initial array elements\n";
\$obj->display(\$arr, \$size);
echo "\nAfter Shuffle array elements\n";
\$obj->shuffle_element(\$arr, \$size);
\$obj->display(\$arr, \$size);
\$obj->shuffle_element(\$arr, \$size);
\$obj->display(\$arr, \$size);
\$obj->shuffle_element(\$arr, \$size);
\$obj->display(\$arr, \$size);
}
main();``````

#### Output

``````Initial array elements
1   0   -3   8   7   3   9   4   2   5   10   6

After Shuffle array elements

5   9   8   3   2   10   7   4   1   6   0   -3

10   -3   5   4   9   8   7   3   6   0   2   1

-3   4   6   8   9   5   1   10   0   3   2   7``````
``````/*
Node Js Program
Shuffle the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
swap_element(arr, i, j)
{
//Get i location element
var temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i] + " ");
}
process.stdout.write("\n");
}
//returns the random location of array elements
random_location(min, max)
{
//Calculate random number between given range
return Math.floor(Math.random() * (max - min) + min);
}
//Function which is Shuffle given array elements
shuffle_element(arr, size)
{
//[i[ and [j] indicate locations
var j = 0;
var i = 0;
//Variable which is controlling the execution process of loop
var counter = 0;
//Loop which is shuffling random elements in array
while (counter < size)
{
// Get random location of array index
i = this.random_location(0, size - 1);
j = this.random_location(0, size - 1);
if (i != j)
{
//swap array elements
this.swap_element(arr, i, j);
counter++;
}
}
process.stdout.write("\n");
}
}

function main()
{
var obj = new MyArray();
//Define array elements
var arr = [1, 0, -3, 8, 7, 3, 9, 4, 2, 5, 10, 6];
var size = arr.length;
process.stdout.write("Initial array elements\n");
obj.display(arr, size);
process.stdout.write("\nAfter Shuffle array elements\n");
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
}
main();``````

#### Output

``````Initial array elements
1   0   -3   8   7   3   9   4   2   5   10   6

After Shuffle array elements

8   1   2   10   7   9   -3   5   0   4   3   6

-3   8   4   5   7   10   9   2   1   0   3   6

1   10   -3   3   4   7   9   2   5   8   0   6``````
``````#   Python 3 Program
#   Shuffle the array elements
import random
import sys
class MyArray :
# Function which is swapping two array elements of given location
def swap_element(self, arr, i, j) :
# Get i location element
temp = arr[i]
# set new values
arr[i] = arr[j]
arr[j] = temp

# Function which is display array elements
def display(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i] ," ", end = "")
i += 1

print("\n", end = "")

# returns the random location of array elements
def random_location(self, min, max) :

# Calculate random number between given range
return random.randint(min,max)

# Function which is Shuffle given array elements
def shuffle_element(self, arr, size) :
# [i[ and [j] indicate locations
j = 0
i = 0
# Variable which is controlling the execution process of loop
counter = 0
# Loop which is shuffling random elements in array
while (counter < size) :
#  Get random location of array index
i = self.random_location(0, size - 1)
j = self.random_location(0, size - 1)
if (i != j) :
# swap array elements
self.swap_element(arr, i, j)
counter += 1

print("\n", end = "")

def main() :
obj = MyArray()
# Define array elements
arr = [1, 0, -3, 8, 7, 3, 9, 4, 2, 5, 10, 6]
size = len(arr)
print("Initial array elements\n", end = "")
obj.display(arr, size)
print("\nAfter Shuffle array elements\n", end = "")
obj.shuffle_element(arr, size)
obj.display(arr, size)
obj.shuffle_element(arr, size)
obj.display(arr, size)
obj.shuffle_element(arr, size)
obj.display(arr, size)

if __name__ == "__main__": main()``````

#### Output

``````Initial array elements
1     0     -3     8     7     3     9     4     2     5     10     6

After Shuffle array elements

4     5     9     0     7     6     2     8     10     3     1     -3

1     5     10     -3     8     3     7     9     0     4     2     6

3     1     10     4     7     8     5     2     0     -3     9     6``````
``````#   Ruby Program
#   Shuffle the array elements

class MyArray

# Function which is swapping two array elements of given location
def swap_element(arr, i, j)

# Get i location element
temp = arr[i]
# set new values
arr[i] = arr[j]
arr[j] = temp
end
# Function which is display array elements
def display(arr, size)

i = 0
while (i < size)

print("  ", arr[i] ," ")
i += 1
end
print("\n")
end
# returns the random location of array elements
def random_location(min, max)
r = Random.new

# Calculate random number between given range
return r.rand(min...max)
end
# Function which is Shuffle given array elements
def shuffle_element(arr, size)

# [i[ and [j] indicate locations
j = 0
i = 0
# Variable which is controlling the execution process of loop
counter = 0
# Loop which is shuffling random elements in array
while (counter < size)

#  Get random location of array index
i = self.random_location(0, size )
j = self.random_location(0, size )
if (i != j)

# swap array elements
self.swap_element(arr, i, j)
counter += 1
end
end
print("\n")
end
end
def main()

obj = MyArray.new()
# Define array elements
arr = [1, 0, -3, 8, 7, 3, 9, 4, 2, 5, 10, 6]
size = arr.length
# Before shuffling  array elements
print("Initial array elements\n")
obj.display(arr, size)
print("\nAfter Shuffle array elements\n")
obj.shuffle_element(arr, size)
obj.display(arr, size)
obj.shuffle_element(arr, size)
obj.display(arr, size)
obj.shuffle_element(arr, size)
obj.display(arr, size)
end
main()``````

#### Output

``````Initial array elements
1   0   -3   8   7   3   9   4   2   5   10   6

After Shuffle array elements

9   -3   10   6   7   1   4   0   2   5   8   3

7   5   8   9   -3   6   2   3   1   0   10   4

-3   7   8   2   5   6   4   9   3   0   10   1
``````
``````/*
Scala Program
Shuffle the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
def swap_element(arr: Array[Int], i: Int, j: Int): Unit = {
//Get i location element
var temp: Int = arr(i);
//set new values
arr(i) = arr(j);
arr(j) = temp;
}
//Function which is display array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i) + " ");
i += 1;
}
print("\n");
}
//returns the random location of array elements
def random_location(min: Int, max: Int): Int = {
val r = new scala.util.Random;

//Calculate random number between given range
return min + r.nextInt((max-min)-1) ;
}
//Function which is Shuffle given array elements
def shuffle_element(arr: Array[Int], size: Int): Unit = {
//[i[ and [j] indicate locations
var j: Int = 0;
var i: Int = 0;
//Variable which is controlling the execution process of loop
var counter: Int = 0;
//Loop which is shuffling random elements in array
while (counter < size)
{
// Get random location of array index
i = random_location(0, size - 1);
j = random_location(0, size - 1);
if (i != j)
{
//swap array elements
swap_element(arr, i, j);
counter += 1;
}
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyArray = new MyArray();
//Define array elements
var arr: Array[Int] = Array(1, 0, -3, 8, 7, 3, 9, 4, 2, 5, 10, 6);
var size: Int = arr.length;
//Before shuffling  array elements
print("Initial array elements\n");
obj.display(arr, size);
print("\nAfter Shuffle array elements\n");
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
obj.shuffle_element(arr, size);
obj.display(arr, size);
}
}``````

#### Output

``````Initial array elements
1   0   -3   8   7   3   9   4   2   5   10   6

After Shuffle array elements

3   -3   7   5   2   8   0   1   9   4   10   6

9   1   -3   7   3   0   4   5   8   2   10   6

9   7   0   1   5   3   8   4   -3   2   10   6``````
``````/*
Swift Program
Shuffle the array elements
*/
import Foundation

#if os(Linux)
srandom(UInt32(time(nil)))
#endif
class MyArray
{
//Function which is swapping two array elements of given location
func swap_element(_ arr: inout[Int], _ i: Int, _ j: Int)
{
//Get i location element
let temp: Int = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i] ," ", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
//returns the random location of array elements
func random_location(_ min: Int, _ max: Int) -> Int
{
//Calculate random number between given range
var number: Int = 0;
#if os(Linux)
number = min + Int(random()%(max-min))
#else
number =  Int(min + arc4random_uniform(max - min))
#endif
return number;
}
//Function which is Shuffle given array elements
func shuffle_element(_ arr: inout [Int], _ size: Int)
{
//[i[ and [j] indicate locations
var j: Int = 0;
var i: Int = 0;
//Variable which is controlling the execution process of loop
var counter: Int = 0;
//Loop which is shuffling random elements in array
while (counter < size)
{
// Get random location of array index
i = self.random_location(0, size );
j = self.random_location(0, size );
if (i != j)
{
//swap array elements
self.swap_element(&arr, i, j);
counter += 1;
}
}
print("\n", terminator: "");
}
}
func main()
{
let obj: MyArray = MyArray();
//Define array elements
var arr: [Int] = [1, 0, -3, 8, 7, 3, 9, 4, 2, 5, 10, 6];
let size: Int = arr.count;
print("Initial array elements\n", terminator: "");
obj.display(arr, size);
print("\nAfter Shuffle array elements\n", terminator: "");
obj.shuffle_element(&arr, size);
obj.display(arr, size);
obj.shuffle_element(&arr, size);
obj.display(arr, size);
obj.shuffle_element(&arr, size);
obj.display(arr, size);
}
main();``````

#### Output

``````Initial array elements
1     0     -3     8     7     3     9     4     2     5     10     6

After Shuffle array elements

3     0     9     7     6     1     5     8     2     4     -3     10

3     7     4     9     -3     1     5     0     10     2     8     6

4     7     2     0     8     6     1     9     -3     10     3     5``````

Time complexity analysis in above solution is O(n), n indicates number of array elements. And may be resulting array is similar to given array (But that is very rarely).

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.