-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathShuffleArray.cpp
50 lines (31 loc) · 989 Bytes
/
ShuffleArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
/*Program to shuffle an array of size 2n, into format a1 b1 a2 b2 a3 b3 a4 b4 etc without using extra memory.
This problem is similar to the queue interleaving problem- eg 1 2 3 4 5 6 7 8 = 1 5 2 6 3 7 4 8.
1)Using divide and conquer
2) using extra space -using queue interleaving solution
*/
void ShuffleArray(int *arr,int l , int h)
{
//finding mid index
int mid = l + (h-l)/2; //mid index
int prev= l +(mid-l)/2;//
//base case
if(l==h) return;
cout<<mid<<" "<<prev<<endl;
for(int k = 1 , i = prev + 1 ; i <= mid ; i++,k++)
{
swap(arr[i],arr[mid+k]);
}
ShuffleArray(arr,l,mid);//recursively call the function on lower half
ShuffleArray(arr,mid+1,h);//recursively call the function on right half
}
//T(n) = O(nlogn), space = O(n) for recursion call stack
int main()
{
int arr[] = {1,2,3,4,5,6,7,8};
int size = (sizeof(arr)/sizeof(arr[0]));
ShuffleUsingQueue(arr);
}