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> key = Arrays.asList(4, 3, 1, 2, 1);
  58.  
  59. // Dupes are allowed
  60. List<String> list1 = Arrays.asList("Four", "Three", "One", "Two", "One*");
  61.  
  62. // List Types do not need to be the same
  63. List<Character> list2 = Arrays.asList('d', 'c', 'a', 'b', 'a');
  64.  
  65. System.out.println("======= Before Sort ========");
  66. System.out.println(key);
  67. System.out.println(list1);
  68. System.out.println(list2);
  69. System.out.println();
  70. System.out.println();
  71.  
  72. // Include key in the values parameter if you want it to be sorted too.
  73. concurrentSort(key, key, list1, list2);
  74.  
  75. System.out.println("======= After Sort =========");
  76. System.out.println(key);
  77. System.out.println(list1);
  78. System.out.println(list2);
  79. }
  80. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
======= Before Sort ========
[4, 3, 1, 2, 1]
[Four, Three, One, Two, One*]
[d, c, a, b, a]


======= After Sort =========
[1, 1, 2, 3, 4]
[One, One*, Two, Three, Four]
[a, a, b, c, d]