Review Problems for AP CS Test
public static void thing(int[] a) { int low = 0; int high = a.length - 1; while (high > low) { for (int i = low; i < high; i++) { if (a[i] > a[i+1]) { swap(a, i, i+1); } } high--; for (int i = high; i > low; i--) { if (a[i] < a[i-1]) { swap(a, i, i-1); } } low++; } }If a contains n elements, what is Big-Oh of this sorting algorithm? (Do you know what this algorithm is called?)
* / \ 6 - / \ 5 2
public class Main { public static void main(String[] args) { System.out.println(mystery(2,12)); } public static long mystery(long a, long b) { return mysteryHelper(a, b, 1); } public static long mysteryHelper(long a, long b, long n) { if (b == 0) { return n; } else if (isEven(b)) { return mysteryHelper(a*a, b/2, n); } else { return mysteryHelper(a, b-1, a*n); } } public static boolean isEven(long b) { return (b%2 == 0); } }
ArrayList al = new ArrayList(); al.add(new Integer(10)); System.out.println((Integer) (al.get(0)).intValue());
ArrayList al = new ArrayList(); al.add(new Integer(10)); System.out.println(((Integer) (al.get(0))).intValue());