/************************************************* * Author: Morrison * Date: 04 Feb 202022 **************************************************/ import java.util.ArrayList; import java.util.Collections; public class Sordid { public Sordid() { } public static void main(String[] args) { ArrayList al = new ArrayList<>(); int n = Integer.parseInt(args[0]); for(int k = 0; k < n; k++) { al.add(k); } Collections.shuffle(al); System.out.println(al); trickle(al); System.out.println(al); } public static void swap(ArrayList x, int j, int k) { T tmp = x.get(j); x.set(j, x.get(k)); x.set(k, tmp); } public static > void trickle(ArrayList x) { for(int p = 1; p <= x.size(); p++) { for(int n = p - 1; n >= 1 && x.get(n).compareTo(x.get(n-1)) < 0; n--) { swap(x, n, n-1); } } } //x and y are SORTED lists. combine them into a single //sorted list and return it public static > ArrayList zipper(ArrayList x, ArrayList y) { // this is O(n) ArrayList out = new ArrayList<>(); int px = 0; int py = 0; while(px < x.size() && py < y.size()) { if(x.get(px).compareTo(y.get(py) < 0) { out.add(x.get(px)); px++; } else { out.add(y.get(py)); py++; } } out.addAll(x.subList(px, x.size())); out.addAll(y.subList(py, y.size())); } }