CTRL+L: Criteria For Algorithm
CTRL+L: Criteria For Algorithm
a finite set of instructions that specify a sequence of operations to perform specific task.
recipe for solvingCtrl+l
a problem
Ctr2
Criteria for Algorithm
1. Input are the data items presented to the algorithm. An algorithm had either no input or a predetermined number of
items.
2. Output – are data items presented to the outside world as the result of the execution of a program based on the
algorithm. An algorithm ought to produce at least one output.
3. Definiteness – each instruction must be clear and simple.
4. Finiteness – all instructions of an algorithm will terminate after a finite number of steps.
5. Effectiveness – each instruction has an impact on the overall process.
---
Data Structures
o Array
o Linked list
o Stacks
o Trees
o Sorting
Pseudocode
Flowchart
Is a graphic representation of how a process works, showing, at a minimum, the sequence of steps.
Once the flowchart is drawn, it becomes easy to write the program in any high-level language.
Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program.
The algorithm and flowchart include following three types of control structures.
1. Sequence: In the sequence structure, statements are placed one after the other and the execution takes place starting
from up to down.
2. Branching (Selection): In branch control, there is a condition and according to a condition, a decision of either TRUE or
FALSE is achieved. In the case of TRUE, one of the two branches is explored; but in the case of FALSE condition, the other
alternative is taken. Generally, the ‘IF-THEN’ is used to represent branch control.
IF -Else Statement – is a conditional statement that provides the computer with a choice between two options
The algorithm and flowchart include following three types of control structures.
3. Loop (Repetition): The Loop or Repetition allows a statement(s) to be executed repeatedly based on certain loop
condition e.g. WHILE, FOR loops.
While Statement – allows the computer to repeatedly perform a series of statements based on a particular condition
Recursion
Basically, recursion is the process of running and rerunning a procedure, based on the performing the steps over and
over. Given the input-output specifications of a procedure, we do not even have to know how the task is accomplished, only that
it is available. The view oft the procedure implies that it is invoked, executed and that it returns control to the appropriate place
in the calling procedure. A recursive procedure is one that calls itself and the process by which a procedure calls itself is called
recursion.
The definition of the factorial of a number, N, may be logically expressed via the ff pseudocode.
Simulation:
Ctrl-I
If (Arr1[Ctr1] > Arr1[Ctr2]) then
Set Temp to Arr1[Ctr1]
Ctr2=3
Contents of Arrl after
Set Arr1[Ctr1] to Arr1[Ctr2]
Set Arr1CCtr2] to Temp Inner Loop
Arr1[4] —Don
LIST
an ordered set of a variable number of elements to which additions and deletion may be made
one of the simplest and most commonly found type of data
LINEAR LIST
DEFINITION
Array - defined as an ordered collection of data items of the same type referred to collectively by a single name (the
name of the array).
Elements - individual data items in an array indicated by the array name followed by its dimensions appears in a square
bracket.
Subscripts- an integer from 1 to n. Array elements are ordered according to subscripts.
Dimensions - an integer from 1-n called dimensioned variables
arrayName [0] [0]
Insert
Adds item to the indicated place/cell
The process is very fast because it only includes one step
Every data inserted is placed in the first vacant cell
Search
Another process that the algorithm carries out
The arrow from the figure states where the search starts
Moves methodically downwards to search for the item
Delete
The algorithm must first locate the item to be deleted
The deleted item causes hole in the array
Dimensionality of an Array
One-dimensional array an array with only one subscript.
TWO-DIMENSIONAL ARRAY
A two-dimensional array is an array with two subscripts. The first subscript is called the row and the second subscript is referred
to as column. Because of this property, two-dimensional arrays are often called tables or matrices.
The figure below shows an example of a two-dimensional array called Arr1 containing 3 rows and 4 columns.
Given three arrays: Arr1, Arr2 and Arr3, where Arr1 and Arr2 are sorted in ascending order, we can merge Arr1 and Arr2 into the
array Arr3 such that the contents of Arr3 are also sorted in ascending order. In order to perform this operation, Arr3 would have
to be large enough to contain all elements.
The algorithm Merge Arr accepts as input the arrays Arr1 and Arr2 along with the following information:
The algorithm also uses three variables. Ctr1, Ctr2, and Ctr3, as subscripts to the three arrays.
TWO-DIMENSIONAL ARRAY
A two-dimensional array is an array with two subscripts. The first subscript is called the row and the second subscript is referred
to as column. Because of this property, two-dimensional arrays are often called tables or matrices.
The figure below shows an example of a two-dimensional array called Arr1 containing 3 rows and 4 columns.
Matrices
A matrix is a mathematical object which arises in many physical problems. A general matrix consists of m rows and n columns.
The following tables are examples of matrices.
Magic Square
is an n x n matrix of the integers 1 to n² such that the sum of every row, column and diagonal is the same. The figure below shows
the magic square for a 3 x 3 matrix where the common sum is 15.
Sparse Matrices
Let us assume that we have a 6 x 6 matrix called Arr1 which contains the following elements:
Transpose
The transpose is a "copy" of a particular matrix wherein the elements in the [] position are transferred to the [] position.
To illustrate this concept, let us consider the figure below which represents a matrix called Arr1.
Merge_Arr (Arrl, Arr2, NI, N2)
Set Ctrl to 1
Set Ctr2 to 1
Set Ctr3 to 1
} / * main end
Continuation...