Results 1 to 7 of 7
- 11-05-2010, 01:21 PM #1
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)
THANKS IN ADVANCE:)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]); } } }
- 11-05-2010, 01:31 PM #2
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 :(
- 11-05-2010, 01:42 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Take a look at the your nested for loop.
Check out your inner loop with the j variable, when is that being changed? How will that end?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; }
- 11-05-2010, 02:37 PM #4
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) addingbefore the last < for > loop .Java Code:System.out.println("The numbers are sorted in ascending order :");
Java Code:Enter 4 integers : 6 3 2 0 The numbers are sorted in ascending order : 6 6 6 6 Process completed.
- 11-05-2010, 03:05 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
- 11-06-2010, 08:31 PM #6
Sorry I couldn't solve it properly ..
Could you help me ?
- 11-06-2010, 10:03 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Similar Threads
-
Most of my trouble is identifying a pattern
By ElkNinja in forum New To JavaReplies: 4Last Post: 10-10-2010, 06:17 AM -
help with identifying components of a gui
By jaytee in forum New To JavaReplies: 1Last Post: 03-08-2010, 02:28 AM -
Identifying System Architecture-Urgent
By Robert_85 in forum Advanced JavaReplies: 2Last Post: 08-16-2009, 04:34 AM -
i can't see the mistake
By PVL268 in forum New To JavaReplies: 3Last Post: 04-29-2009, 05:26 AM -
uniquely identifying a browser window
By mrak in forum Advanced JavaReplies: 3Last Post: 01-17-2009, 06:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks