## Time complexity in algorithms

Which Algorithm Is Better?

The fastest-possible running time for any runtime analysis is O(1), commonly referred to as constant

running time. An algorithm with constant running time always takes the same amount of time

to execute, regardless of the input size. This is the ideal run time for an algorithm, but it’s rarely

achievable.

The performance of most algorithms depends on n, the size of the input. The algorithms can be classified

as follows from best-to-worse performance:

➤➤ O(log n) — An algorithm is said to be logarithmic if its running time increases logarithmically

in proportion to the input size.

➤➤ O(n) — A linear algorithm’s running time increases in direct proportion to the input size.

➤➤ O(n log n) — A superlinear algorithm is midway between a linear algorithm and a polynomial

algorithm.

➤➤ O(nc) — A polynomial algorithm grows quickly based on the size of the input.

➤➤ O(cn) — An exponential algorithm grows even faster than a polynomial algorithm.

➤➤ O(n!) — A factorial algorithm grows the fastest and becomes quickly unusable for even

small values of n.

Filed under algorithms, C++, Uncategorized

## Ajax

index.php

<html>

function findmatch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('results').innerHTML = xmlhttp.responseText;
}
}

xmlhttp.open('GET', 'search.inc.php', true);
xmlhttp.send();

}

<body>

<form id="search" name="search">
Type a name:<br>
<input type="text" name="search_text" onkeydown="findmatch();">
</form>

</body> </html>

search.inc.php

<?php

if(isset(\$_GET['search_text'])) {
\$search_text = \$_GET['search_text'];
echo \$search_text;
}

?>

Filed under Uncategorized

## PHP-2

Server variable-

1.  _SERVER[‘SCRIPT_NAME’]  => will show current page name on which this fun called
2. _SERVER[‘HTTP_HOST’] => Show server IP.
3. _SERVER[‘REMOTE_ADDR’]  => Non reliable  way to get user’s IP.
4. _SERVER[‘HTTP_CLIENT_IP] => Give router ip if there is router other wise client IP.
5. _SERVER[‘HTTP_X_FORWARDED_FOR] => If user using proxy, it will give proxy IP.

2. <?php ob_start();?>  …… <?php ob_end_flush/clean();

Htmlentites-

Use htmlentites( \$_GET[‘var’]) for security purpose other wise user can input dangerous code. when you are displaying data get from POST and GET methods always use HTMLENTITES() function.

Sessions-

set.php

1. <?php
2. session_start ();
3. \$_SESSION[‘name’] = ‘amit’;
4. ?>

view.php

1. <?php
2. session_start();
3. echo \$_SESSION[‘name’];
4. ?>

unset.php

1. <?php
2. session_start();
3. unset( \$_SESSION[‘name’]); // only delete single session with has name ‘name’
4. session_destroy();  //this will delete all session data for current user
5. ?>

Cookie- session destroy when user close client(browser) while cookie can set for years.

2. view.php

• example.xml
<producers>
<producer>
<name> Alex </name>
</producer>
<producer>
<name> amit </name>
</producer>
</producers>
• index.php
1. <?php
3. echo \$xml->producer[1]->name;
4. //or
5. foreach (\$xml->producer as \$producer) {
6.      echo \$producer->name;
7. ?>

Connect php with Mysql-

2. mysql_select_db(‘mysql_db_name’);
3. Search a query in php-
• \$query = select `name` from `table_name` order by `id`;
• mysql_query(\$query);
•  mysql_error();  //will return error from last query.

Example-

connect.php
<?php
\$connect_error = 'Could not connect!';

\$mysql_host = "localhost";
\$mysql_name = 'root';
\$mysql_pass = '';

\$mysql_db = 'amit_db';
if(!mysql_connect(\$mysql_host, \$mysql_name, \$mysql_pass) || !mysql_select_db(\$mysql_db)
{
die(\$connect_error);
}
?>

<?php
required 'connect.php';

\$query =  "select `name` from  `user_table`  order by `id`";
if( \$query_run = mysql_query(\$query) )
{
while(\$query_row = mysql_fetch_assoc(\$query_run)
{
\$name = \$query_row['name']'
echo " user name is \$name ";
}
}
else {
echo mysql_error();
}

Filed under Uncategorized

## PHP

Array Functions II

Another common thing to do with arrays is sort them. Handily enough, PHP has a sort() function for just such an occasion!

\$array = array(5, 3, 7, 1);
sort(\$array);
print join(", ", \$array);
// prints "1, 3, 5, 7"

PHP also has the opposite function:rsort().

\$array = array(5, 3, 7 ,1);
rsort(\$array);
print join(":", \$array);
// prints "7:5:3:1"
Lastly, we use join(glue, array) so we can easily print out the representations of our sorted arrays in this exercise.

Function exercise –

Create an array() with the names of your closest family and friends. The more names in your array, the more fun your game will be! After you’ve created your list, sort it and randomly select a name from the list.  When you have your winning name,print it to the screen in caps so everyone knows how awesome the winner is.

Functions we’ll use here: array(),array_push(), sort(), count(),rand(), and strtoupper(). Play around with some of the other functions you’ve learned as well.

PHP String function –

\$stringInLowerCase = strtolower(string);

 strstr() Finds the first occurrence of a string inside another string (case-sensitive)
 strtolower() Converts a string to lowercase letters
 substr_replace() Replaces a part of a string with another string

 substr_ireplace() Replaces a part of a string with another string ignoring case

we can also use array of string inside substr_replace to replace

Filed under Uncategorized

## Observer design pattern-

Q- If  subject’s state changes to frequently how will you modify subject so that he notify observer only on an important event.

A- Use the setChanged type of method for more details check Head first design pattern page 78 (104/681).

Q- How different design principle apply on observer design pattern?

A-

Observer Design Patter and OOP Principles

## Decorator design pattern-

Decorator design pattern is based on a class should be closed for modification and open for extension. and uses composition to achieve runtime Power to decorate an object.

A disadvantage of Decorator is  sometimes it add a lot of small subclasses in design and this occasionally results in a design that is less straightforward for others to understand.

Filed under Uncategorized

## Dependency –

A dependency typically (but not always) implies that an object accepts or receive a reference or object to a class as part of  as a method parameter, instantiates, or uses another object. And a modification in accepted class may break current class as well. Dependency is represented by dashed arrow starting from dependent class to its dependency. A dependency is very much implied by an association.

## Association –

its represent the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable althogh it might also be implemented as a method argument, or the creation of a local variable. In simple words, it  describes how many elements are taking part in an interaction.

## Aggregation –

Aggregation and composition are two types of association. Aggregation differs from ordinary composition in that it don’t imply ownership. In composition when the owning object is destroyed, so are the contained objects.

Filed under Uncategorized

## Difference between c and C++

1. C is procedural language and C++ is procedural and oop language.
2. C does not support public, private way of data hiding C++ support while in C you can do some-type of implementation hiding by opaque data structure.
3. C does not support namespace while C++.
4. C does not have exception handling while c++ support it.
5. C is not a OOP language so it does not support  inheritance, encapsulation, polymorphism, virtual function ,operator overloading.
6. C does not have constructor destructor concepts.
7. C does not have RAII or smart Pointer mach.
8. C does not have template.
9. In C default linkage of const variable is external while in C++ it is internal linkage that’s why you can  create array of const specific size. below code will work only in c++.
1. const int size =10;
2. int arr[size]
10. C++ support inline function.
11. C++ support different STL containers.