Results 1 to 7 of 7
  1. #1
    o0oNorao0o's Avatar
    o0oNorao0o is offline Member
    Join Date
    Oct 2009
    Posts
    51
    Rep Power
    0

    Red face Help me ! identifying the mistake

    Help me to identify the mistake ..
    it's about selection sort .
    I can't execute it properly although the builder shows (PROCESS COMPLETED)

    Java Code:
    import java.util.Scanner;
    public class select 
    {
    	public static void main (String []args)
    	{
    		Scanner abc= new Scanner (System.in);
    		System.out.println("Enter 4 integers :");
    		int [] num = new int [4];
    		for (int i = 0 ; i<num.length ; i++)
    		{
    					num[i]= abc.nextInt();
    		}
    		for (int i = num.length-1 ; i >=0 ;i--)
    		{
    		int	highest = i;
    		for (int j = i ; j>=0 ;i--)
    		{
    			if (num [j]> num [highest])
    			{
    				highest = j ; 
    				}
    		}
    		int temp = num[i];
    		num[i] = num[highest];
    		num[highest]=temp;
    		}
    		for (int i = 0 ; i<num.length ; i++)
    		{
    			System.out.println(num[i]);
    		}
    	}
    }
    THANKS IN ADVANCE:)

  2. #2
    o0oNorao0o's Avatar
    o0oNorao0o is offline Member
    Join Date
    Oct 2009
    Posts
    51
    Rep Power
    0

    Default

    I mean the output only shows the message

    "Enter 4 integers "
    then the user can write infinite integer numbers although I coded the program to enter only 4 numbers :(

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Take a look at the your nested for loop.
    Java Code:
         for (int i = num.length-1 ; i >=0 ;i--) {
              int highest = i;
              for (int j = i ; j>=0 ;i--) {
                   if (num [j]> num [highest]) {
                         highest = j ; 
                   }
              }
              int temp = num[i];
              num[i] = num[highest];
              num[highest]=temp;
         }
    Check out your inner loop with the j variable, when is that being changed? How will that end?

  4. #4
    o0oNorao0o's Avatar
    o0oNorao0o is offline Member
    Join Date
    Oct 2009
    Posts
    51
    Rep Power
    0

    Default

    Oh thanks I forgot to change the decrement variable of the inner loop
    but I still have problem ..
    Look at the output ..after :
    1) changing the decrement variable of the inner loop to j .
    2) adding
    Java Code:
    		System.out.println("The numbers are sorted in ascending order :");
    before the last < for > loop .

    Java Code:
    Enter 4 integers :
    6
    3
    2
    0
    The numbers are sorted in ascending order :
    6
    6
    6
    6
    
    Process completed.

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Try adding some debug lines to see whats going on when you are sorting. If I had to guess, since you are decrementing in the second for loop, its always finding the largest since its in the lowest index. You then replace all the indexes with the highest.

  6. #6
    o0oNorao0o's Avatar
    o0oNorao0o is offline Member
    Join Date
    Oct 2009
    Posts
    51
    Rep Power
    0

    Default

    Sorry I couldn't solve it properly ..
    Could you help me ?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Did you add some debug lines? (System.out.println() to show the values of things) Perhaps you could post your current code, and say what the behaviour is.

Similar Threads

  1. Most of my trouble is identifying a pattern
    By ElkNinja in forum New To Java
    Replies: 4
    Last Post: 10-10-2010, 07:17 AM
  2. help with identifying components of a gui
    By jaytee in forum New To Java
    Replies: 1
    Last Post: 03-08-2010, 03:28 AM
  3. Identifying System Architecture-Urgent
    By Robert_85 in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2009, 05:34 AM
  4. i can't see the mistake
    By PVL268 in forum New To Java
    Replies: 3
    Last Post: 04-29-2009, 06:26 AM
  5. uniquely identifying a browser window
    By mrak in forum Advanced Java
    Replies: 3
    Last Post: 01-17-2009, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •