468,134 Members | 1,216 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,134 developers. It's quick & easy.

Can someone explain this

I'm just starting to learn C, and the tutorial I'm reading uses this code as an
example:

-----------
#include <stdio.h>

#define MAX 10

int a[MAX];
int rand_seed=10;

/* from K&R
- returns random number between 0 and 32767.*/
int rand()
{
rand_seed = rand_seed * 1103515245 +12345;
return (unsigned int)(rand_seed / 65536) % 32768;
}

int main()
{
int i,t,x,y;

/* fill array */
for (i=0; i < MAX; i++)
{
a[i]=rand();
printf("%d\n",a[i]);
}

/* bubble sort the array */
for (x=0; x < MAX-1; x++)
for (y=0; y < MAX-x-1; y++)
if (a[y] > a[y+1])
{
t=a[y];
a[y]=a[y+1];
a[y+1]=t;
}
/* print sorted array */
printf("--------------------\n");
for (i=0; i < MAX; i++)
printf("%d\n",a[i]);
return 0;
}
-------------

The code prints out ten "random numbers" based on what value the rand_seed is,
and then the "bubble sort" part of the code prints these numbers out in order
from least to greatest. Can someone explain to me how the "bubble sort" part
of the script works?
Nov 13 '05 #1
5 2481

"J Allen Horner" <ja**********@aol.com> wrote in message

/* bubble sort the array */
for (x=0; x < MAX-1; x++)
for (y=0; y < MAX-x-1; y++)
if (a[y] > a[y+1])
{
t=a[y];
a[y]=a[y+1];
a[y+1]=t;
}

Can someone explain to me how the "bubble sort" part of the script works?

It looks buggy to me. x steps through the array. Fine. Now y steps through
the array from 0 to x. The first two elements will be sorted. However then y
steps through the sorted two elements again, uselessly, and then checks the
third element. If it happens to be the lowest item on the list, the list
won't be properly sorted.
Probably the intention was to write the loop for(y=x; y>=0;y--). Then the
approach would work, though it is still a very greedy sort.


Nov 13 '05 #2
In 'comp.lang.c', ja**********@aol.com (J Allen Horner) wrote:

<not bad code snipped>
The code prints out ten "random numbers" based on what value the
rand_seed is, and then the "bubble sort" part of the code prints these
numbers out in order from least to greatest. Can someone explain to me
how the "bubble sort" part of the script works?


Not a script, but a [code] source.

It's really an algorithm question. One of the first thing to learn when you
start programming, is to separate the design parts (like algorithms) from the
coding parts (C-language in your case).

Type "bubble sort" on Google. I'm sure you will have a lot of good answers.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #3
Malcolm wrote:

"J Allen Horner" <ja**********@aol.com> wrote in message

/* bubble sort the array */
for (x=0; x < MAX-1; x++)
for (y=0; y < MAX-x-1; y++)
if (a[y] > a[y+1])
{
t=a[y];
a[y]=a[y+1];
a[y+1]=t;
}

Can someone explain to me how the "bubble sort" part of the script works?

It looks buggy to me. x steps through the array. Fine. Now y steps through
the array from 0 to x.


No. y steps through from 0 to MAX-1-x.
After the first pass (x=0) of the outer loop, the greatest element will be
in a[MAX-1].
After the second pass(x=1) of the outer loop, the second greatest element
will be in a[MAX-2], and so on...
So the code looks fine to me.
Nov 13 '05 #4

"Nejat AYDIN" <ne********@superonline.com> wrote in message
It looks buggy to me. x steps through the array. Fine. Now y steps
through the array from 0 to x.


No. y steps through from 0 to MAX-1-x.

You're right, yes. I misread the minus sign.
Nov 13 '05 #5
On 12 Jul 2003 21:52:24 GMT, in comp.lang.c , ja**********@aol.com (J
Allen Horner) wrote:

The code prints out ten "random numbers" based on what value the rand_seed is,
and then the "bubble sort" part of the code prints these numbers out in order
from least to greatest. Can someone explain to me how the "bubble sort" part
of the script works?


Not trying to be funny, but if you're asking how does bubble sort
wotk, it is a standard sorting algo which should be explained in any
half-decent algorithms book. Knuth probably does it. CLC is the wrong
place to ask.

Also note that C doesn't talk in terms of scripts, but more usually
in terms of code modules, or just code.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Maciej Nadolski | last post: by
8 posts views Thread by Peter Foti | last post: by
3 posts views Thread by MarcJessome | last post: by
5 posts views Thread by klj_mcsd | last post: by
4 posts views Thread by Richard L Rosenheim | last post: by
1 post views Thread by Tran Hong Quang | last post: by
6 posts views Thread by Dave Young | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.