Shuffle 2 n integers in format { a1 , b1 , a2 , b2 , a3 , b3 , ... ... , an , bn } without using extra space | C ++ Naive program to shuffle an array of size 2 n ; function to shuffle an array of size 2 n ; Rotate the element to the left ; swap a [ j - 1 ] , a [ j ] ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void shuffleArray ( int a [ ] , int n ) { for ( int i = 0 , q = 1 , k = n ; i < n ; i ++ , k ++ , q ++ ) for ( int j = k ; j > i + q ; j -- ) swap ( a [ j - 1 ] , a [ j ] ) ; } int main ( ) { int a [ ] = { 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; shuffleArray ( a , n / 2 ) ; for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] << " ▁ " ; return 0 ; }