1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.Arrays; public class Toast { /** * Rearrange an array in order – smallest, largest, 2nd smallest, 2nd largest, .. * Given an array of integers, task is to print the array in the order – smallest number, Largest number, 2nd smallest number, 2nd largest number, 3rd smallest number, 3rd largest number and so on….. * * http://www.geeksforgeeks.org/rearrange-array-order-smallest-largest-2nd-smallest-2nd-largest/ * */ static int[] question_1(int input[]) { int[] in = Arrays.copyOf(input, input.length); Arrays.sort(in); int[] output = new int[in.length]; //using method as array reversing int start = 0, end = in.length - 1; int i = 0; while(start <= end) { output[i++] = in[start++]; if(i < in.length) output[i++] = in[end--]; } return output; } } |
styled using hilite.me
No comments:
Post a Comment