public List<Integer> quicksort(List<Integer> nums) { if (nums.size() <= 1) return nums; int index = new Random().nextInt(nums.size()); int pivot = nums.get(index); List<Integer> eq = new ArrayList<>(); List<Integer> bigger = new ArrayList<>(); List<Integer> smaller = new ArrayList<>(); for (Integer i : nums) { if (i == pivot) { eq.add(i); } else if (i > pivot) { bigger.add(i); } else { smaller.add(i); } } List<Integer> newList = new ArrayList<>(); newList.addAll(quicksort(smaller)); newList.addAll(eq); newList.addAll(quicksort(bigger)); return newList; }