hi to everyone. I need to reverse an array. I made this simple algorithm, and i want to know what would you change of it, and also want to know an algorithm that you recommend for doing this task.

Code:
```void sort()
{
int array[5];                                          // array to reverse
int lowIndex, highIndex;                      // lower and higher indexes in the array
int lowElement, highElement;              // lower and higher elements in the array

array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 13;
array[4] = 5;

lowIndex = 0;
highIndex = 4;                                  // This is array's length at first

while ( highIndex >= lowIndex )
{
// storage lower and higher elements
lowElement = array[lowIndex];
highElement = array[highIndex];

// change their index
array[lowIndex] = highElement;
array[highIndex] = lowElement;

// counters
lowIndex += 1;
highIndex -= 1;
}

int i;

for ( i = 0 ; i < 5 ; i++ )
{
printf ( "%i\n" , array[i] );
}
}```

2. This isn't sorting anything, just reversing.

Technically you only need one extra piece of storage to swap two numbers, but (a) 4 bytes isn't going to hurt anybody these days and (b) your compiler might be able to optimize it away anyway.

You don't need to swap in the case that lowIndex == highIndex, so you can change that while condition.

In terms of reversing in place, I don't think you'll be able to do much better than this.

3. You're right, i might change post title. Sorry about that, i thought that reversing an array was a part of sorting it, but actually what i'm sorting here is array's indexes not array elements.
Thanks about the while condition and that unnecesary four bytes. I've changed it.

Code:
```void sort()
{
int array[5];
int lowIndex, highIndex;
int lowElement;
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 13;
array[4] = 5;

lowIndex = 0;
highIndex = 4;

while ( highIndex > lowIndex )
{
lowElement = array[lowIndex];
array[lowIndex] = array[highIndex];
array[highIndex] = lowElement;
lowIndex += 1;
highIndex -= 1;
}

int i;

for ( i = 0 ; i < 5 ; i++ )
{
printf ( "&#37;i\n" , array[i] );
}

}```

4. Sorry about that, i thought that reversing an array was a part of sorting it, but actually what i'm sorting here is array's indexes not array elements.
So what are you actually trying to do?

5. Reversing it.

6. Reversing it.
Okay, so far your code looks fine, though changing the while loop to a for loop might make it clearer, and I believe that in C you should declare your variables at the start of the block, so int i; is out of place.

I suggest that you change:
Code:
`void sort()`
to:
Code:
`void reverse(int array[], size_t size)`
then rewrite the code to be more general in terms of the size of the input array.

Code:
```#include <stdio.h>

void sort()
{
int array[5];                             // array to reverse
int loopsNeeded ;                              // lower and higher indexes in the array
int tempElement ;
int maxIndex , i ;

array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 13;
array[4] = 5;

maxIndex = 4 ;
loopsNeeded = (5+1)/2 ;

for (i = 0 ; i < loopsNeeded ; ++i)
{
// Swap ends
tempElement = array[i] ;
array[i] = array[maxIndex-i] ;
array[maxIndex-i] = tempElement ;
}

for ( i = 0 ; i <= maxIndex ; i++ )
{
printf ( "&#37;i\n" , array[i] );
}
}

int main(void) {
sort() ;
return 0 ;
}```

8. Don't add 1 when calculating loopsNeeded. The middle item can stay untouched
Code:
`	loopsNeeded = _countof(array)/2 ;`
The iterator approach:
Code:
```int *begin = &array[0];
int *end = &array[_countof(array)];
while (begin < --end)
{
int tempElement = *end;
*end = *begin;
*begin++ = tempElement;
}```

9. Originally Posted by iMalc
Don't add 1 when calculating loopsNeeded. The middle item can stay untouched
You are right. I was thinking 1st, 2nd, 3rd, and should have been thinking 0th, 1st, 2nd. My intent was to ignore the middle element in an odd-sized array.

10. thanks!, i knew if a made a post here you would reply me with this kind of answers. I really appreciate it. Thanks again.