Perfect shuffling & shuffling

Source: Internet
Author: User

Perfect shuffling problem, given an array A1, A2, A3 ,... an, B1, B2, b3 .. BN, set it to B1, A1, B2, A2 ,... BN,.

O (n) algorithm, space of O (n.

For the first n numbers, the ing is f (I) = 2 * I + 1, 0 <= I <n/2; for example, 0-> 1, 1-> 3

For the next n numbers, map to f (I) = 2 (I-n/2), n/2 <= I <n; for example, n/2-> 0, n/2 + 1-> 2... and f (I) = 2 (I-n/2) = 2 * I-n = 2 * I + 1-(n + 1) = (2 * I + 1) % (n + 1 ).

Unified, ing to f (I) = (2 * I + 1) % (n + 1 ).

 1 void perfectShuffle1(int arr[], int n) { 2     int* tmp = new int[n]; 3     for (int i = 0; i < n; ++i) { 4         tmp[((i << 1) + 1) % (n + 1)] = arr[i]; 5     } 6     for (int i = 0; i < n; ++i) { 7         arr[i] = tmp[i]; 8     } 9     delete[] tmp;10 }

Division and control, O (nlgn) algorithm, and O (lgn) space.

Line 4-11 mainly deals with the odd half of the array.

When N/4! When the value is 0, the half of the array is an odd number. In this case, move the number of [n/2, n) to the left and put the number n/2-1 (the last number in the first half) to the end, the last two numbers are arranged. The problem is converted into an even half of the array.

When half of the array is an even number, you only need to swap the First Half of the array and the first half of the array to achieve the purpose of grouping.

 1 void perfectShuffle2(int arr[], int n) { 2     if (n % 2 != 0) return; 3     if (n <= 1) return; 4     if (n % 4 != 0) { 5         int tmp = arr[n / 2 - 1]; 6         for (int i = n / 2; i < n; ++i) { 7             arr[i - 1] = arr[i]; 8         } 9         arr[n - 1] = tmp;10         n -= 2;11     }12     for (int i = 0; i < n / 4; ++i) {13         swap(arr[n / 4 + i], arr[n / 2 + i]);14     }15     perfectShuffle2(arr, n / 2);16     perfectShuffle2(arr + n / 2, n / 2);17 }

Main reference from: http://blog.csdn.net/caopengcs/article/details/10176093, which mentioned the third solution did not go to see, I feel that the interview will not take the exam first effort to understand.

Shuffling is simple. In fact, it is equivalent to randomly selecting M numbers from the array. See the previous blog post. But here M = n.

1 void shuffle(int arr[], int n) {2     srand(time(NULL));3     for (int i = 0; i < n; ++i) {4         swap(arr[i], arr[i + rand() % (n - i)]);5     }6 }

This is the same principle as the fisheryates shuffling algorithm mentioned on the Internet.

Perfect shuffling & shuffling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.