Take bubble sort for example. This is what the lecturer gave us in the slides:
int tmp,i,j;
for (i=0; i<n-1; i++)
{
for (j=0; j<n-i-1; j++)
{
if (data[j] > data[j+1])
{
tmp = data[j];
data[j] = data[j+1];
data[j+1] = tmp;
}
}
}
And this is what I find so much easier to understand:
for (int i=1; i<=db.length-1; i++)
{
for (int j=0; j<db.length-1; j++)
{
if (db[j] > db[j+1])
{
temp = db[j+1];
db[j+1] = db[j];
db[j] = temp;
}
}
}
The only actual difference appears in the boundaries of the "for-loops" so they must be both different implementations of bubble sort, right?
(My concern is general and not only limited to bubble sort, just used it as a way to explain what I mean, hope I didn't confuse you! :))


Sign In
Create Account

Back to top









