fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone{
  9.  
  10. public static <T extends Comparable<T>> void concurrentSort( final List<T> key, List<?>... lists){
  11. // Do validation
  12. if(key == null || lists == null)
  13. throw new NullPointerException("key cannot be null.");
  14.  
  15. for(List<?> list : lists)
  16. if(list.size() != key.size())
  17. throw new IllegalArgumentException("all lists must be the same size");
  18.  
  19. // Lists are size 0 or 1, nothing to sort
  20. if(key.size() < 2)
  21. return;
  22.  
  23. // Create a List of indices
  24. List<Integer> indices = new ArrayList<Integer>();
  25. for(int i = 0; i < key.size(); i++)
  26. indices.add(i);
  27.  
  28. // Sort the indices list based on the key
  29. Collections.sort(indices, new Comparator<Integer>(){
  30. @Override public int compare(Integer i, Integer j) {
  31. return key.get(i).compareTo(key.get(j));
  32. }
  33. });
  34.  
  35. Map<Integer, Integer> swapMap = new HashMap<Integer, Integer>(indices.size());
  36. List<Integer> swapFrom = new ArrayList<Integer>(indices.size()),
  37. swapTo = new ArrayList<Integer>(indices.size());
  38.  
  39. // create a mapping that allows sorting of the List by N swaps.
  40. for(int i = 0; i < key.size(); i++){
  41. int k = indices.get(i);
  42. while(i != k && swapMap.containsKey(k))
  43. k = swapMap.get(k);
  44.  
  45. swapFrom.add(i);
  46. swapTo.add(k);
  47. swapMap.put(i, k);
  48. }
  49.  
  50. // use the swap order to sort each list by swapping elements
  51. for(List<?> list : lists)
  52. for(int i = 0; i < list.size(); i++)
  53. Collections.swap(list, swapFrom.get(i), swapTo.get(i));
  54. }
  55.  
  56. public static void main (String[] args) throws java.lang.Exception{
  57. List<Integer> ids = Arrays.asList(0, 1, 2, 3);
  58.  
  59. // Dupes are allowed
  60. List<String> colors = Arrays.asList("blue", "yellow", "red", "black");
  61.  
  62. // List Types do not need to be the same
  63. List<String> clothes = Arrays.asList("shoes", "pants", "boots", "coat");
  64.  
  65. System.out.println("======= Before Sort ========");
  66. System.out.println(ids);
  67. System.out.println(colors);
  68. System.out.println(clothes);
  69. System.out.println();
  70. System.out.println();
  71.  
  72. System.out.println("======= Sort By ID =========");
  73. concurrentSort(ids, ids, colors, clothes);
  74. System.out.println(ids);
  75. System.out.println(colors);
  76. System.out.println(clothes);
  77. System.out.println();
  78. System.out.println();
  79.  
  80. System.out.println("======= Sort By Color =========");
  81. concurrentSort(colors, ids, colors, clothes);
  82. System.out.println(ids);
  83. System.out.println(colors);
  84. System.out.println(clothes);
  85. System.out.println();
  86. System.out.println();
  87.  
  88. System.out.println("======= Sort By Clothes =========");
  89. concurrentSort(clothes, ids, colors, clothes);
  90. System.out.println(ids);
  91. System.out.println(colors);
  92. System.out.println(clothes);
  93. System.out.println();
  94. System.out.println();
  95. }
  96. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
======= Before Sort ========
[0, 1, 2, 3]
[blue, yellow, red, black]
[shoes, pants, boots, coat]


======= Sort By ID =========
[0, 1, 2, 3]
[blue, yellow, red, black]
[shoes, pants, boots, coat]


======= Sort By Color =========
[3, 0, 2, 1]
[black, blue, red, yellow]
[coat, shoes, boots, pants]


======= Sort By Clothes =========
[2, 3, 1, 0]
[red, black, yellow, blue]
[boots, coat, pants, shoes]