Fastest way to move element on dynamic array
Hello everyone
I have:
1 2
|
int *p = new int [100], a;
for( a = 0; a < 100; a++ ) p[a] = a + 1;
|
now i want to move p[99] value:100 to p[0] so it will be:
p[0] = 100;
p[1] = 1;
p[2] = 2;
...
I would do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void move( int * data, int what, int to )
{
int a; int temp;
if( what > to )
{
temp = data[what];
for( a = what; a > to; a-- ) data[a] = data[a-1];
data[to] = temp;
return;
}
temp = data[what];
for( a = what; a < to; a++ ) data[a] = data[a+1];
data[to] = temp;
}
int *p = new int [100], a;
for( a = 0; a < 100; a++ ) p[a] = a + 1;
move(p, 99, 0 );
|
Is there faster way to do it?
Feel free to go more advanced than new operator
thank you!
Last edited on
Topic archived. No new replies allowed.