Pages

Monday 15 April 2013

Sorting algorithms


Sorting algorithms
Sorting data (placing data in some particular order e.g ascending or descending) will be same no matter which algorithm is used is used. The choice of algorithm should only affect the run time and memory usage of the program.


Selection sort
Is a simple but inefficient sorting algorithm. Th first iteration of the algorithm selects the smallest element and swaps it with first element in the array. The second iteration selects the second smallest item and swaps it with the second element in the array. The algorithm continues until the last iteration selects the second-largest element and replaces it with the second-to-last element, leaving the largest element at the last index of the array.
The selection sort algorithm runs in 0(n2) time.

Merge sort
This is a more efficient but complex sorting algorithm than selection and insertion sorts. The merge sort algorithm sorts an array by splitting an array into two equal sized  sub-array, sorting each sub-array and merging them into one larger array. This results in a pattern that is logarithmic log2 n levels. This results in a total efficiency of 0(n log n)



Tuesday 9 April 2013

Search Algorithms


Two popular search algorithms are linear and binary search.
Linear search algorithm searches each item sequentially to see if the search key is present.

Efficiency of search algorithm

The major thing that differentiate search algorithms is the amount of effort required to complete
the search. This is described by Big 0 notation. How hard an algorithm has to work to solve a
problem.
If the algorithm is independent of the number of elements in the array it is said to have a
constant run time 0(1) this means the number of comparisons are constant - it does not grow
when the size of the array increases.
An algorithm that test whether the first element is equal to any other element in the array will
require n-1 comparisons is 0(n) algorithm having a linear run time pronounced on the order of n
or order n.

Now suppose an algorithm has to test whether any element is duplicated anywhere in the
array. Big 0 is concerned with how an algorithms run time grows in relation to how many items
it processes. Suppose an algorithm requires n2 comparisons. The algorithm is considered to
be 0(n2) referred to as a quadratic run time pronounced on the order of n squared or order
n squared. As n grows you will start to notice performance degradation. The linear search
algorithm can provide great performance if the search element is at or near the front of the
array.

Binary search

This is more efficient than linear search but requires the elements to be sorted first. The
first iteration tests the middle element in the array. If the search key matches the element
the algorithm ends. Assuming the array is sorted, if the search key is less than the middle
element it cannot be in the second half. The algorithm continues with only the first half of the
array. Each iteration tests the middle half of the remaining array if the element does not match,
the algorithm eliminates half again until it finds a match or reduces the sub array to zero.

Efficiency of a binary search

In the worst case scenario searching a sorted array of 1023 elements will require at most 10
comparisons in a binary search. The number 1023 (210 -1) is divided by 2 only 10 times to get
to the value 0. Dividing by 2 is equivalent to one comparison in a binary search. An array of
over one billion elements takes a maximum of 30 comparisons to find the key. A tremendous
improvement on performance over a linear search. The maximum comparison required by a
binary search of a sorted array is the exponent of the first power of 2 greater than the number
of elements in the array which is represented as log2n . Logarithms grow at roughly the same
rate. This results in a big 0 notation of 0(log n) for a binary search or logarithmic run time.



Monday 27 August 2012

Adding Microsoft SQL Server JDBC Driver to your local Maven repository

The Microsoft SQL Server JDBC driver is not available in public repositories because of licensing issues but it is easy to install into into the local repository on your machine. This procedure can be used for other libraries as well.


Extract the file and locate the sqljdbc4.jar.


Download the driver JAR from the link provided by Olaf and add it to your local Maven repository with;


mvn install:install-file -Dfile=sqljdbc4.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0


Modify your POM


<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>sqljdbc4</artifactId>
  <version>4.0</version>
</dependency>

provide your pom the repository of your local machine

<repository>
   <id>local</id>
   <name>local</name>
   <url>file://C:/Users/mywindows/.m2/repository</url>
</repository>


Tuesday 7 August 2012

Generate CSR for Apache-SSL



To generate a CSR on Apache, perform the following steps:
To generate a CSR, a key pair must be created for the server. These two items are a digital certificate key pair and cannot be separated. If the public/private key file or password is lost or changed before the SSL certificate is installed, the SSL certificate will need to be re-issued. The private key, CSR, and certificate must all match in order for the installation to be successful.

The following sequence of commands will generate a 2048 bit key, encrypt it using the triple-DES cipher, and create a CSR based upon it (they assume that openssl is in the path - if not then prefix the openssl command with the path to the binary). It is recommended to use the domain name that will be used for the certificate as the core of the filenames. Also make sure that any existing keys and CSR's are NOT overwritten.


Step 1: Generate Private Key


The utility "OpenSSL" is used to generate the key and CSR. This utility comes with the OpenSSL package and is usually installed under /usr/local/ssl/bin. If the utility was installed elsewhere, these instructions will need to be adjusted accordingly.

Type the following command at the prompt:

openssl genrsa -des3 -out www.yourdomain-example.com.key 2048




This command generates a 2048 bit RSA private key and stores it in the file examplePrivateKey.key.

Note: For all SSL certificates, the CSR key bit length must be 2048.

When prompted for a pass phrase: enter a secure password and remember it, as this pass phrase is what protects the private key. Both the private key and the certificate are required to enable SSL.

NOTE: To bypass the pass phrase requirement, omit the -des3 option when generating the private key. If the private key is left unprotected, RapidSSL recommends access to the server be restricted so that only authorized server administrators can access or read the private key file.

Step 2: Generate the CSR

Type the following command at the prompt:

openssl req -new -key examplePrivateKey.key -out exampleCSR.csr



This command will prompt for the following X.509 attributes of the certificate:




Country Name: Use the two-letter code without punctuation for country, for example: US or CA.

State or Province: Spell out the state completely; do not abbreviate the state or province name, for example: California

Locality or City: The Locality field is the city or town name, for example: Berkeley. Do not abbreviate. For example: Saint Louis, not St. Louis

Company: If the company or department has an &, @, or any other symbol using the shift key in its name, the symbol must be spelled out or omitted, in order to enroll. Example: XY & Z Corporation would be XYZ Corporation or XY and Z Corporation.

Organizational Unit: This field is optional; but can be used to help identify certificates registered to an organization. The Organizational Unit (OU) field is the name of the department or organization unit making the request. To skip the OU field, press Enter on the keyboard.

Common Name: The Common Name is the Host + Domain Name. It looks like "www.rapidssl.com" or "rapidssl.com".

Generate a Certificate Signing Request (CSR) for -Tomcat

Keystore and CSR generation


Generate a private key with the following command:
$JAVA_HOME/bin/keytool -genkey -alias <your_alias_name> -keyalg RSA -keystore <your_keystore_filename> -keysize 2048
You will be prompted for a password. Tomcat uses a default password of "changeit". If you use a different password, you will need to specify a custom password in the server.xml configuration file.
The next field that you will be prompted for is "What is your first and last name?" At this prompt, you must specify the common name (FQDN) of your web site.
You will then be prompted for your organizational unit, organization, etc.

Generate the Certificate Signing Request (CSR):
$JAVA_HOME/bin/keytool -certreq -keyalg RSA -alias <your_alias_name> -file certreq.csr -keystore <your_keystore_filename>
You will now be prompted for the common name, organization, etc. The keytool will use the values that you specify when generating the private key

Create a copy of the keystore file. Having a back-up file of the keystore at this point can help resolve installation issues that can occur when importing the certificate into the original keystore file.

Tuesday 7 February 2012

AppDynamics Lite


AppDynamics is the very first free product designed for troubleshooting java performance while getting full visibility in production environments.