Data Str &amp; Algorithm HW Solutions 21

# Data Str &amp; Algorithm HW Solutions 21 - for(j=0...

This preview shows page 1. Sign up to view the full content.

21 binary search of the subarray will f nd the position n in an additional log n searches at most, for a total cost in O (log n ) searches. 3.19 Here is a description for a simple Θ( n 2 ) algorithm. boolean Corner(int n, int m, Piece P1, Piece** array) { for (int i=0; i<n; i++) for (int j=0; j<n; j++) { if (compare(P1, array[i][j], LEFT)) return FALSE; if (compare(P1,array[i][j],BOTTOM)) return FALSE; } return TRUE; } void jigsaw(int n, int m, Piece** array) { \\ First, find the lower left piece by checking each \\ piece against the others to reject pieces until one \\ is found that has no bottom or left connection. for (i=0; i<n; i++)
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for (j=0; j<m; j++) if (Corner(n, m, array[i][j], array)) { // Found SWAP(array[i][j], array[0][0]); // Swap pieces break; } \\ Now, fill in row by row, column by column. for (i=0; i<n; i++) for (j=0; j<m; j++) { if (j==0) { // First in row if (i!=0) { // Don’t repeat corner piece for (ii=0; ii<n; ii++) for (jj=0; jj<m; jj++) if (compare(array[i][j], array[ii][jj], TOP)) { tempr = ii; tempc = jj; } SWAP(array[i][j], array[tempr][tempc]); }} else { for (ii=0; ii<n; ii++) for (jj=0; jj<m; jj++) if (compare(array[i][j], array[ii][jj], RIGHT)) { tempr = ii; tempc = jj; }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online