You don't need to use malloc, at all. This is a static array.
Your sort code is the first thing - it's a Substitution sort, not a Bubble sort. While these two sort algorithms are very close to each other, here's the difference: the Bubble sort ALWAYS compares adjacent values, as it sorts. The Substitution sort STARTS comparing just one adjacent value, and then compares only non-adjacent values, as it proceeds to sort.
Your call to Bubble sort should be:
Code:
for (int i=0;i<n;i++)
{
bubble(n,i,matrix[i]); //i goes first, before matrix[i]
}
The function's first line should be:
Code:
void bubble(int n, int i, int a[i])
If int i, is not listed before a[i], you should receive an error or warning, from your compiler.
This is a correct Substitution sort:
Code:
for (left=0; left<n-1; left++) //Substitution sort
{
for(right=left+1;right<n;right++) //starts with 1 comparison to
if (a[left]>a[right]) //the adjacent value
{
c=a[left]; //then begins comparing non
a[left]=a[right]; //adjacent values
a[right]=c;
}
}
and this is a correct Bubble sort:
Code:
do {
swapped = 0;
for(left =0; left < n-1; left++) { //Bubble sort
if(a[left] < a[left + 1 ]) { //always compares adjacent values
c=a[left];
a[left]=a[left+1];
a[left+1]=c;
swapped = 1;
}
} //end for
--n; //a reasonable optimization
}while(swapped); //for a small quantity sorter, only.
Both of these sorting algorithms are good for small quantities, only. Don't even THINK about using them for a large quantity sort - they fall right on their faces.