472,983 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2753

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
8
by: Peter Foti | last post by:
I'm working on a site and the client said he thinks "the main title should be around 28pt and the subtitle probably 18pt". I know pt's are bad, and I want to convince him that he should NOT be...
3
by: MarcJessome | last post by:
Hi, I was wondering if someone could help me through learning C++. I've tried learning before, but I find I would work better if I had someone that could help explain a few things to me. Im using...
5
by: klj_mcsd | last post by:
Let's say you set txtlastname1.visible = true Then DirectCast(Page.FindControl("txtLastName1"), TextBox).Visible = False Why when you check txtlastname1.visible it is equal to True?
4
by: Richard L Rosenheim | last post by:
Using Visual Studio 2003 running under Windows XP Pro w/SP 1, when I create a new form, the form's title bar has the Windows 2000 look. Yet, in the various Microsoft demos, the form's title bar...
1
by: Tran Hong Quang | last post by:
Hi, In header file, I see this declaration: #define DECLARE_VTBL(iname) iname vt##iname; then, in C file, there are below codes: typedef struct _Abc { .......... } abc;
4
by: Adrian | last post by:
can someone explain the cross domain security re AJAX in IE? I have a page that calls a web service (WS) from another domain (the target browser is only IE6) and displays it's results! all works...
1
by: mansa | last post by:
a) A company receives hundreds of orders every week through the mail. Explain carefully how BATCH- PROCESSING might be used. Describe the role of the BATCH TOTAL in this process. Explain how batch...
6
by: Dave Young | last post by:
I'm looking at some code that i've inherited and I'm not really familar with what's going on here and was hoping somone could explain it to me. For reference: f1 is a long f2 is a long ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.