PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Selection sort and bubble sort
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Selection sort and bubble sort

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 20h32   #1 (permalink)
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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;
}
}

  Réponse avec citation
Vieux 16/10/2007, 20h51   #2 (permalink)
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selection sort and bubble sort

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?

--
Eric.Sosman@sun.com


  Réponse avec citation
Vieux 17/10/2007, 00h22   #3 (permalink)
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selection sort and bubble sort

On Oct 16, 12:32 pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.com> wrote:
> Selection sort and bubble sort have same performance always, right?


Sort of. They are both O(n*n) but bubble sort has a larger constant
of proportionality.

> Are the following correctly implemented the both functions.

No.

> Comments
> are welcome.

news:comp.programming is the appropriate place for your question.

[snip of icky[tm] sorts, insertion of nearly equally icky[tm] sorts:]

#include <stdlib.h>
#ifdef UNIT_TEST
typedef int e_type;
#else
#include "e_type.h"
#endif

void selection_sort(e_type array[], size_t size)
{
size_t i,
j,
smallest;
e_type tmp;
for (i = 0; i < size - 1; i++) {
smallest = i;
for (j = i + 1; j < size; j++) {
if (array[j] < array[smallest]) {
smallest = j;
}
}
tmp = array[i];
array[i] = array[smallest];
array[smallest] = tmp;
}
}

