data_CHAP01 -  m? / / Data Structures By Hyun-Ju Park /...

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

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

Unformatted text preview:  m? / / Data Structures By Hyun-Ju Park / Hanbat National University / / 6 6 / / / / / 55 55 5 ù ù ù  ú / / / / 55 55 55 5 / 6 6 / k 6 /  6 /  / / / 5 5 à [ / / 55 5 / / / / / Data Structures By Hyun-Ju Park / Hanbat National University /  } ­ ™  ' à  !!  (requirements) (analysis) (design) û¾ a (refinement and coding) (verification) / / Data Structures By Hyun-Ju Park / Hanbat National University /  ›û − − ›   b  j   ›  bø ã ã \ ( )   û r − : − : ú ú  ú ú ›  › › ?  û›  øó › ` › − − j à \ › › \j [é  o ú ú (abstract data type) éà  ¾ é\ [ › ó ›  / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 Ö j − j [ [ à à ø ! \  û` / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  1.2.1  } : ÷ : ”× \   û   o  ã \  ›  ã ñ  ó› 0[ § ô  ã \ ø :  : ñ ñ! : :  ó› é  ó›  ( ) { ñ    \ PQFSBUJOH TZTUFN / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 ø −  ñ ã G › − ø $$ 1BTDBM 4QBSD GMPXDIBSU  à +BWB …  ñ ¾  / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  n ≥ 1[ 1 − 2 − :  ›  _ § ˜\   › ø ý  ª   “˜ :  ó û   øó ˜o  ô ø [\ ” À, list o for ( i = 0; i < n; i++ ) { list[i] list[min] } list[n - 1]   øó › “ ; ø; a list[i]é list[min] / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 3 − :à  , ø void sort ( int list, int n ) { int i, j, min, temp; for ( i = 0; i < n – 1; i++ ) { min = i; for ( j = i + 1; j < n; j++) if (list[j] < list[min]) min = j; SWAP(list[i], list[min], temp); /* temp = list[i]; list[i] = list[min]; list[min] = temp; */ } } 4 − : ›  7 ~ 8, ›  1.3 : ˜ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  n ≥ 1[ − 1 −   ›      § ˜  À list   j i§ ˜ | searchnum  ó -1 list \ a \à  list[i] = searchnum :à  left, right ø \ À í,  ú  |(   ó left = 0, right = n - 1) ó› right ← middle - 1 ó› left ← middle + 1 middle = (left + right) / 2› searchnum < list[middle] ⇒ searchnum searchnum = list[middle] ⇒ middle searchnum > list[middle] ⇒ searchnum list[middle + 1] ó  middle list[right]  list[left]é list[middle - 1] − 2 searchnum   a : searchnum list[middle] ( “ #3) while (there are more integers to check) { middle = (left + right) / 2; if (searchnum < list[middle]) right = middle – 1; else if (searchnum == list[middle]) return middle; else left = middle + 1; á– a  o searchnum list[middle] / / Data Structures By Hyun-Ju Park / Hanbat National University / 3 :à   ( “ #4) int binsearch(int list, int searchnum, int left, int right) { int middle; while (left <= right) { middle = (left + right) / 2; switch (COMPARE(list[middle], searchnum)) { case –1: left = middle + 1; break; case 0: return middle; case 1: right = middle – 1; } } return -1; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 1.2.2 §  (recursive mechanism) ú − ú −  ù  § b  \ §  ø ø û ø ó›  \  GBDUPSJBM  n! = n × (n - 1)! à \ ø ó›  \  MJTU CJOBSZ USFF / / Data Structures By Hyun-Ju Park / Hanbat National University / *UFSBUJWF CJOBSZ TFBSDI BMHPSJUIN      § ˜ª ]  int binsearch(int list, int searchnum, int left, int right) { int middle; while (left <= right) { middle = (left + right) / 2; switch (COMPARE(list[middle], searchnum)) { case –1: left = middle + 1; break; case 0: return middle; case 1: right = middle – 1; } } return -1; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 3FDVSTJWF CJOBSZ TFBSDI BMHPSJUIN     ˜ § ª ]  int binsearch(int list, int searchnum, int left, int right) { int middle; if (left <= right) { middle = (left + right) / 2; switch (COMPARE(list[middle], searchnum)) { case –1: return binsearch(list, searchnum, middle + 1, right); case 0: return middle; case 1: return binsearch(list, searchnum, left, middle – 1); } } return -1; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   ø  & éà  ó  ›  \ § \ ` & j JOU − − (data type) ó [ (objects) [  ø \  (operations)  { 0, +1, -1, +2, -2, …, INT_MAX, INT_MIN }  ø : +, -, *, /, % [› s − − à [    ?  § ñ  ã  \  & j oj (abstract data type, ADT) ó [ é [  [  ó› o ó› /BUVSBM /VNCFS / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 /BUVSBM /VNCFS    _     /BUVSBM /VNCFS  ª ]  struct Natural_Number objects : ‘0 o functions : Nat_No ,   x, y,  Boolean , +, –, <,  ==\ ÷   TRUE, FALSE  . ø  . (INT_MAX)  ::= 0 ::= if (x) return FALSE else return TRUE ::= if ((x + y <= INT_MAX) return x + y else return INT_MAX Boolean Equal(x, y) ::= if (x == y) return TRUE else return FALSE Nat_No Successor(x) ::= if (x == INT_MAX) return x else return x + 1 Nat_No Subtract(x, y) ::= if (x < y) return 0 else return x - y end NaturalNumber Nat_No Zero() Boolean Is_Zero() Nat_No Add(x, y) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    − é !a −  \ (performance analysis) (performance measurement)  ›  é \? b ø \ ? › ›  ›  b  ÷  , ‚  b § ? \ § ! ó› › \?   \? / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 à › ø   é ›  § ! ó› ó ÷  ? \?   › (space complexity)  \j ä  › (time complexity)  \j  ä / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 1.4.1 œA  ,c − −  − : − û ,  ›  , Sp(I) (I) , ? \  \ §   › \ , ( , ` ) \ , ⇒ P S(P) = c + SP(I) , S(P) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6      §  ª  float abc(float a, float b, float c) return (a + b + b * c + (a + b - c) / (a + b) + 4.00); } 3[ §  Sabc(I) = 0 ó›  ` / / Data Structures By Hyun-Ju Park / Hanbat National University /       s    ª  float sum(float list, int n) { float tempsum = 0; int i; for (i = 0; i < n; i++) tempsum += list[i]; return tempsum; } ó› À À − −  \ ó› À  → Àó   ô ›  b Pascal :  : call by value  : À  Ssum(I) = Ssum(n) = n C  : Ssum(I) = Ssum(n) = 0 / / Data Structures By Hyun-Ju Park / Hanbat National University /       s  §  ª  float rsum(float list, int n) { if (n) return (rsum(list, n - 1) + list[n - 1]); return 0; } → [  ,   , o| − 2[ [ ×  +  = (4byte(list) + 4byte(n) + 4byte(  )) = 12byte   §  ã = n × 12byte / / Data Structures By Hyun-Ju Park / Hanbat National University / 1.4.2 ­A P T(P) = TP(n)  ÷ ,n: +  , T( P) (TP) TP  − ·  ›  b › ? / / Data Structures By Hyun-Ju Park / Hanbat National University / È › › › \ ­ \   \ äó \j \ ­ ó›` \ ó› ä › ­   㠛  count“\  § ` \   ›  § ›   z“ › › `  z“  \ ` § bà ã \ û  o / / Data Structures By Hyun-Ju Park / Hanbat National University / 6            DPVOU c  aW  ª ]   void add (int a[MAX_SIZE], int b[MAX_SIZE], int c[MAX_SIZE]), int rows, int cols) { int i, j; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) c[i][j] = a[i][j] + b[i][j]; } void add (int a[MAX_SIZE], int b[MAX_SIZE], int c[MAX_SIZE]), int rows, int cols) { int i, j; for (i = 0; i < rows; i++) { count++; for (j = 0; j < cols; j++) { count++; c[i][j] = a[i][j] + b[i][j]; count++; } count++; } count++; } Data Structures By Hyun-Ju Park / Hanbat National University / 6 / /        }  s/e ª 0 0 0  Ù } 0 0 0 rows+1 rows⋅cols+rows rows ⋅ cols 0 void add (int a[MAX_SIZE], …) { int i, j; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) c[i][j] = a[i][j] + b[i][j]; } 0 0 0 1 1 1 0 rows+1 rows⋅(cols + 1) rows ⋅ cols 0 2rows ⋅ cols + 2rows + 1 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  − ×  s  o \   [   −  −  [   o \   [  § \  \ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 1.4.3  [ ø › (3n + 3) Vs. (100n + 10)  − ù (break-even point) c3n › ù c1n2 + c2n ›  § \ \n  (c1n2 + c2n) Vs. (c3n) … big oh omega theta  (asymptotic notations) : O(n) : Ω(n) : Θ(n) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 Big “oh” upper bound f(n) = O(g(n)) −  n, n ≥ n0 f(n) ≤ cg(n) f(n) = O(nm)  \rä cé n0  f(n) = amnm + ... + a1n + a0 3n + 2 = O(n) 1000n2 + 100n – 5 = O(n2) 6 × 2n + n2 = O(2n) 10n2 + 4n – 5 = O(n4) O(1) O(logn) O(n) O(nlogn) O(n2) O(n3) O(2n) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 Omega lower bound f(n) = Ω(g(n)) −  n, n ≥ n0 f(n) ≥ cg(n)  \rä cé n0  f(n) = amnm + ... + a1n + a0, am > 0 f(n) = Ω(nm) 3n + 2 = Ω(n) 10n2 + 4n + 2 = Ω(n2) = Ω(n) = Ω(1) 3n + 2 = Ω(n) 6 × 2n + n2 = Ω(2n) = Ω(n100) = Ω(n50.2) = Ω(n2) = Ω(n) = Ω(1) / / Data Structures By Hyun-Ju Park / Hanbat National University / Theta f(n) = Θ(g(n)) −  n, n ≥ n0 c1g(n) ≤ f(n) ≤ c2g(n) f(n) = Θ(nm)  \rä c1, c2é n0  f(n) = amnm + ... + a1n + a0, am > 0 3n + 2 = Θ(n) ≠ Θ(1) / / Data Structures By Hyun-Ju Park / Hanbat National University /       ­e ª   void add (int a[MAX_SIZE], …) { int i, j; for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) c[i][j] = a[i][j] + b[i][j]; } 0 0 0 Θ(rows) Θ(rows ⋅ cols) Θ(rows ⋅ cols) 0 Θ(rows ⋅ cols) / / Data Structures By Hyun-Ju Park / Hanbat National University / int binsearch(int list, int searchnum, int left, int right) { int middle; while (left <= right) { middle = (left + right) / 2; switch (COMPARE(list[middle], searchnum)) { case –1: left = middle + 1; break; case 0: return middle; case 1: right = middle – 1; } } return -1; } = while   n − −   : : : :  Θ(1)   1 1 2i-1 = n  list ­ n/4 n / (22) ó› n/8  … n / (23) i = log2n + 1 By Hyun-Ju Park / Hanbat National University n/2 n / (21) 1 … n / (2i-1) = 1 i\  i -1= log2n / / Data Structures /     - NBHJD TRVBSF ª  15 16 22 3 9 n ÷ |   í ã :  1 8 14 20 21 2 1 7 13 19 25 24 5 6 12 18 17 23 4 10 11 11 (by Coexter) \ ø 1  ø ó›, íó›  ó ` ó û÷ ó› § \ − ó› è“ ó› ` \ ø ø ` ó  › ¾ó› / / Data Structures By Hyun-Ju Park / Hanbat National University / 6      - ª  for (i = 0; i < size; i++) for (j = 0; j < size; j++) square[i][j] = 0; square[0][(size - 1) / 2] = 1; /*    1 \ */ i = 0; j = (size - 1) / 2; for (count = 2; count <= size * size; count++) { row = (i – 1 < 0) ? (size – 1) : (i – 1); /* › */ column = (j – 1 < 0) ? (size – 1) : (j – 1); /* íó› */ if (square[row][column]) i = (++i) % size; /* › */ else { i = row; j = (j – 1 < 0) ? (size – 1) : –– j ; } square[i][j] = count; } /* */ printf(“Magic Square of size %d : \n\n”, size); for (i = 0; i < size; i++) { for (j = 0; j < size; j++) printf(“%5d”, square[i][j]); printf(“\n”); } ù −   for  for › ù = Θ(n2) ù = Θ(n2) ù = Θ(n2) r  for ù = Θ(n2) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 1.4.4      sª ]  n ­A 1 log n n nlogn n2 n3 2n n! }« l l 1 1 0 1 0 1 1 2 1 2 1 1 2 2 4 8 4 2 4 1 2 4 8 16 64 16 24 8 1 3 8 24 64 512 256 40326 16 1 4 16 64 256 4096 65536 20922789888000 32 1 5 32 160 1024 32768 4294967296 26313 26313 × 1033 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   › , ¾ ÷,  \ \  z“  (clocking function) UJNF :   44, ›  1.23   %  › ‚ \j ú  j ã  j §  \à  ã     ø j § \ \ − \ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6 ...
View Full Document

Ask a homework question - tutors are online