[Algorithmic problem] swap element values for two sub arrays

Source: Internet
Author: User

Problem Description:

Set A[0:n-1] is an array of n elements, K (0<=k<=n-1) is a nonnegative integer. Try to design an algorithm to put the sub array

A[0:K] and a[k+1:n-1] transposition. The algorithm requires an O (n) in the worst case and uses only O (1) of the auxiliary space.

This problem is more common, the general approach is to separate the two sub arrays in reverse order, and then the entire array in reverse order. That is to say, an array

A[8] = {1,2,3,4,5}, if k = 2, then the two sub arrays are first ordered in reverse order {3, 2,1,5,4}, and then the entire array is ordered in reverse sequence to get {4,5,1,2,3}.

Here is another approach, using recursive methods, in conjunction with the previous example of the thought, in the example of the number of elements earlier than the number of elements (preceded by 3, followed by two), then the number of sub arrays containing fewer elements for the standard exchange, get the sequence: {4,5,3,2,1 More than 3 of the middle is not involved in this exchange, at this time, the sub array {4,5} with fewer elements has been swapped to the last appropriate location, and the subsequent operations may not have to be considered. The operation of the remaining sub arrays obviously has similar behavior to the original problem, and we have to do is Exchange sequence {3} And the location of the sequence {2,1}, so this is a problem that can be solved recursively.

It should be said that the two algorithms are different. The previous approach at worst, each element is moved two times to reach the final fit, but the second option is to move the element to the right place when the number of elements in the two array is the same.

Of course, this method is not as "smart" as the previous one, but it is not easy to write correctly, but it can be used as a partition or a recursive method to solve the problem of an example.

#include <stdio.h>

void Displayarray (int *parray, int nlen)
{
for (int i = 0; i < Nlen; ++i)
{
printf ("array[%d] =%d\n", I, parray[i]);
}
}

void Swapsubarray (int *parray1, int *parray2, int n)
{
int temp;
for (int i = 0; i < n; ++i)
{
temp = * (pArray1 + i);
* (PArray1 + i) = * (PArray2 + i);
* (PArray2 + i) = temp;
}
}

void Changearray (int *parray, int nlen, int k)
{
if (NULL = = Parray)
Return
if (k < 0 | | | k > nLen-1)
Return

int m, n, num;
if (Nlen-k-1 > k + 1)//If the latter part of the element is more
{
m = nlen-k-1-k-1; M is the difference between the number of elements in two parts
Swapsubarray (Parray, Parray + k + 1 + m, K + 1); Swap parts with the same number of elements
Changearray (Parray, Nlen-k-1, k);
}
else if (Nlen-k-1 < K + 1)//If the first half of the element is more
{
m = k + 1-nlen + k + 1;
Swapsubarray (Parray, Parray + k + 1, K + 1-m); Swap parts with the same number of elements
Changearray (Parray + nlen-k-1, k + 1, m-1);
}
else//before and after two parts of the same number of elements
{
Swapsubarray (Parray, Parray + k + 1, K + 1);
}
}

int main ()
{
int array[] = {0, 1, 2, 3, 4, 5, 6, 7};
int nlen = sizeof (array)/sizeof (array[0));
Displayarray (array, nlen);

Changearray (Array, nlen, 1);
printf ("After change:\n");
Displayarray (array, nlen);

return 1;
}

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.