void bubble_sort(e_type * array, size_t length)
{
size_t i,
j;
e_type tmp;
for (i = 0; i < length; i++) {
for (j = 0; j < i; j++) {
if (array[i] < array[j]) {
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}

#ifdef UNIT_TEST
#include <stdio.h>
#include <time.h>

static e_type arr[65535];
static size_t e_size = sizeof arr / sizeof arr[0];

void sort_check(void)
{
size_t index;
int failed = 0;
for (index = 1; index < e_size; index++) {
if (arr[index - 1] > arr[index]) {
printf("Out of sort at %d where arr[%d]=%d > arr[%d]=%d
\n", index, index - 1, arr[index - 1], index, arr[index]);
failed = 1;
}
}
if (failed)
puts("Sort failed.");
else
puts("Sort succeeded.");

}

void mk_rand_arr(void)
{
size_t index;
for (index = 0; index < e_size; index++) {
arr[index] = rand();
}
}

int main()
{
time_t start;
double delta;
mk_rand_arr();
puts("Selection Sort:");
start = time(NULL);
selection_sort(arr, e_size);
delta = difftime(time(NULL), start);
sort_check();
printf("Elapsed time is %f\n", delta);
mk_rand_arr();
puts("Bubble Sort:");
start = time(NULL);
bubble_sort(arr, e_size);
delta = difftime(time(NULL), start);
printf("Elapsed time is %f\n", delta);
sort_check();
return 0;
}
#endif


  Réponse avec citation
Vieux 17/10/2007, 02h57   #4 (permalink)
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selection sort and bubble sort

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.

--
pete
  Réponse avec citation
Vieux 17/10/2007, 03h15   #5 (permalink)
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 17/10/2007, 03h50   #6 (permalink)
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selection sort and bubble sort

On Oct 17, 7:22 am, user923005 <dcor...@connx.com> wrote:
> On Oct 16, 12:32 pm, "lovecreatesbea...@gmail.com"
>
> <lovecreatesbea...@gmail.com> wrote:
> > Selection sort and bubble sort have same performance always, right?

>
> Sort of. They are both O(n*n) but bubble sort has a larger constant
> of proportionality.
>
> > Are the following correctly implemented the both functions.

>
> No.
>
> > Comments
> > are welcome.

>
> news:comp.programming is the appropriate place for your question.
>


People use different language there, and I like the C code.

> [snip of icky[tm] sorts, insertion of nearly equally icky[tm] sorts:]
>
> #include <stdlib.h>
> #ifdef UNIT_TEST
> typedef int e_type;
> #else
> #include "e_type.h"
> #endif
>
> void selection_sort(e_type array[], size_t size)
> {
> size_t i,
> j,
> smallest;
> e_type tmp;
> for (i = 0; i < size - 1; i++) {
> smallest = i;
> for (j = i + 1; j < size; j++) {
> if (array[j] < array[smallest]) {
> smallest = j;
> }
> }
> tmp = array[i];
> array[i] = array[smallest];
> array[smallest] = tmp;
> }
>
> }
>


Your code does less exchange than mine (though an asterisk missed for
the parameter a).

> void bubble_sort(e_type * array, size_t length)
> {
> size_t i,
> j;
> e_type tmp;
> for (i = 0; i < length; i++) {
> for (j = 0; j < i; j++) {
> if (array[i] < array[j]) {
> tmp = array[i];
> array[i] = array[j];
> array[j] = tmp;
> }
> }
> }
>
> }
>
> #ifdef UNIT_TEST
> #include <stdio.h>
> #include <time.h>
>
> static e_type arr[65535];
> static size_t e_size = sizeof arr / sizeof arr[0];
>
> void sort_check(void)
> {
> size_t index;
> int failed = 0;
> for (index = 1; index < e_size; index++) {
> if (arr[index - 1] > arr[index]) {
> printf("Out of sort at %d where arr[%d]=%d > arr[%d]=%d
> \n", index, index - 1, arr[index - 1], index, arr[index]);
> failed = 1;
> }
> }
> if (failed)
> puts("Sort failed.");
> else
> puts("Sort succeeded.");
>
> }
>
> void mk_rand_arr(void)
> {
> size_t index;
> for (index = 0; index < e_size; index++) {
> arr[index] = rand();
> }
>
> }
>
> int main()


If UNIT_TEST isn't defined, the main() function is skipped. Your .c
file can compile but can't link, am I right?

> {
> time_t start;
> double delta;
> mk_rand_arr();
> puts("Selection Sort:");
> start = time(NULL);
> selection_sort(arr, e_size);
> delta = difftime(time(NULL), start);
> sort_check();
> printf("Elapsed time is %f\n", delta);
> mk_rand_arr();
> puts("Bubble Sort:");
> start = time(NULL);
> bubble_sort(arr, e_size);
> delta = difftime(time(NULL), start);
> printf("Elapsed time is %f\n", delta);
> sort_check();
> return 0;}
>
> #endif



  Réponse avec citation
Vieux 17/10/2007, 04h07   #7 (permalink)
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selection sort and bubble sort

On Oct 16, 7:15 pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.com> wrote:
> 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.


As a rule of thumb, don't post anything here until it compiles or
until you are simply unable to figure out why it won't compile.
In the latter case, point out "I can't get this code to compile --
what's wrong with it?" or something like that.

When you do get the code to compile, simply copy and paste the working
code into your browser or news client. It is far less error prone
than retyping and considerably easier also.

  Réponse avec citation
Vieux 17/10/2007, 04h41   #8 (permalink)
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selection sort and bubble sort

On Oct 16, 7:50 pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.com> wrote:
> On Oct 17, 7:22 am, user923005 <dcor...@connx.com> wrote:
>
> > On Oct 16, 12:32 pm, "lovecreatesbea...@gmail.com"

>
> > <lovecreatesbea...@gmail.com> wrote:
> > > Selection sort and bubble sort have same performance always, right?

>
> > Sort of. They are both O(n*n) but bubble sort has a larger constant
> > of proportionality.

>
> > > Are the following correctly implemented the both functions.

>
> > No.

>
> > > Comments
> > > are welcome.

>
> > news:comp.programming is the appropriate place for your question.

>
> People use different language there, and I like the C code.


That does not make your question topical here. You are not asking
about C. You are asking about algorithms. There is no algorithms
group, per se, so the closest thing is to ask in
news:comp.programming. If you ask for C there, you will most likely
get it.

> > [snip of icky[tm] sorts, insertion of nearly equally icky[tm] sorts:]

>
> > #include <stdlib.h>
> > #ifdef UNIT_TEST
> > typedef int e_type;
> > #else
> > #include "e_type.h"
> > #endif

>
> > void selection_sort(e_type array[], size_t size)
> > {
> > size_t i,
> > j,
> > smallest;
> > e_type tmp;
> > for (i = 0; i < size - 1; i++) {
> > smallest = i;
> > for (j = i + 1; j < size; j++) {
> > if (array[j] < array[smallest]) {
> > smallest = j;
> > }
> > }
> > tmp = array[i];
> > array[i] = array[smallest];
> > array[smallest] = tmp;
> > }

>
> > }

>
> Your code does less exchange than mine (though an asterisk missed for
> the parameter a).


Of course, it's simply awful anyway. The only O(n^2) sort worth
knowing is insertion sort. There are some very, very rare cases where
selection or even bubble sort can prove worthwhile, but it takes a
real expert to know what they are and even in that case, it carries
intense danger (in case the initial conditions do not hold).

> > void bubble_sort(e_type * array, size_t length)
> > {
> > size_t i,
> > j;
> > e_type tmp;
> > for (i = 0; i < length; i++) {
> > for (j = 0; j < i; j++) {
> > if (array[i] < array[j]) {
> > tmp = array[i];
> > array[i] = array[j];
> > array[j] = tmp;
> > }
> > }
> > }

>
> > }

>
> > #ifdef UNIT_TEST
> > #include <stdio.h>
> > #include <time.h>

>
> > static e_type arr[65535];
> > static size_t e_size = sizeof arr / sizeof arr[0];

>
> > void sort_check(void)
> > {
> > size_t index;
> > int failed = 0;
> > for (index = 1; index < e_size; index++) {
> > if (arr[index - 1] > arr[index]) {
> > printf("Out of sort at %d where arr[%d]=%d > arr[%d]=%d
> > \n", index, index - 1, arr[index - 1], index, arr[index]);
> > failed = 1;
> > }
> > }
> > if (failed)
> > puts("Sort failed.");
> > else
> > puts("Sort succeeded.");

>
> > }

>
> > void mk_rand_arr(void)
> > {
> > size_t index;
> > for (index = 0; index < e_size; index++) {
> > arr[index] = rand();
> > }

>
> > }

>
> > int main()

>
> If UNIT_TEST isn't defined, the main() function is skipped. Your .c
> file can compile but can't link, am I right?


That's the idea of a unit test. If you have UNIT_TEST defined, then
the code is tested. If UNIT_TEST is not defined, then you will need a
header file to define what e_type means (possibly switching by means
of a macro). In any case, you won't want to use either selection sort
or bubble sort for anything at all besides a toy demo program because
both of them are icky(tm).

> > {
> > time_t start;
> > double delta;
> > mk_rand_arr();
> > puts("Selection Sort:");
> > start = time(NULL);
> > selection_sort(arr, e_size);
> > delta = difftime(time(NULL), start);
> > sort_check();
> > printf("Elapsed time is %f\n", delta);
> > mk_rand_arr();
> > puts("Bubble Sort:");
> > start = time(NULL);
> > bubble_sort(arr, e_size);
> > delta = difftime(time(NULL), start);
> > printf("Elapsed time is %f\n", delta);
> > sort_check();
> > return 0;}

>
> > #endif- Hide quoted text -


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h52.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22406 seconds with 16 queries