Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help me understand the program! pls

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Location
    Bangalore!!
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help me understand the program! pls

    Hi here is the array sort code i came across.

    class Arraysorting
    {
    public void bubblesort(Int A[])
    {
    // array elements are : {5,3,8,9,2,1,12,90,15}
     
    int i,j,tmp;
    for (i=0 ; i <10 ; i++)
    for (j=0; j<9-i-1; j++)
    {
     if (A[j] > A[j +1])
    {
    tmp = A[j];
    A[j] = A[j +1];
    A[j +1] = tmp;
    }
    }
    System.out.println("Array in ascending order is ->");
    for (i=0 ; i <10; i++)
    System.out.println(A[i] + "\n");
    }
    }

    here i couldn't understand the use of " (j=0; j<9-i-1; j++)".

    If possible also help me understand the program on the whole.
    Last edited by jps; August 12th, 2013 at 11:15 AM. Reason: code tags


  2. #2
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    22
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand the program! pls

    Are the array elements in order like is A[0] = 5? I'm trying to help but this might clear it up for me.

    edit :
    There is an error on (Int A[]){ it should be (int A[]){

    --- Update ---

    Alright so I ran the program (after making a main method for it) and here was the output:
    Array in ascending order is ->
    1
    2
    3
    4
    5
    8
    9
    12
    15
    90
    Then i had an exception in main with an out of bounds exception.(I don't know how exceptions yet. My fault coding the main method.) But as you can see it puts the numbers in order from least to greatest. I tried to trace the code (see how it worked) and goodness it was hard! Once I get a clear answer on how it works i'll tell you! Hope this helped!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Help me understand the program! pls

    As has been mentioned to you in the past, please use code tags when posting code, instructions can be found on the Announcements page. This is to preserve readability of the code for future readers, and to save time for the volunteers who help answer questions.

    Quote Originally Posted by YogiB1810 View Post
    here i couldn't understand the use of " (j=0; j<9-i-1; j++)".
    Have you tried to step through the code on paper? Start at the first loop, write out what happens in the background as the code progresses. You will easily see which elements of the array are being considered on each iteration of the loop. You will see when the conditional passes and which elements are swapped.

    What did your favorite search engine say about bubble sort?

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Location
    Bangalore!!
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Help me understand the program! pls

    Hi, can you pass me the code you that you have altered. I guess it would be better to understand.


    Quote Originally Posted by lil_misfitss View Post
    Are the array elements in order like is A[0] = 5? I'm trying to help but this might clear it up for me.

    edit :
    There is an error on (Int A[]){ it should be (int A[]){

    --- Update ---

    Alright so I ran the program (after making a main method for it) and here was the output:
    Array in ascending order is ->
    1
    2
    3
    4
    5
    8
    9
    12
    15
    90
    Then i had an exception in main with an out of bounds exception.(I don't know how exceptions yet. My fault coding the main method.) But as you can see it puts the numbers in order from least to greatest. I tried to trace the code (see how it worked) and goodness it was hard! Once I get a clear answer on how it works i'll tell you! Hope this helped!

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Help me understand the program! pls

    Hello.
    Did you copy the code from internet?
    If so then I would suggest you should first learn about bubble sort and write your own code. Initially it may look difficult.
    But as you step in it shall become easier.

    Syed.

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Location
    Bangalore!!
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand the program! pls

    Hi Syed,

    No, it was from one of the books i was referring to and i came across this program.

Similar Threads

  1. Problem with my program! Pls HELP!
    By adi2609 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2013, 10:23 PM
  2. pls give create the program for the following
    By brsshalini in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 24th, 2013, 09:17 AM
  3. pls help me with word occurances program
    By kashif in forum Collections and Generics
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  4. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2011, 09:39 AM
  5. can anyone help me pls to build this program
    By Xhejn in forum Member Introductions
    Replies: 1
    Last Post: May 31st, 2011, 05:05 PM

Tags for this Thread