"Algorithmic thinking" loops through an array

Source: Internet
Author: User

question: How do I move an array to the left or shift the K-bit to the right?     In the following solution, let's take the loop left shift as an example . The easiest thing to think about is to copy the first k elements into a temporary array, then move the remaining n-k elements to the left by the K-position, and then copy the previous K-elements to the remaining positions. This method uses a K extra storage space. We think of another way of using only one temporary space, moving only 1 bits to the left at a time, looping k times. This method produces more than the elapsed time. in the previous article, the program implemented the algorithm of looping right-moving an array. The above mentioned is a more conventional algorithm, the following from other angles to consider the problem:     the loop array x is actually a sub-array of the swap array x, B gets B, a. For example, suppose the array x is int x[]={1,2,3,4,5,6,7,8,9,10}; int a[]={1,2,3,4,5}; int b[]={6,7,8,9,10}; then move the x loop to the left 5 bits is x1={6,7,8,9,10,1,2,3,4,5}; it is obvious that the array, a, B, is exchanged and then stitched together. Of course, here is a special case, is to facilitate understanding. The following is an analysis of the general situation:Suppose the array x is int x[]={1,2,3,4,5,6,7,8,9,10}; int a[]={1,2,3,}; int b[]={4,5,6,7,8,9,10}; then shift the X loop to the left 3 bits is x1={ 4,5,6,7,8,9,10,1,2,3}; So moving K-bits, we assume that the size of a is k-bit, and the rest is B. If you exchange directly, it is the same as the first method mentioned above. Here is a new method of switching : If the element of a is less than B, then B is divided into two array bl,br, where BR is the same length, BL is the left part of B, and BR is the right part of B. In the example above, bl={4,5,6,7},br={8,9,10}. the array x is made up of a,bl,br stitching. After moving the K-bit left, the result array X1 is composed of bl,br,a stitching. So there needs to be a two-step exchange, where a and BR are exchanged, and then the BR is exchanged with BL, which becomes the br,bl,a.Bl,br,a.     The top priority of the problem is exchange. We can also exchange this: we define a={1,2,3} as the inverse of a ' {3,2,1}. Then the AB becomes BA and can be obtained by (a ' B ') ' . So we can write a sub-function of negation, which is used to find the inverse of a particular part of an array. First to a negation, then to the B negation, and finally to the overall negation, you can get results. You need to call the Negation function 3 times.     The advantage of the method mentioned in this article is that it is not necessary to calculate the position that should be reached after moving K-bit. The method mentioned in this article is not the best method, just shows that we can do so, meet a problem, we can find a variety of ways to solve. This article mainly introduces a kind of idea, the procedure is relatively simple, does not realize concretely.

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.