Algorithm Summary based on C Language (occasionally updated), regular update of C language Algorithms
In this blog, I am going to write some algorithms I have seen. Although I have rarely seen them yet, I believe they will become more and more accessible for future reference.
Okay.
The most effective algorithm for solving the maximum subsequence sum
1 int MaxSubsequenceSum (const int A [], int N) 2 {3 int ThisSum, MaxSum, j; 4 // define the sum of the current loop and the sum of the largest and is 0 5 ThisSum = MaxSum = 0; 6 // The sum of the cycle 7 for (j = 0; j <N; j ++) 8 {9 ThisSum + = A [j]; 10 // judge the sum of the current sum and the maximum sum. If the sum and ratio of the current sum are the largest, assign the value of this sum to the largest and 11 if (ThisSum> MaxSum) 12 MaxSum = ThisSum; 13/* if the sum of this sum is less than 0, assign this sum to 0 because it is not the largest subsequence if it is less than 0, and assign it to 0 if it is compared with the largest subsequence */15 else if (ThisSum <0) 16 ThisSum = 0; 17} 18 return MaxSum; 19} 20 // This algorithm calculates the maximum subsequence and
Obtain the subscript of a value for an array that has been sorted ----> split search
1 int BinarySearch (const int A [], int X, int N) 2 {3 // defines the minimum coordinate, intermediate coordinate, maximum coordinate 4 int Low, Mid, High; 5 Low = 0; High = N-1; 6 // minimum coordinate of the cyclic condition <= maximum coordinate 7 while (Low <= High) {8 // 9 Mid = (Low + High)/2 calculated by the Intermediate coordinates; 10 // determine whether the value corresponding to the intermediate coordinate is smaller than the value to be found. 11 if (A [Mid] <X) {12/* If the minimum coordinate is less than 113 (that is, you do not need to consider the number before the intermediate coordinate) */14 Low = Mid + 1; 15} else if (A [Mid]> X) {16/* if the maximum coordinate is greater than or equal to the Center Coordinate-117 (that is, you do not need to consider the number after the Center Coordinate) */18 High = Mid-1; 19} else {20 // cyclically locate the coordinate 21 return Mid; // Found22} 23} 24 return-1; // Not Found25}
Calculate the maximum public factor (Euclidean Algorithm)
The maximum public factor (Gcd) of the two integers is the maximum integer for the division of the two at the same time: Gcd (50, 15) = 5
1 unsigned int Gcd (unsigned int M, unsigned int N) 2 {3 unsigned int Rem; 4 while (N> 0) {5 Rem = M % N; 6 M = N; 7 N = Rem; 8} 9 return M; 10} 11 // The algorithm calculates the remainder continuously until the remainder is 0. The last non-zero remainder is the maximum public factor.
Efficient Power Algorithm
1/* 2 if N is an even number, we have the N power of X = the N/2 power of X * The N/2 power of X. 3 if N is an odd number, we have the nth power of X (N-1)/The second power X (N-1) /2 power * X 4 */5 long int Pow (long int x, int N) 6 {7 if (N = 0) {8 return 0; 9} 10 if (N = 1) {11 return x; 12} 13 if (N % 2 = 0) {14 return Pow (x * x, n/2); 15} else {16 return Pow (x * x, N/2) * x; 17} 18} 19 // This algorithm applies recursion.
Array sorting: Bubble Sorting
1 void sortArray (int a [], int len) 2 {3 for (int I = 0; I <len-1; I ++) {4/* 5 each sort determines a number, so we need to recycle len-I times, but because each sort is a comparison of 6 adjacent two numbers, for a [j + 1] Do not cross-border, let j loop to len-i-1 stop 7 */8 for (int j = 0; j <len-i-1; j ++) {9 int tmp; 10 // if the conditions are met, exchange the values of adjacent numbers a [j] And a [j + 1]. 11 if (a [j]> a [j + 1]) {12 tmp = a [j]; 13 a [j] = a [j + 1]; 14 a [j + 1] = tmp; 15} 16} 17} 18}
Array sorting: Select sorting
1 void selectSort (int a [], int len) 2 {3 // determine the number of times to be sorted 4 for (int I = 0; I <len-1; I ++) {5 // compare an element with other elements at a time, and find the minimum value of 6 for (int j = I + 1; j <len; j ++) {7 if (a [I]> a [j]) {8 int tmp = a [I]; 9 a [I] = a [j]; 10 a [j] = tmp; 11} 12} 13} 14}
------------------------------------------------- Not complete to be continued -----------------------------------------------
Tip: 1. If the code is incorrect, please remind me that I will correct it in time.
2. comments indicate that your personal understanding is ambiguous. Thank you.