i'm a little confused

P

porky008

We are still going over pseudo code and we are working on arrays right
now. I am having some difficulty with the bubble sort algorithm. Can
some one give me a better example than this one? I do understand that
each pass the larger values are to drop to the bottom but the algorithm
is still throwing me off.
While the array A is not sorted
For K = 1 Step 1 To N - 1
If A[K] > A[K + 1] Then
Interchange A[K] and A[K + 1]
End If
End For
End While
 
N

nutsnduts

porky008 said:
We are still going over pseudo code and we are working on arrays right
now. I am having some difficulty with the bubble sort algorithm. Can
some one give me a better example than this one? I do understand that
each pass the larger values are to drop to the bottom but the algorithm
is still throwing me off.
While the array A is not sorted
For K = 1 Step 1 To N - 1
If A[K] > A[K + 1] Then
Interchange A[K] and A[K + 1]
End If
End For
End While

With each pass (one K=1 to N-1 round of For loop) you are successively
sorting the array. So in Pass X (X=1,2.. N-1) you end up sorting the
top (highest) X values of array in top(index) X corner of array.

e.g.,
in Pass 1... you have (atleast)sorted 1 highest value in 1 highest
index ( i.e. in N-1 index)
in Pass 2... you have (atleast)sorted 2 highest values in 2 highest
indexes ( i.e. in N-1 and N-2 indexes)
....
.....
in Pass N-1... you have (atleast)sorted N-1 highest values in N-1
highest indexes ( i.e. in N-1 and N-2,N-3.....3,2,1,0 indexes)


hope this helps

-nutsnduts
(e-mail address removed)
 
P

pete

porky008 said:
I am having some difficulty with the bubble sort algorithm. Can
some one give me a better example than this one?

Here's four variations on bubblesort with a qsort interface:

/* bbsort, too simple bubblesort */
/* b0sort, very simple bubblesort */
/* b_sort, bubblesort */
/* c_sort, cocktail shaker bubblesort */

#define BYTE_SWAP(A, B) \
{ \
p1 = (A); \
p2 = (B); \
end = p2 + size; \
do { \
swap = *p1; \
*p1++ = *p2; \
*p2++ = swap; \
} while (p2 != end); \
}

void bbsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *last, *middle;
unsigned char *p1, *p2, *end, swap;

if (nmemb-- > 1) {
last = base;
last += nmemb * size;
do {
middle = base;
do {
if (compar(middle, middle + size) > 0) {
BYTE_SWAP(middle, middle + size);
}
middle += size;
} while (middle != last);
} while (--nmemb != 0);
}
}

void b0sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *last, *middle;
unsigned char *p1, *p2, *end, swap;

if (nmemb-- > 1) {
last = base;
last += nmemb * size;
do {
middle = base;
do {
if (compar(middle, middle + size) > 0) {
BYTE_SWAP(middle, middle + size);
}
middle += size;
} while (middle != last);
last -= size;
} while (--nmemb != 0);
}
}

void b_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *last, *next, *middle;
unsigned char *p1, *p2, *end, swap;

if (nmemb-- > 1) {
last = base;
last += nmemb * size;
do {
middle = next = base;
do {
if (compar(middle, middle + size) > 0) {
BYTE_SWAP(middle, middle + size);
next = middle;
}
middle += size;
} while (middle != last);
last = next;
} while (last != base);
}
}

void c_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *last, *next, *middle;
unsigned char *p1, *p2, *end, swap;

if (nmemb-- > 1) {
next = base;
next += nmemb * size;
do {
middle = last = next;
do {
if (compar(middle - size, middle) > 0) {
BYTE_SWAP(middle - size, middle);
next = middle;
}
middle -= size;
} while (middle != base);
if (last == next) {
break;
}
middle = base = next;
do {
if (compar(middle, middle + size) > 0) {
BYTE_SWAP(middle, middle + size);
next = middle;
}
middle += size;
} while (middle != last);
} while (base != next);
}
}
 
O

osmium

porky008 said:
We are still going over pseudo code and we are working on arrays right
now. I am having some difficulty with the bubble sort algorithm. Can
some one give me a better example than this one? I do understand that
each pass the larger values are to drop to the bottom but the algorithm
is still throwing me off.

sorted <- false
While the array A is not sorted sorted <- true
For K = 1 Step 1 To N - 1
If A[K] > A[K + 1] Then
Interchange A[K] and A[K + 1]
sorted <- false
End If
End For
End While

ISTM some key parts were left out of your sample.
 
P

porky008

Thanks for the help. My instructor is not that good at answering
questions so I am fighting to keep up with the others. If its not to
much to ask for a little more help, I asked my instructor 2 days ago
how to add in a way to find the "mean" of the group to be sorted. I
know you can but haven't found a way in the text book or net to do
it. I am not a big fan of asking others for help but could you maybe
point me in the right direction on this one.
porky008 said:
We are still going over pseudo code and we are working on arrays right
now. I am having some difficulty with the bubble sort algorithm. Can
some one give me a better example than this one? I do understand that
each pass the larger values are to drop to the bottom but the algorithm
is still throwing me off.

sorted <- false
While the array A is not sorted sorted <- true
For K = 1 Step 1 To N - 1
If A[K] > A[K + 1] Then
Interchange A[K] and A[K + 1]
sorted <- false
End If
End For
End While

ISTM some key parts were left out of your sample.
 
O

osmium

porky008 said:
I asked my instructor 2 days ago
how to add in a way to find the "mean" of the group to be sorted. I
know you can but haven't found a way in the text book or net to do
it. I am not a big fan of asking others for help but could you maybe
point me in the right direction on this one.

There's really not much to know. The arithmetic mean is simply the sum of
the samples divided by the number of samples. The only thing that might not
be pretty obvious is that you will rarely get the right answer if you divide
two integers. Force at least one of the operands to either be, or be
treated as (by casting) , a double before dividing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top