Selection sort and bubble sort
Selection sort and bubble sort have same performance always, right?
Are the following correctly implemented the both functions. Comments
are welcome.
Selection sort an array of intergers range between index l and r in
ascending order. For example, sort "312" into "123"
void sort_sel(int a, int l, int r)
{
int i, j, n;
for (i = l; i < r; i++)
for (j = i + 1; j <= r; j++)
if (a[i] > a[j]){
n = a[i];
a[i] = a[j];
a[j] = n;
}
}
Bubble sort an array of intergers range between index l and r in
ascending order. For example, sort "312" into "123"
void sort_bub(int a, int l, int r)
{
int i, n;
for (; l < r; r--)
for (i = l; i < r; i++)
if (a[i] > a[i + 1]){
n = a[i];
a[i] = a[i + 1];
a[i + 1] = n;
}
}
|