Re: Selection sort and bubble sort
On Oct 17, 9:57 am, pete <pfil...@mindspring.com> wrote:
> Eric Sosman wrote:
>
> > lovecreatesbea...@gmail.com wrote On 10/16/07 15:32,:
> > > Selection sort and bubble sort have same performance always, right?
>
> > <off-topic> Wrong. </off-topic>
>
> > > 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;
> > > }
> > > }
>
> > <off-topic> This is a bubble sort, implemented
> > inefficiently even by B.S. standards. </off-topic>
>
> > > 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;
> > > }
> > > }
>
> > <off-topic> This is also a bubble sort, whose
> > implementation efficiency rivals that of the first
> > example. </off-topic>
>
> > Did you have a C question?
>
> int a, should probably be int *a, instead.
Yes, they're removed carelessly when the code was posted.
|