0% found this document useful (0 votes)
44 views

CTRL+L: Criteria For Algorithm

An algorithm is a set of steps to solve a problem or complete a task. It must be finite, unambiguous, and effective. Algorithms use data structures like arrays, linked lists, stacks and trees to store and organize data. Pseudocode and flowcharts are used to design algorithms before writing the actual program code. Key elements of algorithms include inputs, outputs, sequence, branching, and looping. Common algorithms include sorting, searching, and merging data.

Uploaded by

Ellie Ulry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

CTRL+L: Criteria For Algorithm

An algorithm is a set of steps to solve a problem or complete a task. It must be finite, unambiguous, and effective. Algorithms use data structures like arrays, linked lists, stacks and trees to store and organize data. Pseudocode and flowcharts are used to design algorithms before writing the actual program code. Key elements of algorithms include inputs, outputs, sequence, branching, and looping. Common algorithms include sorting, searching, and merging data.

Uploaded by

Ellie Ulry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

What is an algorithm?

 a finite set of instructions that specify a sequence of operations to perform specific task.
 recipe for solvingCtrl+l
a problem

 Example of simple algorithm

o Apply to wet hair


o Massage gently
o Leave on for a few moments
o Rinse Off
 all the tasks that can be carried out by a computer can be stated as algorithms

Ctr2
Criteria for Algorithm

Every algorithm must satisfy the following criteria:

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

 specialized format to store and organize data in a computer’s memory or disk


 collection of variables, possibly of several different data types connected in various ways

Types of Data Structures

o Array
o Linked list
o Stacks
o Trees
o Sorting

Pseudocode

 textual presentation of a flowchart


o close to natural language
o the control structures impose the logic
 may become part of the program documentation
 could be translated into a program 

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 Statement – is a conditional statement

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.

Contents Of Arrl after


Inner Loop
Sort_Arr (Arr1,N)

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

 a list which displays the relationship of physical adjacency


 is a finite sequence of simple data, items, or records

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

Hole-one or more cells that have filled cells above them

Dimensionality of an Array
 One-dimensional array an array with only one subscript.

Example: Array1 [j]

 Two-dimensional array - an array with two subscripts.

Example: Array1 [i,j]

 Multi-dimensional arrays an array with more than two subscripts.

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.

Merging Two One-Dimensional Arrays

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:

1. N1 The size of array Arr1


2. N2 The size of array Arr2

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

 interchanging the rows and the columns.

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...

set Arr3[Ctr3] to Arr1[Ctr1] Increment Ctrl

Set Arr3[Ctr3] to Arr2[Ctr2]


Increment Ctr2

1. Arrl is a one-dimensional array of


size 4 containing the following elements:

2. Arr2 is a one-dimensional array of


size 3 containing the following elements:

3, Arr3 is array of size 7 containing


blanks:

While Loop: iæraäon


Third Condition*/ "Copy_from_Arr1 () */ Arr3[6]
= Sleepy ctrl = 5 ctr3 = 7
While Loop:
/*First Condition*"
/*Copy_ Arr3C7] = Sneezy
Ctr2 = 4 ctr3 = 8

Contents Of Ard after Merge_Arr


ctrl = 1 Ctr2 - 1 ctr3 = 1
While Loop: 1st
"Fourth Conditionx/
/*Copy_from_Arr2() */
Arr3C1] = Bashful Ctr2 2 ctr3 = 2
b•Vhile Loop:
/*Fourth Condition
"Copy_from_Arr2() */
Arr3C2] = Doc Ctr2 = 3 ctr3 = 3
b•Vhile Loop: 36
Third Condition*/
/*Copy_from_Arr1 () */ Arr3C31 = Dopey ctrl = 2
Ctr3 = 4
b•Vhile Loop: iæraäon
/*Third Condition*/
/*Copy_from_Arrl () */ Arr3(4] = Grumpy
ctrl = 3 ctr3 = 5
b•Vhile Loop: 5h
/*Third Condition*/
"Copy_from_Arr1 () */
Arr3C5] = Happy
Ctrl = 4 ctr3 = 6

You might also like