I wrote this function to order array elements, is it good?
I’m still a noob in C programming, so I prefer to ask.
bSortArray(int array[], int arrayL)
{
int i,j,temp;
for(i=0;i<arrayL;i++)
{
for(j=0;j<arrayL-1;j++)
{
if(array[i]<array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}
You can have a bubble sort bring small values to the beginning of the array,
or you can have bubble sort bring large values to the end of the array.
The animation on the Wikipedia page shows the 2nd case.
Refer to:
http://en.wikipedia.org/wiki/Bubble_sort
Here is an implementation that will terminate the looping early if it does a pass over i without swapping anything.
http://www.c.happycodings.com/Sorting_Searching/code4.html
Note that the inner loop should compare the value at a given index against its’ neighbor.
i.e.
array[j] > array[j+1].