{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Data Structures &amp; Alogs HW_Part_6

# Data Structures &amp; Alogs HW_Part_6 - 21 binary...

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

21 binary search of the subarray will fi 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++) 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; }

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.

{[ snackBarMessage ]}