Explore pointers, dynamic memory allocation, algorithms, and ADTs in C programming. Learn to translate problems into algorithms and create efficient programs. Dive into data encapsulation, abstraction, and abstract data types.
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
F I N
Plus de contenu associé
Transcription de la présentation
CHAPTER 1BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures in C”, 1/e, Computer Science Press, 1992, 2/e,Silicon press, 2008. CHAPTER 1
Pointers • * • dereferencing operator, indirection operator • & • address operator • int i, *pi; • pi = &i; • *pi = 10; or i = 10; CHAPTER 1
Data Structure What is data structure? Ans. Data structure探討一群相關資料的資料表示方 法與資料運作方法 目的 : 使用最有效率的方式,對一群相關資料進行處理 如何設計 : 1. 找出對該資料的各種運算 2. 考慮最適當的Data Structure,使得各種運算的 效率最佳 3. 設計一個完整的Algorithm CHAPTER 1
How to create programs • Requirements: What inputs, functions, and outputs • Analysis: bottom-up vs. top-down • Design: data objects and operations • Refinement and Coding • Verification • Program Proving • Testing • Debugging CHAPTER 1
Algorithm • DefinitionAn algorithm is a finite set of instructions that accomplishes a particular task. • Criteria • Input: zero more quantities are externally supplied. • Output: at least one quantity is produced. • definiteness: clear and unambiguous • finiteness: terminate after a finite number of steps • effectiveness: instruction is basic enough to be carried out CHAPTER 1
Algorithm • One distinguishes between an algorithm and a program, the latter of which does not have to satisfy the fourth condition (Finiteness). • For example, we can think of an operating system that continues in a wait loop until more jobs are entered. CHAPTER 1
Translating a Problem into an Algorithm • Problem • Devise a program that sorts a set of n>= 1 integers • Solution I • From those integers that are currently unsorted, find the smallest and place it next in the sorted list • Solution II • for (i= 0; i< n; i++){ Examine list[i] to list[n-1] and suppose that the smallest integer is list[min]; Interchange list[i] and list[min]; } CHAPTER 1
Translating a Problem into an Algorithm • Solution III #define swap(x,y,t) ((t)= (x), (x)= (y), (y)= (t)) 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); } 上面swap意思即類似 void swap(int *x, int *y) { int temp= *x; *x= *y; *y= temp; } CHAPTER 1
Data Type • Data TypeA data type is a collection of objects and a set of operations that act on those objects. • Example of "int" • Objects: 0, +1, -1, ..., Int_Max, Int_Min • Operations: arithmetic(+, -, *, /, and %), testing (equality/inequality), assigns, functions CHAPTER 1
Data encapsulation and Data abstraction • Data encapsulation or Information hiding是把資料物件的內部程式碼內容隱藏不被外部的使用者知道 目的 : 1. 讓往後可能的修改能夠局限在此程式單元之中 2. 讓程式單元不需隨著ADT內部的表示方式的改 變而修改外部的使用方式3. 可提高程式的可重用度,可讀性, 方便除錯… • Data abstraction是將一個資料物件的specification和 implementation 分離 目的 : 只描述主要特徵而隱藏大部分的次要特徵,可以將複 雜的物件加以簡化 CHAPTER 1
Abstract Data Type • Abstract Data TypeAn abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations. • ADT Operations — only the operation name and its parameters are visible to the user — through interface • Why abstract data type ? • implementation-independent • 不管如何(How)作,只管作了什麼(What) • Abstraction is … • Generalization of operations with unspecified implementation CHAPTER 1
Classifying the Functions of a Data Type • Creator/constructor • Create a new instance of the designated type • Transformers • Also create an instance of the designated type by using one or more other instances • Observers/reporters • Provide information about an instance of the type, but they do not change the instance CHAPTER 1
*ADT 1.1:Abstract data type Natural_Number (p.20)structure Natural_Number is (denoted by Nat_No)objects: an ordered subrange of the integers starting at zero and ending at the maximum integer (INT_MAX) on the computerfunctions: for all x, y Nat_Number; TRUE, FALSE Boolean and where +, -, <, and == are the usual integer operations.Nat_No Zero ( ) ::= 0Boolean Is_Zero(x) ::= if (x) returnFALSEelse returnTRUENat_No Add(x, y) ::= if ((x+y) <= INT_MAX) return x+y else returnINT_MAXBoolean Equal(x,y) ::= if (x== y) returnTRUEelse returnFALSENat_No Successor(x) ::= if (x == INT_MAX) return xelse return x+1Nat_No Subtract(x,y) ::= if (x<y) return 0else return x-yend Natural_Number Creator Observer Transformer CHAPTER 1
Recursive Algorithms • Direct recursion • Functions call themselves • Indirect recursion • Functions call other functions that invoke the calling function again • Any function that we can write using assignment, if-else, and while statements can be written recursively. CHAPTER 1
Recursive algorithm • How to determine that express an algorithm recursively ? • The problem itself is defined recursively • Statements: if-else and while can be written recursively • How to design recursive algorithm 1. 找出遞迴關係 • 找出遞迴關係才能夠對問題進行切割 2. 找出終止條件 • 找出終止條件避免無窮遞迴下去 非常重要!! CHAPTER 1
Binary Search int binsearch(int list[], int searchnum, int left, int right) {// search list[0]<= list[1]<=...<=list[n-1] for searchnum 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;} int compare(int x, int y) { if (x< y) return -1; else if (x== y) return 0; else return 1; } CHAPTER 1
Recursive Implementation of Binary Search int binsearch(int list[], int searchnum, int left, int right) {// search list[0]<= list[1]<=...<=list[n-1] for searchnum 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; } CHAPTER 1
河內塔程式碼 void Hanoi(int n, char x, char y, char z) { if (n > 1) { Hanoi(n-1,x,z,y); printf("Move disk %d from %c to %c.\n",n,x,z); Hanoi(n-1,y,x,z); } else { printf("Move disk %d from %c to %c.\n",n,x,z); } } CHAPTER 1
Hanoi (n , x , y , z) n=4 的結果 執行 Hanoi(3,x,z,y). 執行 Hanoi(2,x,y,z). 執行 Hanoi(1,x,z,y). 列印 Move disk 1 from x to y. 列印 Move disk 2 from x to z. 執行 Hanoi(1,y,x,z). 列印 Move disk 1 from y to z. 列印 Move disk 3 from x to y. 執行 Hanoi(2,z,x,y). 執行 Hanoi(1,z,y,x). 列印 Move disk 1 from z to x. 列印 Move disk 2 from z to y. CHAPTER 1
執行 Hanoi(1,x,z,y). 列印 Move disk 1 from x to y. 列印 Move disk 4 from x to z. 執行 Hanoi(3,y,x,z). 執行 Hanoi(2,y,z,x). 執行 Hanoi(1,y,x,z). 列印 Move disk 1 from y to z. 列印 Move disk 2 from y to x. 執行 Hanoi(1,z,y,x) 列印 Move disk 1 from z to x. 列印 Move disk 3 from y to z. 執行 Hanoi(2,x,y,z). 執行 Hanoi(1,x,z,y). 列印 Move disk 1 from x to y. 列印 Move disk 2 from x to z. 執行 Hanoi(1,y,x,z). 列印 Move disk 1 from y to z. CHAPTER 1
Performance Analysis (machine independent) • Space and Time • Does the program efficiently use primary and secondary storage? • Is the program's running time acceptable for the task? • Evaluate a program generally • Does the program meet the original specifications of the task? • Does it workcorrectly? • Does the program contain documentation that show how to use it and how it works? • Does the program effectively use functions to create logical units? • Is the program's code readable? CHAPTER 1
Performance Analysis(Cont.) Practice Practice Practice • Evaluate a program Meet specifications, Work correctly, Good user-interface, Well-documentation, Readable, Effectively use functions, Running time acceptable, Efficiently use space/storage • How to achieve them? • Good programming style, experience, and practice • Discuss and think Think Think Think CHAPTER 1
S(P)= c+ Sp(I) P: a program I: instance (input, output) • The space needed is the sum of • Fixed space and Variable space • Fixed space: c • Includes the instructions, variables, and constants • Independent of the number and size of I/O • Variable space: Sp(I) • Includes dynamic allocation, functions' recursion • Total space of any program • S(P)= c+ Sp(Instance) CHAPTER 1
*Program 1.10: Simple arithmetic function (p.23)float abc(float a, float b, float c){ return a + b + b * c + (a + b - c) / (a + b) + 4.00; }*Program 1.11: Iterative function for summing a list of numbers (p.24)float sum(float list[ ], int n){ float tempsum = 0; int i; for (i = 0; i<n; i++) tempsum += list [i]; return tempsum;} Sabc(I) = 0 Ssum(I) = 0 Recall: pass the address of the first element of the array & pass by value CHAPTER 1
*Program 1.12: Recursive function for summing a list of numbers (p.24)float rsum(float list[ ], int n){ if (n) return rsum(list, n-1) + list[n-1]; return 0; }*Figure 1.1: Space needed for one recursive call of Program 1.11 (p.25) (以16bit x86 small/near為例) Ssum(I)=Ssum(n)=6n * CHAPTER 1
Time Complexity • Total time • T(P)= compile time + run (or execution) time • May run it many times without recompilation. Run time • How to evaluate? • + - * / … (最細的估計) • Use the system clock 直接用量的比較省事 • Number of steps performed (中等的估計) • machine-independent • Instance及所費時間函數的趨勢 (粗略的估計, p.33) • Definition of a program step • A program step is a syntactically or semantically meaningful program segment whose execution time is independent of the instance characteristics CHAPTER 1
Methods to compute the step count • Introduce variable count into programs • Tabular method • Determine the total number of steps contributed by each statement • steps_per_statement * frequency_for_that_statement • s/e, steps/execution • add up the contribution of all statements CHAPTER 1
*Program 1.13: Program 1.11 with count statements (p.26)float sum(float list[ ], int n){ float tempsum = 0; count++; /* for assignment */ int i; for (i = 0; i < n; i++) {count++; /*for the for loop */ tempsum += list[i]; count++; /* for assignment */ }count++; /* last execution of for */count++; /* for return */ return tempsum; } Iterative summing of a list of numbers 2n + 3 steps CHAPTER 1
*Program 1.15: Program 1.12 with count statements added (p.27)float rsum(float list[ ], int n){count++; /*for if conditional */ if (n) {count++; /* for return and rsum invocation */ return rsum(list, n-1) + list[n-1]; }count++; return list[0];} Recursive summing of a list of numbers 2n+2 steps CHAPTER 1
*Program 1.16: Matrix addition (p.28)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]; } Matrix addition CHAPTER 1
*Program 1.17: Matrix addition with count statements (p.29)void add(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ][MAX_SIZE], int row, int cols ){ int i, j; for (i = 0; i < rows; i++){count++; /* for i for loop */ for (j = 0; j < cols; j++) {count++; /* for j for loop */ c[i][j] = a[i][j] + b[i][j];count++; /* for assignment statement */ }count++; /* last time of j for loop */ }count++; /* last time of i for loop */} 2rows * cols + 2 rows + 1 CHAPTER 1
*Program 1.18: Simplification of Program 1.17 (p.29)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++)count += 2;count += 2; }count++;} 2rows cols + 2rows +1 Suggestion: Interchange the loops when rows >> cols CHAPTER 1
Asymptotic Notation(O, , ) • Exact step count • Compare the time complexity of two programs that computing the same function • Difficult task of most of programs • Asymptotic notation • Big “oh” • upper bound(current trend) • Omega • lower bound • Theta • upper and lower bound CHAPTER 1
Asymptotic Notation O • Definition • f(n)= O(g(n)) iff there exist positive constants c and n0 such that f(n)<= cg(n) for all n, n>= n0 • Examples • 3n+ 2= O(n) as 3n+ 2<= 4n for all n>= 2 • 10n2+ 4n+ 2= O(n2) as 10n2+ 4n+ 2<= 11n2 for n>= 5 • 3n+2<> O(1), 10n2+ 4n+ 2<> O(n) • Remarks • g(n) is upper bound, the least? (估algorithm, 作答時…) • n=O(n2)=O(n2.5)= O(n3)= O(2n) • O(1): constant, O(n): linear, O(n2): quadratic, O(n3): cubic, and O(2n): exponential (各舉一例) CHAPTER 1
Asymptotic Notation • Definition • f(n)= (g(n)) iff there exist positive constants c and n0such that f(n)>= cg(n) for all n, n>= n0 • Examples • 3n+ 2= (n) as 3n+ 2>= 3n for n>= 1 • 10n2+ 4n+ 2= (n2) as 10n2+4n+ 2>= n2 for n>= 1 • 6*2n+ n2= (2n) as 6*2n+ n2 >= 2n for n>= 1 • Remarks • lower bound, the largest ? (used for problem) • 3n+3= (1), 10n2+4n+2= (n); 6*2n+ n2= (n100) • Theorem • If f(n)= amnm+ ...+ a1n+ a0 and am> 0, then f(n)= (nm) CHAPTER 1
Asymptotic Notation • Definition • f(n)= (g(n)) iff there exist positive constants c1, c2, and n0 such that c1g(n)<= f(n) <= c2g(n) for all n, n>= n0 • Examples • 3n+2=(n) as 3n+2>=3n for n>1 and 3n+2<=4n for all n>= 2 • 10n2+ 4n+ 2= (n2); 6*2n+n2= (2n) • Remarks • Both an upper and lower bound • 3n+2!=(1); 10n2+4n+ 2!= (n) • Theorem • If f(n)= amnm+ ... +a1n+ a0 and am> 0, then f(n)= (nm) CHAPTER 1
Example of Time Complexity Analysis Statement Asymptotic complexity void add(int a[][Max.......) 0 { 0 int i, j; 0 for(i= 0; i< rows; i++) (rows) for(j=0; j< cols; j++) (rows*cols) c[i][j]= a[i][j]+ b[i][j]; (rows*cols) } 0 Total (rows*cols) CHAPTER 1
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.