Data Str & Algorithm HW Solutions 21

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

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

View Full Document Right Arrow Icon
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++)
Background image of page 1
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

This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online