Array Loop Movement

Source: Internet
Author: User

Array Loop Movement

Personal information: studied at Yan University undergraduate software engineering major at present;

I blog: Google search "cqs_2012" can be;

Personal Hobbies: Love data structure and algorithms, hope to work in the future algorithm to make their own contribution to the people;

Programming Language: C + +;

Programming Bad Environment: Windows 7 Professional Edition x64;

Programming Tools: VS2008;

Cartographic tools: Office in PowerPoint;

Hardware information: 7g-3 notebook;


Mantra

Go ahead, don't hesitate, hesitate is the best chance for others to surpass themselves.

Topic

The array moves right through.

Solution

I designed the algorithm time complexity of O (n), the spatial complexity of O (1)

The algorithm is as follows: N is an array length, K is the number of moving digits (K great article here)

First consider if k<0, that is to move to the left;

In consideration if k>n, this is to explain that K has moved several times, it is not necessary, if k = = N, that is, after the move, the array has not changed; what do we do then take k = k%n;

My algorithm is divided into two cases.

d = min (k, n-k),

The first case is n = = = 0 (this needs to find an example, try more, and then you can find the pattern)

The second case is n% d! = 0 (This is a relatively simple case)

C + + code representation algorithm

Array loop shift void Array::roat_right_array (int *data,unsigned int const length,unsigned int k) {//Exception input if (data = NULL | | length = = 0) {cout<< "Abnormal input Roat_right_array" <<ENDL;} Normal input else{//If you move to the left, turn to the right while (K < 0) K + = Length;k = k% Length;//If the entire array does not change after the move, then you do not need to move, otherwise you need to move, as follows if (k! = 0) {int min = (k < (LENGTH/2))? k:length-k;//can divide evenly, meaning is not a dragon if (length% min = = 0) {int dlater,dbefore;for (int j=0;j<min;j++) {dlater = data[(j+k)%length ];d before =data [j];for (unsigned int m=0,i=j;m < (length/min); m++) {data[(i+k)%length] = Dbefore;i = (i+k)%length; Dbefore = Dlater;dlater = data[(i+k)%length];}}} Not divisible, meaning is dragon else{int dbefore= data[0],dlater = data[k%length];for (unsigned int i=0,j=0;i<length;i++) {data[(j+k)% Length] = Dbefore;j = (j+k)%length;dbefore = dlater;d later = data[(j+k)%length];}}}}





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.