Algorithm (i)

Source: Internet
Author: User

first explain the meaning of the following title: on the internet read a Daniel wrote the basic algorithm related to the blog, feel that they are not gifted, so decided to implement it once as a.
Opening :
This blog post today is about array lookups, which is very simple.
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.
Original address

I feel that the above content is better written, so I carry it. The following is a formal implementation of their own version.
1. Array Lookup Function Version 1.0:

intFind_array (intArr[],unsignedLengthConst int& value) {intindex =0;returnIndex;} Test:#include <iostream>#include <assert.h>//There is a return subscript, otherwise return-1 indicates that the lookup failed. intFind_array (intArr[],unsignedLengthConst int& value) {intindex =0;returnIndex;}Static voidTest1 () {intarr[Ten] = {0}; ASSERT (Find_array (nullptr,Ten,1)==-1); ASSERT (Find_array (arr,0,1) == -1);}intMain () {test1 ();} The test can see the wrong program is not robust, you need to modify the entrance.

Version 2.0:

int Find_Array(intlengthconstint& value){    iflength0)        return -1;    int0;    return index;}//可以看到我们对入口进了测试。

Version 3.0:

intFind_array (intArr[], std::size_tlength, the constint& value) {if(arr = = Nullptr | |length==0)return-1; for(std::size_tIndex=0;Index<length; ++Index){if(arr[Index] = = value)return Index;Continue; }return-1;} static void Test1 () {intarr[Ten] = {1,2}; ASSERT (Find_array (nullptr,Ten,1)==-1); ASSERT (Find_array (arr,Ten,1) ==0);}

4.0 optimized version:

1) is optimized with pointers:intFind_array (intArr[], std::size_tlength, the constint& value) {if(arr = = Nullptr | |length==0)return-1; for(std::size_tIndex=0;Index<length; ++Index){if(*(arr+Index) = = value)return Index;Continue; }return-1;}

2) Optimization of formal parameters:

using Array=int(&) [Ten];a reference to the//array type. intFind_array (ArrayArrConst int& value) {if(arr = =nullptr)return-1; for(STD:: size_t index =0; Index <Ten; ++index) {if(Arr[index] = = value)returnIndexContinue; }----------C + + One:using Array=int(&) [Ten];a reference to the//array type. intFind_array (ArrayArrConst int& value) {intindex =0; for(Const Auto& X:arr) {if(x = = value)returnIndex ++index;Continue; }return-1;}voidTest () {intarr[Ten] = {1,2}; ASSERT (Find_array (arr,Ten) == -1); ASSERT (Find_array (arr,2) ==1);}//We omitted the parameter of the array size, but there are some limitations in the above notation. ----------3) Template function Version:Template<classT>intFind_array (Constt*Const&arr,Const STD:: size_t length,Constt& value) {if(arr = =nullptr|| Length = =0)return-1;AutoBeg = arr, end = beg + length; while(Beg! = end)if(*beg++ = = value)returnbeg-arr-1;return-1;}voidTest () {intarr[Ten] = {1,2}; ASSERT (Find_array (arr,Ten,1) ==0); ASSERT (Find_array (arr,Ten,3) == -1);}

Related instructions:
1. Why use size_t? Avoid array lengths of less than 0.
2. Optimization of parameters: try to optimize with const reference, pinching irritate D.
3. Test platform: VS 2013+win 7.

Two times optimized version:

Recently learned a bit about the content of the standard library, so it is implemented with the array type defined by the vector and Standard Edition.

#include <iostream>#include <assert.h>#include <vector>#include <array>//There is a return subscript, otherwise return-1 indicates that the lookup failed. //vector version. intFind_array (Const STD:: vector<int>& Ivec,Const int& value) {//Do not use the subscript version, use the iterator directly.     AutoBeg = Ivec.begin (), end = Ivec.end ();if(Beg = = end)return-1;intindex =0; while(Beg! = end)if(*beg++==value)returnBeg-ivec.begin ()-1;return-1;}voidTest1 () {STD:: vector<int>Ivec = {1,2,3}; ASSERT (Find_array (Ivec,2)==1); ASSERT (Find_array (Ivec,Ten) == -1); ASSERT (Find_array (STD:: vector<int>(),Ten) == -1);}//array version of the standard library. intFind_array (Const STD:: Array<int,10>& Arr,Const int& value) {if(Arr.empty ())return-1;intindex =0; for(Const Auto& X:arr) {if(x = = value)returnIndex ++index;Continue; }return-1;}voidTest2 () {STD:: Array < int, >arr= {1,2,3}; ASSERT (Find_array (arr,2) ==1); ASSERT (Find_array (arr,Ten) == -1); ASSERT (Find_array (STD:: Array<int,10>(),Ten) == -1);}intMain () {test1 ();    Test2 (); System"Pause");return 0;} ----------///Try a variable parameter list. intFind_array (Const STD::initializer_list<int>& I1,Const int& value) {AutoBeg = I1.begin (), end = I1.end ();if(Beg = = end)return-1;intindex =0; while(Beg! = end)if(*beg++ = = value)returnBeg-i1.begin ()-1;return-1;}voidTest3 () {Assert (Find_array (STD::initializer_list<int>{1,2,3},1) ==0); ASSERT (Find_array (STD::initializer_list<int> (),Ten) == -1);}

Summary:

1. The idea of the algorithm is very simple, I just idle to rewrite it again. The lookup involves operations such as traversing, counting, and so on basic operations.
2. Learn to optimize the algorithm in a step-by-step approach.
3. Widen the usability of the algorithm.
4. Ensure robustness.
5. Post reference: The original address, poke me in

Algorithm (i)

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.