Data Structures & Alogs HW_Part_6

Data Structures & Alogs HW_Part_6 - 21 binary...

Info iconThis preview shows pages 1–2. 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++) 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;
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

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

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online