# Algorithm Analysis

Algorithm Efficiency
is used to describe properties of an algorithm relating to how much of various types of resources it consumes

Criteria of Algorithm Efficiency
Space Utilization – the amount of memory required to store the data Time Efficiency – the amount of time required to process the data

Algorithm Execution Time Factors
Size of the input – the number of input items affects the time required for the process to be completed - Example: the time it takes to sort a list of items depends on the number of items in the list. Thus, the execution time T of an algorithm must be expressed as a function T(n) of the size n of the input. The kinds of intruction & the speed with which the machine can execute the intructions - the value of T(n) cannot be express in real time units, instead by the approximate count of the intructions executed Quality of the source code that implements the algorithm & the quality of the source code generated by the compiler from the source code

Algorithm to Calculate a Mean
/* Algorithm to find the mean of x[0],...,x[n-1] */ 1. 2. 3. 4. 5. Initialize sum = 0 Initialize index variable i = 0 While i < n do the following: A. Add x[i] to the sum B. Increment i by 1 end while 6. Calculate and return mean = sum / n *note: statements 1 and 2 are each executed 1 time, statement 4 & 5 which comprise the body of the while loop are each executed n times, and statement 3 which controls the repetition is executed n + 1 times, since 1 additional check is required to detemine that the control variable i is no longer less than the value of n. Statement 6 is executed 1 time.

Summarized Analysis
Statement 1 2 3 4 5 6 Total # of time executed 1 1 N+1 N N 1 3n + 4

Conclusion
The computing time for the algorithm to calculate a mean is given by T(n) = 3n + 4 So as the number of input increases, the value of T(n) at a rate proportional to n, so we say the T(n) has “order of magnitude n” T(n) is O(n)

Proving ALgorithms Correct
It is a deductive proof of a program’s correctness - consist of: Precondition – Pre(the “Given”) Postcondition – Post(the “To show:”)

Example
/* Algorithm to find the mean of x[0],...,x[n-1] */ 1. 2. 3. 4. 5. Initialize sum = 0 Initialize index variable i = 0 While i < n do the following: A. Add x[i] to the sum B. Increment i by 1 end while 6. Calculate and return mean = sum / n Pre: Input consist of an integer n ≥ 1 and an array x of n real numbers. Post: Execution of the algorithm will terminate, and when it does, the value of the variable mean is the mean(average) of x[0],..., x[n-1]

Note:
To demonstrate that the postcondition Post follows from the precondition Pre and the execution of the algorithm,one usually introduces, at several points in the algorithm, intermediate assertions about the state of processing when execution reaches these points. For the preceding algorithm we might use an additional intermediate assertion at the bottom of the while loop that will be true each time execution reaches this point. Such assertion is called loop invariant.

Loop Invariant
Pre: Input consist of an integer n ≥ 1 and an array x of n real numbers. 1. 2. 3. 4. 5. Initialize sum = 0 Initialize index variable i = 0 While i < n do the following: A. Add x[i] to the sum B. Increment i by 1 a. The value of sum is the sum of the first i elements of array x and i is the number of times execution has reached this point end while 6. Calculate and return mean = sum / n Post: Execution of the algorithm will terminate, and when it does, the value of the variable mean is the mean(average) of x[0],..., x[n-1]

Standard Algorithm in Java
In java it is know as “standard library” or “java class library” (JCL) the library that is conventionally made available in every implementation of that language. In some cases, the library is described directly in the programming language specification; in other cases, the contents of the standard library are determined by more informal social practices in the programming community.

Standard Library Includes
Subroutines Macro definitions Global variables Class definitions Templates Most standard libraries include definitions for at least the following commonly used facilities: Algorithms (such as sorting algorithms) Data structures (such lists, trees and hash tables) Interaction with the host platform, including input/output and operating system calls

Java Class Library (JCL)
is a set of dynamically loadable libraries that Java applications can call at runtime. The Java Class Library is almost entirely written in Java itself, except the parts that need to have direct access to the hardware and operating system (as for I/O, or Graphic Rasterisation). The Java classes that give access to these functions commonly use java native interface (JNI) wrappers to access the native API of the operating system.

Purposes of JCL within the Java Platform
Like other standard code libraries, they provide the programmer a well-known set of functions to perform common tasks, such as maintaining lists of items or performing complex string parsing. In addition, the class libraries provide an abstract interface to tasks that would normally depend heavily on the hardware and operating system. Tasks such as network access and file access are often heavily dependent on the native capabilities of the platform. Finally, some underlying platforms may not support all of the features a Java application expects. In these cases, the class libraries can either emulate those features using whatever is available, or provide a consistent way to check for the presence of a specific feature.

Java Platform

Main Feature of JCL
Features of the Class Library are accessed through classes grouped by packages. java.lang contains fundamental classes and interfaces closely tied to the language and runtime system. I/O and networking: access to the platform file system, and more generally to networks, is provided through the java.io, java.nio, and java.net packages. Mathematics package: java.math provides regular mathematical expressions, as well as arbitrary-precision decimals and integers numbers. Collections and Utilities : provide built-in Collection data structures, and various utility classes, for Regular expressions, Concurrency, logging and Data compression.

Main Feature of JCL (cont)
GUI and 2D Graphics: the java.awt package supports basic GUI operations and binds to the underlying native system. It also contains the 2D Graphics API. The javax.swing package is built on AWT and provides a platform independent widget toolkit, as well as a Pluggable look and feel. It also deals with editable and non-editable text components. Sound: provides interfaces and classes for reading, writing, sequencing, and synthesizing of sound data. Text: the java.text package deals with text, dates, numbers, and messages. Image package: java.awt.image and javax.imageio provide APIs to write, read, and modify images. XML: built-in classes handle SAX, DOM, StAX, XSLT transforms, XPath, and various APIs for Web services, as SOAP protocol and JAX-WS. CORBA and RMI APIs, including a built-in ORB

Main Feature of JCL (cont2)
Security and Cryptography Databases: access to SQL databases is provided through the java.sql package. Access to Scripting engines: the javax.script package gives access any Scripting language that conforms to this API. Applets: java.applet allows applications to be downloaded over a network and run within a guarded sandbox Java Beans: java.beans provides ways to manipulate reusable components.

END