This preview shows pages 1–2. Sign up to view the full content.

Answer to Final CS100J, Fall 2005 NAME David Gries NET ID djg page 1 Question 1. /** Let x be the value currently in b[h]. Permute b[h. .k] and return an int j that satisfies b[h. .j-1] <= b[j] = x < b[j+1. .k]. */ public static int partition( int [] b, int h, int k) { int j= h; int t= k; /* inv: b[h. .j-1] <= b[j] = x <= b[t+1. .k] */ while (j < t) { if (b[j+1] <= b[j]) { Swap b[j+1] and b[j]; j= j+1; } else { Swap b[j+1] and b[t]; t= t-1; } } return j; } Question 2. int p= h; int q= p; while (q <= k) if (b[q] % 2 != 0) { b[p]= b[q]; p= p + 1; } q= q + 1; } Question 3a. h __a1__ 3b. /** see exam for spec */ public static void swap( int [][] b, int n, int h, int k, int p, int q) { for ( int r= 0; r != n; r= r+ 1) { for ( int c= 0; c != n; c= c+1) { // Swap b[h+r][k+c] with b[p+r][q+c] int temp= b[h+r][k+c]; b[h+r][k+c]= b[p+r][q+c]; b[p+r][q+c]= temp; } } } Question 4. /** = a string containing n occurrences of char c. Precondition: n

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.