Hi all.
I wrote this algorithm, which seems to work fine, I but would like to know how it could be improved? I know that (size -1) iterations is required to do one pass(inner loop), and added an outer loop with the same number of iterations for no good reason. No logic to this line of thinking at all.
So I am now thinking that there are excess iterations with this algorithm(once again, not based on logic, just a hunch), meaning that the sorting will completed way before the outer loop has finished. Based on logic, how do I determine exactly what the number of iterations is required for the outer loop to do the job efficiently?
Thanks in advance.
Code:
#include <stdio.h>
int * sort(int array[], int size);
void display(int array[], int size);
int main(void)
{
int array[10] = {4, 12, 8, 0, 32, 2000, 234, 786, 56, 3456};
sort(array, 10);
display(array, 10);
return 0;
}
int * sort(int array[], int size)
{
int ctr, ctr1, temp;
for(ctr = 0; ctr < size - 1; ctr++)
{
for(ctr1 = 0; ctr1 < size - 1; ctr1++)
{
if(array[ctr1] < array[ctr1 + 1])
{
continue;
}
else
{
temp = array[ctr1];
array[ctr1] = array[ctr1 + 1];
array[ctr1 + 1] = temp;
}
}
}
return array;
}
void display(int array[], int size)
{
int ctr;
for(ctr = 0; ctr < size; ctr++)
{
printf("%d \n", array[ctr]);
}
}