C++1

Source: Internet
Author: User

The algorithm is the life of the computer. No algorithm, there is no software, the computer has become a cold machine, there is no practical value. Many people think that arithmetic is the content of mathematics, it is particularly troublesome to learn. We cannot think of this view as wrong. But we also know that software is a composite technology, if a person only know the algorithm, but can not be implemented in a good programming language, then the best algorithm can not play a role. A person only has the very good computer knowledge and the mathematics knowledge, can in the algorithm study unceasing progress. Regardless of the algorithm is simple, you have to practice their own hands, only to constantly understand the mistakes, and constantly find mistakes, can continue to improve their programming ability, and constantly improve their business level.

Here the name of a step-by-step writing algorithm for the purpose of the main two: first, to ensure that our algorithms are everyone can learn to understand, and secondly, to ensure that our algorithm is robust, can be tested. So in the process of writing, our algorithm development process is accompanied by the increase in test cases, no test case guarantees that the code is just a random character, no value.

In fact, any algorithm has its own application environment and application scenarios, no algorithm can be applied to all scenarios. I hope members will understand this point. At the same time, we also need to understand that the complex algorithms are made up of ordinary algorithms, there is no ordinary algorithm can not be a complex algorithm, so the complexity of the simple, from the Dahua small, which is the algorithm of the basic idea of the recursive treatment.

We can start with a function that looks up the following array. In one sentence, let's start with the simplest function constructs:

int find (int array[], int length, int value) {int index = 0;return Index;}

As seen here, the lookup function is just a normal function, so the first thing to judge is the legitimacy of the argument:

static void Test1 () {int array[10] = {0};assert (false = = Find (NULL, ten, Ten)); assert (false = = Find (array, 0, 10));}

As can be seen here, we do not judge the legitimacy of the parameters, then the original lookup function should be how to modify it?

int find (int array[], int length, int value) {if (NULL = = Array | | 0 = = length) return false;int index = 0;return Index;}

See the code above to show that we have already judged the entry parameters. So here's the code to start writing.

int find (int array[], int length, int value) {if (NULL = = Array | | 0 = = length) return false;int index = 0;for (; index < L Ength; index++) {if (value = = Array[index]) return index; return FALSE;}

The above code is nearly complete, so how do you write the test case?

static void Test2 () {int array[10] = {1, 2};assert (0 = = Find (array, 1)), assert (FALSE = = Find (array, 10, 10));}

After running through all the test cases, let's see where the original code can be optimized. In fact, we can turn arrays into pointers.

int find (int array[], int length, int value) {if (NULL = = Array | | 0 = = length) return false;int* start = array;int* end = AR Ray + length;while (Start < end) {if (value = = *start) return ((int) start-(int) array)/(sizeof (int)); start + +;} return FALSE;}

What if the above code parameter must be a generic data type?

Template<typename type>int Find (type array[], int length, type value) {if (NULL = = Array | | 0 = = length) return false;t ype* start = array;type* End = array + length;while (Start < end) {if (value = = *start) return ((int) start-(int) array)/(s Izeof (type)); start + +;} return FALSE;}

Does the test case need to be re-modified at this point?

static void Test1 () {int array[10] = {0};assert (false = = Find<int> (NULL, ten)); assert (false = = Find<int> (ar Ray, 0, 10));} static void Test2 () {int array[10] = {1, 2};assert (0 = = find<int> (array, ten, 1)); assert (FALSE = = find<int> (arra Y, 10, 10));}


So here's a summary:

(1) Our algorithm requires validation of test cases

(2) Any optimization should be based on the test.

(3) test and code writing to be done synchronously

(4) The successful operation of the algorithm, step-by-step, the success of each step must be established in the original success

"Trailer: Next introduction loop and recursion"

C++1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.