473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

i'm a little confused

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

Aug 23 '06 #1
6 1361

porky008 wrote:
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
ni*******@rediffmail.com

Aug 24 '06 #2
porky008 wrote:
>
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);
}
}

--
pete
Aug 24 '06 #3
"porky008" writes:
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.
Aug 24 '06 #4
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.

osmium wrote:
"porky008" writes:
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.
Aug 24 '06 #5
"porky008" writes:
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.
Aug 25 '06 #6
porky008 wrote:
Thanks for the help.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>


Brian (dismayed at a new crop of top-posters)

Aug 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
2
by: Charlie T | last post by:
Hello, I am a little confused... please help: ----------------------------------- <DIV id="01"> <DIV id="aNum"></DIV> <DIV id="bNum"></DIV> <DIV id="cNum"></DIV> </DIV>
1
by: Jeff Uchtman | last post by:
I have read 10000 opinions on returning random records and I am more confused now that before I started. I have a small access db that will contain 20 or so rows with 3 or 4 columns. I like to...
3
by: Laszlo Szijarto | last post by:
In using the BitArray class to parse a single byte, I noticed that the bits show up in Little Endian order. Aren't bits always arranged in Big Endian order? I'm a bit confused and would...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
14
by: CMM | last post by:
Do the developers of Visual 2005 actuall use it??? There's lots of great things in VS2005 (mostly related to the outstanding work done on the CLR)... but in general the LITTLE THINGS totally drag...
6
by: manochavishal | last post by:
Hi , Ihave this code to show binary of an integer. #include<stdio.h> #include<stdlib.h> typedef struct binary* binaryptr; typedef struct binary { int data;
16
by: mdh | last post by:
Can someone look at this little program and tell me why the last line is not compiling. #include <stdio.h> int main (){ int i, j, k; i=0;
1
by: shapper | last post by:
Hello, I need to create a data object to hold a number of records with 3 columns. A datatable would do. My problem is this: 1. I will use this as a GridView datasource. 2. I will need to...
4
by: mattmao | last post by:
I am moving onto the tough part in learning C:( First, about the declaration of a user defined structure: I found this syntax in my lecture notes: struct userinfo { char username; ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.