473,386 Members | 1,883 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,386 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 2782

"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.