fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. //{first max,first min,second max,second min,third max,third min and so on.....}
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.List;
  9.  
  10. public class Main {
  11.  
  12. public static void main(String args[]) {
  13.  
  14. List<Integer> list = new ArrayList<Integer>();
  15.  
  16. list.add(10);
  17. list.add(20);
  18. list.add(30);
  19. list.add(70);
  20. list.add(50);
  21. list.add(60);
  22. list.add(40);
  23.  
  24. Comparator<Integer> cmp = new Comparator<Integer>() {
  25.  
  26. @Override
  27. public int compare(Integer o1, Integer o2) {
  28.  
  29. if (o1 < o2) {
  30. return 1;
  31. } else {
  32. return -1;
  33. }
  34. }
  35. };
  36.  
  37. Collections.sort(list, cmp);
  38. list = modifyList(list);
  39. for(int elem : list){
  40. System.out.print(elem + " ");
  41. }
  42. System.out.println();
  43.  
  44. }
  45.  
  46. public static List<Integer> modifyList(List<Integer> list) {
  47. List<Integer> result = new ArrayList<>(list.size());
  48.  
  49. int i = 0, j = list.size() - 1;
  50. int r = 0;
  51. while (r < list.size()) {
  52. result.add(r++, list.get(i++));
  53. if (i < j)
  54. result.add(r++, list.get(j--));
  55. }
  56.  
  57. return result;
  58. }
  59. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
70 10 60 20 50 30 40