// permutation 全排列 publicstaticvoidperm(char[] list, int k, int m) { if(k == m) { for(int i=0; i<=m; i++) cout << list[i]; cout << endl; } else { for(int i=k; i<=m; i++) { swap(list[k], list[i]); perm(list, k+1, m); swap(list[k], list[i]); } } } // m 是数组最后一个元素的下标, k 是把数组分为两个部分的指标 // 递归到 m-1 个元素的全排列和 m 个元素全排列的递推关系 // 用循环找递推关系:每次把单独拿出来的元素跟后面全排列中的某一个元素交换顺序
1 2 3 4 5 6 7 8 9
// hanoi tower problem publicvoidmoveDISKs(int n, char fromTower, char toTower, char auxTower) { if (n == 1) Move disk 1 from the fromTower to the toTower; else { moveDISKs(n-1, fromTower, auxTower, toTower); Move disk n from the fromTower to the toTower; moveDISKs(n-1, auxTower, toTower, fromTower); } }
C语言不断调用函数就是不断压栈的过程,每调用一个新函数则新创建一个栈空间(调用构成回路即构成递归)
ADT and OO
ADT:Abstract Data Types 是将类型和与这个类型有关的操作封装在一起的数据模型
OO:
object-oriented = object + class + inherit + communicate
object:attribute values + operates
程序和算法的不同点
Program:is written by languages that can be performed by machine. can not satisfy the finiteness. For example, OS.
Algorithm:has multiple descriptive methods, such as language, graph, table.
Chapter 2
Space Complexity and Time Complexity
Space Complexity:the amount of memory a program needs to run to completion
Time Complexity:the amount of time a program needs to run to completion
Space Complexity
1 2 3 4 5 6 7
// Sequential Search publicstaticintSequentialSearch(int[] a, int x) { int i; for(i=0; i<a.length&&a[i]!=x; i++); if(i == a.length) return -1; return i; }
0 / -1 放不放栈内存? –> 取决于编译器。
total data space:x, i, a[i], 0, -1, a.length –> 6*int [each of them costs 2 bytes]
a数组中数据增加? –> 实际传入的是引用,数据增加并不影响。
S(n) = 0
The best comparison count is one, the worst is n.
The average count for a successful search is:(隐含假设:等概率)
1 2 3 4 5
// recursive code to add a[0: n-1] publicstaticfloatRsum(float[] a, int n) { if(n > 0) return Rsum(a, n-1) + a[n-1]; return0; }
recursion stack space:a, n, 0
depth of recursion:n+1
S(n) = 6(n+1) [6? each of them costs 2 bytes]
PPT上的计算方法:
Time Complexity
1 2 3 4 5 6 7 8 9
// finding the largest number in a[0: n-1] publicstaticintMax(int[] a, int n) { // locate the largest element in a[0: n-1] intpos=0; for(int i=1; i<n; i++) { if(a[pos] < a[i]) pos = i; // key operation } return pos; }
i < n:n次
i++:n-1次
compare time:n-1次
排序算法
选择排序
the number of element move is 3(n-1)
the total number of comparisons is (n-1)+(n-2)+…+3+2+1=n*(n-1)/2
(swap:需要临时存储空间 移动次数按三次计)
1 2 3 4 5 6 7
publicstaticvoidSelectionSort(int[] a, int n) { // sort the n numbers in a[0: n-1] for(int size=n; size>1; size--) { intj= Max(a, size); swap(a[j], a[size-1]); } }
冒泡排序
the number of comparisons is n*(n-1)/2
1 2 3 4 5 6 7 8 9 10
publicstaticvoidBubble(int[] a, int n) { // bubble largest element in a[0: n-1] to right for(int i=0; i<n-1; i++) if(a[i] > a[i+1]) swap(a[i], a[i+1]); } publicstaticvoidBubbleSort(int[] a, int n) { // sort a[0: n-1] using a bubble sort for(int i=n; i>1; i--) Bubble(a, i); }
publicstaticvoidRank(int[] a, int n, int[] r) { // rank the n elements a[0: n-1] for(int i=0; i<n; i++) r[i] = 0; for(int i=1; i<n; i++) { for(int j=0; j<i; j++) { if(a[j] < a[i]) r[i]++; else r[j]++; } } } publicstaticvoidRearrange(int[] a, int n, int[] r) { // in-place rearrangement into sorted order for(int i=0; i<n; i++) { while(r[i] != i) { intt= r[i]; swap(a[i], a[t]); swap(r[i], r[t]); } } }
插入排序
the number of comparisons
the best case is n-1
the worst case is (n-1)*n/2
move number
the best case is 2*(n-1)
the worst case is (1+2) + (2+2) + … + (n-2+2) + (n-1+2) = (n-1)*n/2 + 2*(n-1) =
1 2 3 4 5 6 7 8 9 10 11 12 13
publicstaticvoidInsert(int[] a, int n, int[] x) { // insert x into the sorted array a[0: n-1] int i; for(i=n-1; i>=0&&x<a[i]; i--) a[i+1] = a[i]; a[i+1] = x; } publicstaticvoidInsertionSort(int[] a, int n) { for(int i=1; i<n; i++) { // 此处可以从i=1开始 intt= a[i]; Insert(a, i, t); } }