Can someone explain this

J

J Allen Horner

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=rand();
printf("%d\n",a);
}

/* 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);


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?
 
E

Emmanuel Delahaye

In 'comp.lang.c', (e-mail address removed) (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 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.
 
M

Malcolm

J Allen Horner said:
/* 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.
 
N

Nejat AYDIN

Malcolm said:
J Allen Horner said:
/* 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.
 
M

Mark McIntyre

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.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top