Data is stable (that is, insert and delete operations are not allowed)
At any given moment, the algorithm can give the answer to the sub-sequence problem for the data it has read, and the algorithm with this feature is called online algorithm.
Divide and Conquer (Divide-and-conquer) strategy: The idea is to divide the problem into two roughly equal sub-problems and then solve them recursively, which is the "fractional" part. The "cure" stage merges the solution of two sub-problems together and may do a little additional work, and finally get the solution of the whole problem.
When writing a recursive routine, it is important to remember that the recursive four Basic Law is:
Baseline scenario. Some reference cases can be solved without recursion
continue to advance. Each recursive must push the solution state toward the baseline situation
Design rules. Assume that all recursive calls can run
The law of synthetic benefit (compound interest rule). Do not perform repetitive work in different recursive invocations when solving the same instance of a problem
If an algorithm uses constant Time (O (1)) to reduce the size of the problem to its part (usually 1/2), then the algorithm is O (LOGN). On the other hand, if you use constant time just to reduce the problem by a constant (such as reducing the problem by 1), then the algorithm is O (N).
Program 1: The number of k in the output array
#include <stdio.h> #define size 100int main () { int i,j,k,t,n, num[size],tmp[size]; //the original array num as follows printf ("Please input the array size (n) : "); &NBSP;SCANF ("%d", &n); printf ("Please input the array (including %d integer) :\n ", n); for (i=0;i<n;++i) { &NBSP;SCANF ("%d", &num[i]); } //output the kth number in num printf ("Please input the number k (rank k ' Th in the descending order array): "); &NBSP;&NBSP;&NBSP;SCANF ("%d", &k); tmp[0]=num[0]; //put The number num[i] into tmp[t],in tmp,the order is descending for (i=1;i <k;++i) { for (j=i-1;j>=0;--j) { if (Num[i] <=TMP[J]) { break; } else { tmp[j+1]=tmp[j]; } } t=j+1; tmp[t]=num[i]; } //insert Num[i] into tmp[j+1] when num[i]>tmp[k] for (i=k;i<n;++i ) { for (j=k-1;j>=0;--j) { if (num[i]<=tmp[j ]) break; else tmp[j+1]=tmp[j ]; } if (j+1<=k-1) { tmp[j+1]=num[i]; } } printf ("FOLLOWING&NBSP;THE&NBSP;DESCENDING&NBSP;ORDER,THE&NBSP;%DTH&NBSP;NUMBER&NBSP;IS&NBSP;: %d\n ", k,tmp[k-1]); return 0;}
Program 2: All data in the output file
#include <stdio.h> #include <stdlib.h>int main () {FILE *fp=fopen ("/usr/include/stdio.h", "r+"); char* Buffer=malloc (sizeof (char) *65535); if (fp==null) {printf ("FP is NULL"); return 0; } while (Fread (buffer,sizeof (char), 65535,FP)!=0) {printf ("%s", buffer); } printf ("\ n"); Fclose (FP); return 0;}
Program 3: Output maximum sub-sequence and. The following 3 algorithms, when the data volume is very large, the running time decrements
Algorithm 1:
#include <stdio.h> #define size 10int main () { int num[size]; int i,j; int tmp,sum=0; int start=0,end=0; printf ("Please input the element of the array[%d] :\n ", SIZE); for (i=0;i<size;++i) &NBSP;&NBSP;&NBSP;SCANF ("%d", &num[i]); for (i=0;i<size;++i) { tmp=0; for (J=I;J<SIZE;++J) { tmp+=num[j]; if (sum<tmp) { start=i; sum=tmp; end=j; } } } printf ("the max_sublist_sum is %d\n", sum); for (i=sTart;i<end;++i) printf ("%d ", Num[i]) printf ("%d\n", Num[end]); return 0;}
algorithm 2:
#include <stdio.h> #define &NBSP;SIZE&NBSP;10INT&NBSP;MAXSUBSUM (int array[],int left,int right) { int maxleftsum,maxrightsum,maxcentersum; int maxleftbordersum=0,maxrightbordersum=0; int leftbordersum=0,rightbordersum=0; int center,i; if (left==right) if (array[left]>0) return array[left]; else return 0; center= (left+right)/2; maxleftsum= Maxsubsum (Array,left,center); maxrightsum=maxsubsum (array,center+1,right); for (i=center;i>=left;--i) { leftbordersum+= Array[i]; if (maxleftbordersum<leftbordersum) maxleftbordersum= Leftbordersum; }&nBsp; for (i=center+1;i<=right;++i) { rightbordersum+= Array[i]; if (maxrightbordersum<rightbordersum) maxrightbordersum= rightbordersum; } maxcentersum= Maxrightbordersum+maxleftbordersum; return maxleftsum>maxrightsum? (maxleftsum>maxcentersum?maxleftsum:maxcentersum):(maxrightsum>maxcentersum?maxrightsum:maxcentersum); }int main () { int array[size],left=0,right=size-1,i; printf ("please input the element of the array[%d] :\n", SIZE); for (i=0;i<size;++i) { scanf ("%d", &array[i]); } printf ("THE&NBSP;MAX_SUBLIST_SUM&NBSP;IS&NBSP;: %d\n ", Maxsubsum (array,left,right)); /* for (i=left;i<right;++i) printf ("%d ", Array[i]); printf ("%d\n ", Array[right]); */ return 0;}
Algorithm 3:
#include <stdio.h> #define SIZE 10int Main () {int thissum=0,maxsum=0,i; int array[size]; printf ("Please input the element of the array[%d]: \ n", SIZE); for (i=0;i<size;++i) {scanf ("%d", &array[i]); } for (I=0;i<size;++i) {thissum+=array[i]; if (thissum>maxsum) maxsum=thissum; else if (thissum<0) thissum=0; } printf ("The Max_sublist_sum is:%d\n", maxsum); return 0;}
Program 4: Pair lookup (binary search)
#include <stdio.h> #define size 10int main () { int array[size],i , goal,left=0,right=size-1,mid; printf ("Please input the element of the array[%d]:\n ", SIZE); for (i=0;i<size;++i) { &NBSP;SCANF ("%d", &array[i]); } printf ("Please input the goal : "); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d ", &goal); while (left<=right) { mid= (left+right)/2; if (Array[mid] >goal) right=mid-1; else if (array[mid]<goal) left=mid+1; else { printf ("THE&NBSP;INDEX&NBSP;IS&NBSP;: %d\n ", mid); return 0; } } printf ("Not find&nbsP;the goal\n "); return 0;}
Program 5: Euclidean algorithm for calculating the maximum common factor
#include <stdio.h>gcd (unsigned int m,unsigned int N) {unsigned int Rem; while (n>0) {rem=m%n; M=n; N=rem; } return M;} int main () {unsigned int m,n; printf ("Please input M and N:"); scanf ("%d%d", &m,&n); printf ("The GCD is:%d\n", GCD (M,n)); return 0;}
Program 6: Power operation
#include <stdio.h>long int Pow (long int x,unsigned int N) {long int tmp; if (n==0) return 1; else if (n==1) return x; Tmp=pow (X*X,N/2); if (n%2!=0) tmp*=x; return TMP; }int Main () {printf ("Please input the X and N:"); long int x; unsigned int N; scanf ("%ld%u", &x,&n); printf ("The result is:%ld\n", Pow (X,n)); return 0;}
Program 7: Calculate the probability of two randomly selected and less than or equal to the reciprocal positive integer of the NN
#include <stdio.h> #define NN 6unsigned int Gcd (unsigned int m,unsigned int N) {unsigned int Rem; while (n>0) {rem=m%n; M=n; N=rem; } return M;} int main () {int rel=0,tot=0,j,i; for (I=1;i<=nn;++i) for (j=i+1;j<=nn;++j) {tot++; if (GCD (i,j) ==1) rel++; } printf ("The probability of it that they was Prime is:%f\n", (rel+0.0)/tot); return 0;}
"Data structure and algorithm analysis--c language description" post-reading notes