Perfect Shuffle algorithm
Topic Details: There is an array of length 2n {a1,a2,a3,..., an,b1,b2,b3,..., bn}, if you wish to sort {a1,b1,a2,b2,...., an,bn}, consider a solution with no time complexity O (n), spatial complexity 0 (1). http://blog.csdn.net/starcuan/article/details/19079913
#include <iostream> #include <algorithm> #include <functional> using namespace std; /* Perfect Shuffle algorithm topic details: There is an array of length 2n {a1,a2,a3,..., an,b1,b2,b3,..., bn}, you want to sort {a1,b1,a2,b2,...., an,bn}, consider there is no time complexity O (n), The solution of Space complexity 0 (1). */#define N 1000//Time O (n), the space O (n) array subscript starting from 1//any of the element I, and finally to (2 * i)% (2 * N + 1) position void pefect_shuffle1 (int *a, int n) {int n2 = n * 2, I, b[n]; for (i = 1; I <= n2; ++i) {b[(i * 2)% (N2 + 1)] = A[i]; } for (i = 1; I <= n2; ++i) {a[i] = B[i]; }}//Time O (nlogn) space O (1) array subscript starting from 1 void perfect_shuffle2 (int *a,int n) {int t,i; if (n = = 1) {t = a[1]; A[1] = a[2]; A[2] = t; Return } int n2 = n * 2, n3 = N/2; if (n% 2 = = 1) {//odd processing t = A[n]; for (i = n + 1; I <= n2; ++i) {a[i-1] = A[i]; } A[N2] = t; --n; }//To this n is an even for (i = n3 + 1; I <= n; ++i) {t = A[i]; A[i] = a[i + n3]; A[i + n3] = t; }//[1.. n/2] Perfect_shuffle2 (A, N3); Perfect_shuffle2 (A + N, N3); }/* First lap: 1, 2, 4, 8, 7, 5, 1--and 3, 6, 3: Original array: 1 2 3 4 5 6 7 8 Array of small Labels: 1 2 3 4 5 6 7 8 go first lap: 5 1 (3) 2 7 (6) 8 4 Go second lap: 5 1 6 2 7 3 8 4 */* Divine Conclusion: If 2*n= (3^k-1), the number of rings can be determined and their respective The starting position of the head for 2*n = (3^k-1) This length of the array, just a K-ring, and each lap head starting position is 1,3,9, ... 3^ (k-1). *//array subscript starting from 1, the From is the head of the Circle, MoD is to modulo the number mod should be 2 * n + 1, time complexity O (circle length) void Cycle_leader (int *a,int from, int mod) {int last = A[from],t,i; for (i = from * 2 mod;i! = from; i = i * 2 mod) {t = A[i]; A[i] = last; last = t; } A[from] = last; }//Flip string time complexity O (to-from) void reverse (int *a,intFrom,int to) {int t; for (; from < to; ++from,--to) {t = A[from]; A[from] = a[to]; A[to] = t; }}//Loop right shift num bit time complexity O (n) void right_rotate (int *a,int num,int N) {reverse (A, 1, n-num); Reverse (A, n-num + 1,n); Reverse (A, 1, n); }/* Perfect shuffle algorithm, the algorithm flow is: input array a[1..2 * N] Step 1 found 2 * m = 3^k-1 make 3^k <= 2 * N < 3^ (k + 1) Step 2 a[m + 1..N + m] that part of the loop moves the M-bit step 3 to each i = 0,1,2..k-1,3^i is the head of the ring, do the cycle_leader algorithm, the array length is M, so to 2 * m + 1 modulo. Step 4 The back part of the array a[2 * m + 1:2 * n] continues to use this algorithm, which is equivalent to n reduced m. *//Time O (n), Space O (1) void perfect_shuffle3 (int *a,int n) {int n2, M, I, k,t; for (; n > 1;) {//step 1 n2 = n * 2; for (k = 0, M = 1; n2/m >= 3; ++k, M *= 3); M/= 2; 2m = 3^k-1, 3^k <= 2n < 3^ (k + 1)//Step 2 right_rotate (A + M, m, N); // Step 3 for (i = 0, t = 1; i < K; ++i, T *= 3) {Cycle_leader (A, T, M * 2 + 1); }//step 4 A + = M * 2; N-= m; }//n = 1 t = a[1]; A[1] = a[2]; A[2] = t; } int main () {int arr[]={0,1,2,3,4,5,6,7,8}; Perfect_shuffle3 (arr,4); For_each (arr,arr+9,[] (int i) {cout<<i<<endl;}); return 0; }
Perfect Shuffle algorithm