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] );
}
}