Small algorithms, big ideas, with the best code to illustrate the coding style

Source: Internet
Author: User

Two_arg.cpp
Title: Two ordered arrays for common elements (assuming two ascending arrays).
Requirements: Time complexity: O (n), space complexity s (1).
Algorithm Description: One traversal, two pointers starting to point to two arrays respectively. Loop controls whether to the end of two arrays
Each loop compares the size of the element pointed to by two pointers, moves the small pointer back, and, if found to be equal, has a common element.
Output results.
//**********************************************
Code Description:

2015-5-26

//**********************************************

#include <iostream>
using namespace Std;
#define MAX 7

BOOL MyComp (const int *ARG1, const int *ARG2)//Here the parameter is const int* when your function is not in the address
{//element when modifying operations, note that control permissions do not cause errors, and
int I, J; More secure, robust and easy to understand.
if (NULL! = arg1&&null! = arg2)//Here The IF condition is more efficient than a condition where the frequency of execution is relatively high
{                                            //
for (i = 0, j = 0; i<max&&j<max;)
{
if (Arg1[i] < arg2[j])
++i; This is used ++i instead of i++, because i++ produces intermediate variables in memory
else if (arg1[i] = = Arg2[j])//The value of the expression being present, and the value of I. And ++i only exists the value of I, which is
return true; The value of I is the value of an expression.
Else
++j;
}
return false;
}
Else
{
cout << "Err in args" << Endl;
Exit (0);
}
}
int main (void)
{
int Arg1[max] = {1, 4, 6, 7, 8, 9, 10};
int Arg2[max] = {2, 3, 5, 12, 23, 44, 55};

if (MyComp (Arg1, arg2))//The return value here is bool, the return value can implement a chain expression
cout << "Be same number" << Endl;
Else
cout << "No same number" << Endl;
return 0;
}

Very simple algorithm, each step with the best and most efficient way to solve the small points of knowledge involved in it is a lot,
The author himself learned, met, think of the finishing out, some of the places are insufficient or no mention of the welcome guidance, thank you.
Of course, if it is really just such a small program can be written into the main function, the most efficient, but it is a habit,
is also a style, we can not blindly pursue efficiency, ignoring the language of the robustness, reusability, scalability. Of course
Can also be said to say, just such a piece of code, efficiency and improve, will not have any impact. This argument is undeniable.
is right, in order to better work later, and to be a good programmer, it is necessary to understand clearly and must
Develop habits. The style of code determines the height of development .......

Small algorithms, big ideas, with the best code to illustrate the coding style

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.