This blog I am going to write some of the algorithms I have seen, although I have seen very little, but I believe there will be more and more to facilitate the future of their own inspection
Okay, here we go.
The most efficient algorithm for solving maximal sub-sequences
1 intMaxsubsequencesum (Const intA[],intN)2 {3 intThissum, Maxsum, J;4 //define the and with maximum and 0 of the current cycle5Thissum = Maxsum =0; 6 //Loop sum7 for(j =0; J < N; J + +)8 {9Thissum + =A[j];Ten //Judging the magnitude of this and the maximum and size, if this and larger than the maximum and large, the value of this and the values assigned to the maximum and One if(Thissum >maxsum) AMaxsum =thissum; - /*if this and less than 0, will this and the assignment is 0 because the addition to less than 0 is definitely not the maximum subsequence and the - assign a value of 0 to the new maximum sub-sequence and comparison*/ the Else if(Thissum <0) -Thissum =0; - } - returnmaxsum; + } - //This algorithm only loops the array once to find the maximum subsequence and
known sorted array, the subscript of a value----> lookup
1 intBinarySearch (Const intA[],intXintN)2 {3 //define the minimum, intermediate, and maximum coordinates4 intLow , Mid, high;5Low =0; High = N-1;6 //cyclic condition minimum coordinate <= maximum coordinate7 while(Low <=High ) {8 //Middle coordinates.9Mid = (low + high)/2;Ten //determines whether the value corresponding to the intermediate coordinate is less than the value to find One if(A[mid] <X) { A /*if less than let the minimum coordinates become intermediate coordinates + 1 - (that is, don't think about the number in front of the intermediate coordinates)*/ -Low = Mid +1; the}Else if(A[mid] >X) { - /*if greater than let the maximum coordinates become intermediate coordinates-1 - (that is, do not consider the number after the intermediate coordinates)*/ -High = Mid-1; +}Else { - //Loop to find coordinates + returnMid;//Found A } at } - return-1;//Not Found -}
Calculate maximum common factor (Euclidean algorithm)
The maximum common factor (GCD) of two integers is the largest integer that is divisible by both: GCD (50, 15) = 5
1UnsignedintGCD (unsignedintM, unsignedintN)2 {3UnsignedintRem;4 while(N >0) {5Rem = M%N;6M =N;7N =Rem;8 }9 returnM;Ten } One //The algorithm continuously calculates the remainder until the remainder is 0, and the last non-0 is the maximum common factor
Efficient power-taking algorithm
1 /*2 if n is an even number, we have X's n-th square = X's N/2 x's n/2-th square3 if n is odd, we have X's N square = x (N-1)/2 x (N-1)/2 X4 */5 Long intPow (Long intXintN)6 {7 if(N = =0) {8 return 0;9 }Ten if(N = =1) { One returnx; A } - ifN2==0) { - returnPow (X*x, n/2); the}Else { - returnPow (X*x, n/2) *x; - } - } + //This algorithm applies recursion to the recursive
Array sort: bubble sort
1 voidSortarray (intA[],intlen)2 {3 for(inti =0; I < len-1; i++) {4 /*5 Each order will determine a number, so you need to cycle len-i times, but because each time is6 adjacent two number comparison, in order to A[J + 1] does not cross over, let J Loop to Len-i-1 stop7 */8 for(intj =0; J < len-i-1; J + +) {9 inttmp;Ten //Exchange A[j] and a[j+1] Two adjacent values if conditions are met One if(A[j] > a[j+1]) { ATMP =A[j]; -A[J] = a[j+1]; -a[j+1] =tmp; the } - } - } -}
Array Sort: Select sort
1 voidSelectsort (intA[],intlen)2 {3 //determine the number of times you want to sort4 for(inti =0; I < Len-1; i++) {5 //each time, we compare one element to the other, and find the minimum value .6 for(intj = i +1; J < Len; J + +) {7 if(A[i] >A[j]) {8 intTMP =A[i];9A[i] =A[j];TenA[J] =tmp; One } A } - } -}
-----------------------------------------------to Be Continued-----------------------------------------------
Hint: 1, code by my hand to play as if there is a mistake please remind me, I will promptly correct
2, comments on behalf of the individual understanding of the ambiguity please advise, thank you
Summary of algorithm based on C language (irregular update)