+ 1

JAVA how to solve this question (JAVA)

Write a Java program to sort an array of positive integers of an given array, in the sorted array the value of the first element should be maximum, second value should be minimum value, third should be second maximum, fourth second be second minimum and so on. below is my code,but i get blank as output,anyone help me check? https://code.sololearn.com/cVxQVkwx93I8

2nd Jul 2019, 12:03 PM
Tzion
Tzion - avatar
6 Answers
+ 1
at the beginning you are trying to sort the values to reverse order. But if you want to do this separately from other code, add } as the end of the first loop: for (int i = ..) { for (int e = ..) { if (arr[i] .. ) { ... } } } // add bracket here (and delete one at the end of code) /*This for loop check whether .. ------------- then your code fall in to infinity loop: a=1 b=8 (45,3) [245, 45, 35, 8, 7, 6, 5, 4, 3, 1] (45>3) true (swap) (45==6||3==6) false (no break) a=3 b=6 ( 8,5) [245, 3, 35, 8, 7, 6, 5, 4, 45, 1] ( 8>5) true (swap) ( 8==6||5==6) false (no break) a=5 b=4 (6,7) [245, 3, 35, 5, 7, 6, 8, 4, 45, 1] ( 6>7) false (no swap, no break, no incrementation) a=5 b=4 (6,7) ( 6>7) false (same) a=5 b=4 (6,7) ( 6>7) false (same) infinity ----------------- I think you need copy values from (head,tail) to new array or do insert, instead swap
3rd Jul 2019, 7:10 AM
zemiak
+ 1
its hard to see the code in phone i think,better in pc....
2nd Jul 2019, 12:04 PM
Tzion
Tzion - avatar
+ 1
zemiak Oh i see , how if i reverse those loop, i mean put the checking loop first a == 6 || b == 6 before a > b ?maybe it would work?
3rd Jul 2019, 12:41 PM
Tzion
Tzion - avatar
+ 1
I rewrite it with your tip, and correct your final output loop, it is now out of If-else.. it produce some result and ends with break import java.util.Arrays; ... var s = System.out; //for debbug output s.printf() label: // necessary for break two loops with one break for ( a = 1 .. ) { for ( b = .. ) { s.printf("a=%d b=%d (%d,%d) %s\n", a,b, arr[a],arr[b], Arrays.toString(arr) ); //output 1 s.printf("(%d==%d||%d==%d) %b\n", arr[a],arr[half],arr[b],arr[half], arr[a] == arr[half] || arr[b] == arr[half]); //output 2 if (arr[a] == arr[half] || arr[b] == arr[half]) { break label; } s.printf("(%d>%d) %b\n",arr[a],arr[b], arr[a]>arr[b]); // //output 3 if (arr[a] > arr[b]) { ... } } } ... else } //end of else for (int w : arr) { //print it out //System.out.println(w); System.out.print(w+","); } }}//main(), class output is: a=1 b=8 (45,3) [245, 45, 35, 8, 7, 6, 5, 4, 3, 1] (45==6||3==6) false (no break) (45>3) true (swap) a=3 b=6 (8,5) [245, 3, 35, 8, 7, 6, 5, 4, 45, 1] (8==6||5==6) false (no break) (8>5) true (swap) a=5 b=4 (6,7) [245, 3, 35, 5, 7, 6, 8, 4, 45, 1] (6==6||7==6) true (break ok) 245,3,35,5,7,6,8,4,45,1 (your final output)
3rd Jul 2019, 1:37 PM
zemiak
+ 1
zemiak Thank you so much! finally i did it , and i first time heard the (name): and break (name); , can u tell me more about it? it like class. One more thing ,the reason i cannot add break only because it will jump to my second for loop in my previous code? add a label: and break label; will break whole code. am i right?
3rd Jul 2019, 2:08 PM
Tzion
Tzion - avatar
+ 1
labeled break is not like class, label is just name for place outside loops where you jump after break - mean you are outside this loops and program continue after this loops. You can break one or more nested loops. Or if you have 3 nested loops, you can break only two loop, depends where label is. Name for label can be different, like here: or breakPosition: but with : If you break without label, you break only one last loop, but your previous code has not been break because if condition was not true. With label you not break whole code, only loops and you can jump only to next line after loops.
3rd Jul 2019, 5:42 PM
zemiak