Ho scritto questo semplice programma che dovrebbe dare l'ordinamenot con l'algoritmo bubble sort.
Non ricevo errori di compilazione, il problema sta nel fatto che non riordina gli elementi nel vettore e non riesco a trovare l'errrore.
Potete aiutarmi? grazie
Codice PHP:
void bubblesort(int nelem, int array[]);
int main()
{ int nelem = 10;
int i = 0;
srand(time(NULL));
int array[nelem];
int m = 0;
for(i; i< nelem; i++){
array[i]= rand() %100;
printf(" %d", array[i]);
}
printf("\n");
bubblesort(nelem,array);
for( m; m<nelem; m++){
printf(" %d",array[m]);
}
system("PAUSE");
return 0;
}
void bubblesort(int nelem, int array[])
{
int i;
int j = 0;
int temp;
for(i =1 ; i<nelem; i++){
for(j; j < nelem -i; j++){
if( array[j] > array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}