
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Shuffle Array Contents
This algorithm will take an array and shuffle the contents of the array. It will generate a random permutation of the array elements.
To solve this problem, we will swap elements starting from the last index to randomly generated an index in the array.
Input and Output
Input: An array of integers: {1, 2, 3, 4, 5, 6, 7, 8} Output: Shuffle of array contents: 3 4 7 2 6 1 5 8 (Output may differ for next run)
Algorithm
randomArr(array, n)
Input: The array, number of elements.
Output: Shuffle the contents of the array.
Begin for i := n – 1 down to 1, do j := random number from 0 to i swap arr[i] and arr[j] done End
Example
#include <iostream> #include<cstdlib> #include <ctime> using namespace std; void display(int array[], int n) { for (int i = 0; i < n; i++) cout << array[i] << " "; } void randomArr ( int arr[], int n ) { //generate random array element srand (time(NULL)); //use time to get different seed value to start for (int i = n-1; i > 0; i--) { int j = rand() % (i+1); //randomly choose an index from 0 to i swap(arr[i], arr[j]); //swap current element with element placed in jth location } } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; int n = 8; randomArr(arr, n); display(arr, n); }
Output
4 7 8 2 6 3 5 1
- Related Articles
- Shuffle an Array in Python
- How to randomize (shuffle) a JavaScript array?
- How to shuffle an array in Java?
- Shuffle an Array using STL in C++
- Java Program to shuffle an array using list
- how to shuffle a 2D array in java correctly?
- Checking the intensity of shuffle of an array - JavaScript
- Golang program to shuffle the elements of an array
- Swift Program to Shuffle the Elements of an Array
- How to randomize and shuffle array of numbers in Java?
- How to shuffle an array in a random manner in JavaScript?
- How to print the contents of array horizontally using C#?
- shuffle() function in PHP
- Alternative shuffle in JavaScript
- How to sort by the difference in array contents in MongoDB?

Advertisements