Changes between Initial Version and Version 1 of Updates/Rotate Vector


Ignore:
Timestamp:
Jun 6, 2008, 12:13:58 AM (16 years ago)
Author:
Cory McWilliams
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Updates/Rotate Vector

    v1 v1  
     1= Remembering How to Rotate a Vector =
     2For no reason that I can remember, I was recently reminded of a solution to a homework problem way back from my first semester in college that I thought was creative.  Having not had anything to say here for four months, I thought it would be fun to dig up the code and take a look.
     3
     4The challenge was to rotate a vector in place using constant extra memory and less than O(n^2^) moves.  There was very little additional information.  I remember scratching my head for a while and then coming up with this crazy scheme involving the greatest common divisor which I drew out in great detail with boxes and arrows explaining how everything would move.  I was disgusted that I couldn't arrive at a way to remove the gcd function entirely.
     5
     6Now that I look, I see it is an early problem in book ''Programming Pearls.''  Their solutions can be found [http://cm.bell-labs.com/cm/cs/pearls/rotate.c here].  Algorithm 3 is the closest to mine, and they indeed don't need to calculate the gcd ahead of time.
     7
     8Oh, and I was sloppy with whitespace and some other details back then.
     9
     10{{{
     11#!cpp
     12/* Euclid's algorithm for calculating the greatest common divisor of two numbers.
     13        Taken from page 58 of "Data Structures & Algorithm Analysis in C++ Second Edition" by Mark Allen Weiss. */
     14int gcd(int a, int b)
     15{
     16        while (b)
     17        {
     18                int r = a % b;
     19                a = b;
     20                b = r;
     21        }
     22
     23        return a;
     24}
     25
     26/* Rotates a vector i steps, using minimal moves.
     27        This requires an "extra" calculation of the greatest common divisor of i and v.size(), but this algorithm
     28        still wins out over rotating v one element at a time i times, because this is O(n) while the latter method is O(n^2) */
     29template<class T> void rotate(vector<T> &v, int i)
     30{
     31        int size = v.size();
     32        int factor = gcd(size, i);
     33        i %= size;
     34
     35        int p;
     36
     37        for (p = 0; p < factor; p++)
     38        {
     39                int n;
     40                T temp = v[p];  // Pull out one element so we can shift everything forward.
     41                for (n = (p + i) % size; n != p; n = (n + i) % size)    // Until we get back to where we start, shift each element forward.
     42                        v[(n - i + size) % size] = v[n];
     43                       
     44                v[(p - i + size) % size] = temp;        // Put back original element.
     45        }
     46}
     47}}}
     48
     49[[AddComment]]