diff --git a/DataStructureAndAlgorithm/01 绪论.md b/DataStructureAndAlgorithm/01 绪论.md new file mode 100644 index 0000000..808f726 --- /dev/null +++ b/DataStructureAndAlgorithm/01 绪论.md @@ -0,0 +1,389 @@ +# + +֪ʶṹ + +![֪ʶṹ](https://img-blog.csdnimg.cn/20200906115502350.png) + + +--- +## һʲôݽṹ + +1绰뱡IJѯ⡣ + + +$$ +(a_1,b_1),(a_2,b_2),\dots,(a_n,b_n) +$$ + +$a_i$ʾ$b_i$ʾ绰룬$i=1,2,\dots,n$ + +˼ + +- ֯ݣ +- 洢ݣ +- ݣ + - Ӳ + - ɾ + - ޸IJ + - ѯ + - +- ʵ֣ + +![](https://img-blog.csdnimg.cn/20200906101144586.png) + + +2ѧȻѯ⡣ + +˼ + +- ֯ݣ +- 洢ݣ +- ݣ + - Ӳ + - ɾ + - ޸IJ + - ѯ + - +- ʵ֣ + +![](https://img-blog.csdnimg.cn/20200906101406453.png) + +**ݽṹĶ壨Data Structure** + +ݽṹоݵ߼ṹ洢ṹṹԼ֮ĹϵֽṹӦ㣬ƳӦ㷨 + + +ʽݽṹһԪ + + +$$ +Data\_Struct=(D,R) +$$ + + +У$D$Ԫص޼ϣ $R$$D$Ϲϵ޼ϡ + +3Ͻṹ + +$Set=(D,R)$ У$D=\lbrace item_1,item_2,item_3,item_4,itme_5 \rbrace$$R=\lbrace \rbrace$ Ԫ֮ûйϵ + +磺 +- Pythonе `Set` +- C#е `HashSet` + +4Խṹ + +$Line=(D,R)$У$R=\lbrace ,,, \rbrace$β㣬ֻһֱǰһֱӺ̣ + +磺 +- Numpy е`ndarray` +- C#е +- Matlabеľ + +--- +## + +**1ݣData** + +ָвķżϡ + +ֵݣʵҲǷֵݣַ֡ͼΡȡ + + + +**2ԪأData Element** + +ݵĻλҲΪ㣬㣬¼ + +ڼУͨΪһпǺʹ + + +**3Data Item** + +жСʶλҲΪֶΣField + +磺ݿϢϵͳУݱеһ¼һԪء¼еѧѧšԱ𡢼ᡢ¡ɼֶξ + +ɼԪɣԪɡ + +**4߼ṹLogic Structure** + +֮߼ϵڼ + +![߼ṹ](https://img-blog.csdnimg.cn/20200906104744948.png) + + +**5洢ṹStorage Structure** + +߼ṹڼ洢еʵ֣ڼеĴŷʽ + +- ˳ݽд洢 +- ʽָݽд洢 +- λݵĴ洢 +- ɢУɢкԪعؼλݵĴ洢 + + +**6ͣData Type** + +ݵȡֵΧͶݽвܺ͡ + +磺intlongfloatdoubleboolchar + + + +**7ͣAbstract Data Type** + +һԼڸϵһɡ + +͵ĸʽ£ + +```python +ADT Name is + Data + ɸóĻ + Operations + Constructor + Initial Valuesֵ + Pressʼ + Operation1 + Input1Ҫûֵ + PreConditionϵͳִв1ǰ״̬ + Press1Ľ˵ + Output1󷵻ص + PostConditionϵͳִв1״̬ + Operation2 + + OperationN + +End ADT Name +``` + +5νṹADT + +```python +ADT Rectangle is + Data + float Length + float Width + Operations + Constructor + Initial ValuesʱͿֵ + Pressʼ󣬸εijͿֵ + SetLength + InputLengthֵ + PreCondition + Pressεijֵ޸Ϊֵ + Output + PostConditionεijֵ޸ + SetWidth + InputWidthֵ + PreCondition + Pressοֵ޸Ϊֵ + Output + PostConditionεĿֵ޸ + GetArea + Input + PreConditionεijȺͿȶ + Pressõε + Outputؾֵ + PostCondition + GetPerimeter + Input + PreConditionεijȺͿȶ + Pressõεܳ + Outputؾεܳ + PostCondition +End ADT Rectangle +``` + +һ˾νṹADTͿôʵˡ + +```c +public class Rectangle +{ + public float Length; + public float Width; + public Rectangle(float length, float width) + { + Length = length > 0 ? length : 0f; + Width = width > 0 ? width : 0f; + } + public void SetLength(float length) + { + Length = length > 0 ? length : 0f; + } + public void SetWidth(float width) + { + Width = width > 0 ? width : 0f; + } + public float GetArea() + { + if (Length * Width == 0) + throw new ArgumentOutOfRangeException("ȻΪ㡣"); + return Length * Width; + } + public float GetPerimeter() + { + if (Length * Width == 0) + throw new ArgumentOutOfRangeException("ȻΪ㡣"); + return (Length + Width) * 2; + } +} +``` + +--- +## 㷨㷨 + + +**1㷨Ļ** + +1.1 㷨Ķ + +Ϊ˽ijضһ򣬼ijЩֵֵļΪ벢ijЩֵֵļΪһϵ㲽衣 + +1.2 㷨Ҫ + +- ԣFinity޵ʱɡ +- ȷԻ޶ԣUnambiguousnessͬ룬õͬ +- ԣRealizability +- 루Input0롣 +- Output1 + +1.3 㷨ƵҪ + +- ȷԣCorrectness +- ɶԣReadability +- ׳ԣRobustnessкǿݴԱ߽쳣 +- ʱ䣨Running Time +- ռÿռ䣨Storage Spaceɹܵǰ£ʱԽ٣ռԽСԽá + +1.4 㷨 + +- ʽͬ + - 㷨ȻԡαԵ + - 򣺼 +- Ƿ߱ + + +**2㷨** + +2.1 ʱ临ӶȣTime Complexity + +1㷨ķѵʱ + +һ㷨ķѵʱ = 㷨ÿִʱ֮ + +ÿִʱ = ִд$\times$ִһʱ + +һΪÿִһʱǵλʱ䣬һ㷨ķѵʱ = ִƵ֮ + +6㷽$A_{n\times n}\times B_{n \times n}$ 㷨ķѵʱ䡣 + +```c +static double[,] MatrixMultiply(double[,] A, double[,] B, uint n) +{ + double[,] C = new double[n, n]; // 1 + for (int i = 0; i < n; i++) // n+1 + { + for (int j = 0; j < n; j++) // n*(n+1) + { + C[i, j] = 0; // n*n + for (int k = 0; k < n; k++) // n*n*(n+1) + { + C[i, j] += A[i, k] * B[k, j]; // n*n*n + } + } + } + return C; // 1 +} +``` + +$T(n)=2n^3+3n^2+2n+3$ + +2Ĺģ + +㷨һһʾ +- ⣬Ľ +- ͼ⣬ͼĽߵ + +3㷨ʱ临Ӷ $T(n)$ + +Ǹ㷨ʱķѣǹģ$n$ĺ + +$n\to\infty$ʱ$T(n)$ͬ׵ļ򵥺Ϊ㷨Ľʱ临Ӷȡ + +7 $T(n)=2n^3+3n^2+2n+3$ + + +$\displaystyle \lim_{n \to \infty}{\frac{2n^3+3n^2+2n+3} {n^3}=2}$ + + $T(n)=O(n^3)$ + +8һ⣬㷨$A_1$$A_2$ķʱ$T_1(n)=100n^2$$T_2(n)=n^3+n+1$$n\to\infty$ʱ$T_1(n)=O(n^2)$$T_2(n)=O(n^3)$㷨$A_1$ķѵʱԶС㷨$A_2$ںϿͨʱ临Ӷȱʾ㷨ӡ + + +һ㣬ǰѽʱ临Ӷ$T(n)=O(f(n))$Ϊʱ临Ӷȡ$f(n)$ʾ㷨ƵƵ + +9 + +```c +int x = 0; +int y = 0; +for (int i = 0; i < n; i++) + x++; +for (int j = 0; j < n; j++) + for (int k = 0; k < n; k++) + y++; +``` +$T(n)=O(n^2)$ + +10 + +```c +int i = 50; +int j = 60; +int temp = i; +i = j; +j = temp; +``` + +$T(n)=O(1)$ + +4ʱ临Ӷ + +- ף$O(1)$ +- ף$O(log(n))$ +- Խף$O(n)$ +- Զף$O(nlog(n))$ +- ƽף$O(n^2)$ +- ף$O(n^3)$ +- $k$ף$O(n^k)$ +- ָף$O(2^n)$ + + +5ʱ临Ӷ + +11`A[n]`вҸֵ`k` + +```c +static int Find(int[] A, int k) +{ + int i; + for (i = 0; i < A.Length; i++) //(#) + if (A[i] == k) + break; + return i == A.Length ? -1 : i; +} +``` + +- `A`û`k`ȵԪأ(#)Ƶ$f(n)=n+1$ +- `A`е1Ԫ`k`ȣ(#)Ƶ$f(n)=1$ + +ʱ临Ӷȣµʱ临Ӷȡ + +һ㲻ر˵۵ʱ临Ӷȣָʱ临Ӷȡ + + +2.2 ռ临ӶȣSpace Complexity + +ָ㷨ķѵĴ洢ռ䣬Ҳģ$n$ĺռ临ӶҲΪռ临Ӷȡ \ No newline at end of file diff --git a/DataStructureAndAlgorithm/Task01 数组.md b/DataStructureAndAlgorithm/02 数组.md similarity index 100% rename from DataStructureAndAlgorithm/Task01 数组.md rename to DataStructureAndAlgorithm/02 数组.md diff --git a/DataStructureAndAlgorithm/Task02 顺序表和链表.md b/DataStructureAndAlgorithm/03 顺序表和链表.md similarity index 100% rename from DataStructureAndAlgorithm/Task02 顺序表和链表.md rename to DataStructureAndAlgorithm/03 顺序表和链表.md diff --git a/DataStructureAndAlgorithm/Task03 栈与递归.md b/DataStructureAndAlgorithm/04 栈与递归.md similarity index 100% rename from DataStructureAndAlgorithm/Task03 栈与递归.md rename to DataStructureAndAlgorithm/04 栈与递归.md diff --git a/DataStructureAndAlgorithm/Task04 队列.md b/DataStructureAndAlgorithm/05 队列.md similarity index 100% rename from DataStructureAndAlgorithm/Task04 队列.md rename to DataStructureAndAlgorithm/05 队列.md diff --git a/DataStructureAndAlgorithm/Task05 字符串.md b/DataStructureAndAlgorithm/06 字符串.md similarity index 100% rename from DataStructureAndAlgorithm/Task05 字符串.md rename to DataStructureAndAlgorithm/06 字符串.md