hi! can anyone help me determine the sorted values of the array in this script.

for (x=0,x<=4,x++)
    for (y=0,y<=3,y++)
         {     if(num[y]>num[y+1])
                     {     temp=num[y];
                           num[y]=num[y+1];
                           num[y+1=temp;
                     }
         {

I'm confused. What values? Why don't you type it in and compile it?

oops im sorry.

num[0]=3
num[1]=10
num[2]=6
num[3]=8
num[4]=1

Okay, well, if you can't compile and run it to see the answer, then you have to be the compiler. Trace it through by hand on a piece of paper, writing what the array looks like after each step.

im just confused on the script, is it translated as ascending or descending? im really having a hard time in absorbing this bubble sort topic.

I understand, but I don't want to take away your fun, this is a good learning exercise. Trace it through by hand for all of the x values and all of the y values, and actually do the swapping. Start out with, is 3 > 10, no, so you leave it alone, is 10 > 6, yes, so swap them. The sorting order will start to be apparent after a few moves.

LOL. can you check this one?

by the way, it should be -6 rather than 6.

3 10 -6 8 1


3 -6 10 8 1
-6 3 10 8 1


-6 3 8 10 1


-6 3 8 1 10
-6 3 1 8 10
-6 1 3 8 10

???

awesome! time to practice more complicated problems. thanks a lot for guiding. :)

Not a problem, thanks for being a good sport. See for your own benefit if you can figure out how to change the code to make it descending.

@jonsca, this ones confusing me, with the same script above, and

num[0]= 6
num[1]= 14
num[2]= 2
num[3]= 11
num[4]= 4

the objective is to find the values of

num[0]=
num[1]=
num[2]=
num[3]=
num[4]=

if x=2 and y=4.

"huh?" can you please breakdown to me on how to answer this one? thanks in adv.

It's asking you for the order during an intermediate point in the sorting process. You've swapped everything around for x = 0 (and the lowest number is in the first element), then done the second pass over the whole array for x = 1 (now the second lowest element is in place, now you're on the third pass over the array (x=2). Y shouldn't ever be 4, but presumably they are saying you've done all the swaps for the third pass. So it will be the two highest elements that are in question.

based on my understanding, if x=2 and y=4

num[0]= 6
num[1]= 14
num[2]= 2
num[3]= 11
num[4]= 4

then

num[0]= 6
num[1]= 14
num[2]= 2 will be replaced by 4?
num[3]= 11
num[4]= 4

does that make any sense or not at all? LOL. although there are 2 number 4.

This article has been dead for over six months. Start a new discussion instead.