L
lovecreatesbea...
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 > a[j]){
n = a;
a = 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 > a[i + 1]){
n = a;
a = a[i + 1];
a[i + 1] = n;
}
}
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 > a[j]){
n = a;
a = 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 > a[i + 1]){
n = a;
a = a[i + 1];
a[i + 1] = n;
}
}