DATA STRUCTURES AND ALGORITHMS...

51
Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION Data -these are raw facts which act as input to a system for processing to obtain an output that is information Structure -This is an arrangement, organization, skeleton or building representation of something. -Combination of components Algorithms -this is a finite set of instructions to effectively and efficiently perform a task or solve a problem. -they work or operate on data. Data structures -this is representation of facts/raw data in memory in an organized way to enable efficient operation on it. Efficiency is about: a) Accuracy b) Speed c) Resources(minimal) Application of algorithm/data structures 1. Software development 2. System development 3. System design 4. Psychology 5. Math 6. Engineering i. Civil ii. Mechanical iii. Electrical Fields 1. Communication and entertainment 2. Medicine 3. Military

Transcript of DATA STRUCTURES AND ALGORITHMS...

Page 1: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 1

DATA STRUCTURES AND ALGORITHMS

INTRODUCTION

Data

-these are raw facts which act as input to a system for processing to obtain an output that is information

Structure

-This is an arrangement, organization, skeleton or building representation of something.

-Combination of components

Algorithms

-this is a finite set of instructions to effectively and efficiently perform a task or solve a problem.

-they work or operate on data.

Data structures

-this is representation of facts/raw data in memory in an organized way to enable efficient operation on

it.

Efficiency is about:

a) Accuracy

b) Speed

c) Resources(minimal)

Application of algorithm/data structures

1. Software development

2. System development

3. System design

4. Psychology

5. Math

6. Engineering

i. Civil

ii. Mechanical

iii. Electrical

Fields

1. Communication and entertainment

2. Medicine

3. Military

Page 2: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 2

4. Industrial

5. Astronomy

6. Training

7. Biometrics

8. Transport

Communication

Wireless communication systems

Cellular

Radar

WLANs

a) Wimax

b) Wi-Fi

c) Bluetooth

d) Zigbee

Satellite

a) Developing

b) Launching

c) Operation

d) Maintenance

Computers- internet and other e-technology

Entertainment

Developing of multimedia-text, audio, video, graphics, animation

Use of different equipment including:-

a) Keyboard

b) Piano

c) Drum-sets

Trends in communication and entertainment

i. Use of artificial intelligent: –

a) Robots

b) Intelligent agents

c) instruments

ii. Communication systems are becoming small in size e.g. smart phones

iii. Robots playing games

iv. Holograms- virtual images

Software

Page 3: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 3

Medicine

Equipment

Treatment

Diagnosis- expert system

Patient data- record keeping

a) Equipments

X-rays

CT-scanners

MRI-magnetic resonance imaging

Chemotherapy

Radiotherapy

Ultrasound system

Endoscopy and colonoscopy

Radar-detecting cancer

b) Treatment

Surgery

Chemotherapy

Radiotherapy

Trends

i. Use of robots; artificial intelligence

ii. Performing surgery by use of video conferencing

iii. Artificial arms or organs e.g. pancreas

Military

Equipment

Research

Battle

Intelligence

Use of encryption i.e. increase security of systems

a) Equipment

Drones

Autopilots

Auto drivers-navigation systems

Atomic bombs

Ulcers

Treatments

Page 4: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 4

Trends

i. Artificial intelligent e.g. robots in the battle field

Industrial

Equipment

Business process

Raw materials

Transportation

Production

Packaging

Marketing

Sales

After sales services

a) Equipment

A scada system-supervisory control and data acquisition system

Automated system e.g. plc -programmable logic controller

Trends

i. Formulae e.g. Coca-Cola company

ii. Production of chemical combination

iii. Robots re artificial intelligence e.g.-

Speed

Environment

Integration of systems

Astronomy

It’s the study of space.

Equipment

Process of performing study

Information transfer

scada

Other system

Page 5: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 5

a) Equipment

Telescope

Robots

Satellite

Trends

i. Hotels in space

ii. Space vehicles

iii. Simulation will be done in all fields

Training

Learning institutions

Systems-video conferencing

(webinar-seminar done online)

E-learning

Trends

i. Hologram

ii. Robots

Biometrics

Use of special features to develop software

Fingerprints

Voice

Facial

DNA

Eye (retina)

Thermogram

Walking style

Palm

Transport

Infrastructure

Systems- cards, cashless

Auto system

Page 6: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 6

Trends

i. Train moving at a high speed i.e. 3000km/hr

ii. Electromagnetic train (mombasa-uganda)

Historical perspective

The word algorithm has been used for a long time

The first time it was coined by a Persian mathematician by the name Abu Abdullah in the 9th

century

But before this time it was being used by the Greece.

One example being the Euclidean algorithm in solving given mathematical problems like

determining the great common division-GCD.

From the above it can be noted that the algorithms were used in solving mathematical problems

In the 19th century development of most communication systems was done using algorithms e.g.

telegraph

Telephones-1890’s by Alexander

Wireless system-by Marconi in 1906

Radio system and tv-1920-30’s

The use of computers in algorithms in 1940’s after the invention of the 1st computer-ENIAC to

solve the Second World War problems.

Earlier the development of binary was done by George Boole on which the computer run i.e.

machine language.

He developed the Boolean algebra being used currently in digital logic-computer hardware

The development of processors in 1970’s the 1st being Intel 4004.

In 1950’s the coining of AI being currently used in computational intelligence

Another person who contributed to this field was called Alan Turing

He performed an experiment to indicate that a computer cannot achieve the human intelligence

levels

The trends in applications of algorithms mentioned under applications can also be discussed

under this section

Page 7: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 7

ALGORITHMS

PROPERTIES OF AN ALGORITHM

Finite-it ends at a certain point

Definite

Effective

Have an output and input

Process

Flexible

Dynamic

Finite

An algorithm should have both a starting and end point.

This is usually represented using start and stop.

It takes some time to process an input

The shorter the time it takes the better

Definite

This is a representation where the algorithm can be understood by the user or any person dealing

with it.

This is by using a language that can be understood e.g. English and also symbols that are well

known to the users e.g. flowcharts symbols

Effective

Should be able to solve a problem

Fast and with minimal errors or none

It can actually be fast by using a shorter algorithm. If there are several that can solve the same

problem e.g.

P=N×N

P= N2

P=N+N+N…………+N N times

N2 Is faster

for instance as above.

A shorter algorithm ensures that the algorithm does not have errors

Easier software to use and understand

Page 8: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 8

Inputs

This is the data that the algorithm will be processing.

Depending on the method there are different ways of inputting the data

For instance one can use:-

Input

Read

Acquire

Input and read are the most commonly used methods in obtaining data.

Output

This is the processed data. It’s usually transferred by the user in different ways:-

a) Output

b) Write

c) Display

The user can actually be a person or another algorithm

Process

This is the manipulation of the input to obtain the output (usable)

It’s the transforming of raw facts to information

There are several manipulations including:-

a) Arithmetic - add, subtract, divide, multiply, integrating, differentiating.

b) Logic operation- AND, OR, NOT, NOR, XNOR, compare.

The process is what determines the complexity of an algorithm.

Flexible

Algorithm can be used/applied in different platforms or application areas.

For instance in math’s, engineering, business.

Dynamic

Can change according to the requirements i.e. one can modify to suit a given application

Example 1

1. Start -finite

2. 𝑅𝑒𝑎𝑑 𝐴, 𝐵, 𝐶 -input

3. 𝑆 = 𝐴 + 𝐵 + 𝐶 -process

4. 𝑋 = 𝑆3⁄ -process

Page 9: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 9

5. 𝑊𝑟𝑖𝑡𝑒 𝑥 -output

6. Stop -finite

In terms of effectiveness it’s not since step 3 and 4 can be combined to one step.

i.e. 𝑥 = [ 𝐴+𝐵+𝐶 3

]

All statements are well defined and therefore understood. Except S=A+B+C

This can also be interpreted as an OR gate with 3 inputs A, B & C.

The above is flexible since it can be used in different fields e.g. business -average of sales, math-

average and basic math, in English-projects.

Dynamic-Adaptive-can change. One can input different values for the above. Static-cannot

change.

But to make it dynamic one requires to prompt for an input where the user is required to provide

those inputs.

Page 10: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 10

ELEMENTS \FUNCTIONS OF AN ALGORITHM

Obtaining an input

Computation

Iteration

Selection

Providing an output

a) Obtaining an input

This is achieved by the use of input for instance input x, y & z

Depending on the programming language there are several methods that can be used to obtain

the input.

b) Computation

This involves processing of the input to obtain a given output

Several formulae or process types can be used to perform the same

𝐴𝑉𝐸𝑅𝐴𝐺𝐸 = (𝑥 + 𝑦 + 𝑧)3⁄

c) Selection

Process of determining a sequence of operation after a given test which can be true or false.

True if…….. statement can be used to achieve this

If statement is true

then action

else

alternative action

d) Iteration

This is the process of repeating a given operation.

It’s also referred to as looping

If a given statement is true or false one goes back and repeats an operation

The following can be used to achieve looping –

i. while

ii. do while

iii. for

Page 11: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 11

1. while

this can be implemented this way

while statement in true

action

end while

2. Do while

Can be implemented in do action while statement is true end

3. For

Can be implemented using-

For a number of times

action

end for

All the above can be implemented in different programming languages with minimal differences

Page 12: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 12

ALGORITHM IMPLEMENTATION METHODS

There are several methods that can be used to implement an algorithm to solve a given problem.

They include:-

Natural language

Flowchart

Pseudo code

State diagrams

Nassi schneiderman charts

Program code

a) Natural language

It is representation where the language being spoken or understood by an individual is used.

There are several languages i.e.:- English, Kiswahili, mother tongue, French, German, Japanese.

All problems to be solved: - step 1 is to present in a natural language form.

Features

Uses the language rules

No syntax rules (errors)

Follows a sequence of statements usually by giving numbers.

Advantages

1. Very easy to understand

2. Simple to write

3. Simple to troubleshoot in case of a problem

4. Not unique

Disadvantages

1. Wordy

2. Ambiguous-statement can have several meanings\interpretation to a given statement.

3. Difficult to be transformed into a program code.

Example 1:

Description of an algorithm that can display the larger number of given two numbers.

Natural language

1. Start

2. Obtain the inputs of the two numbers

3. Compare the two numbers

4. Select the larger number

Page 13: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 13

5. Display the larger number

6. Stop/end

Number 3 can be split into other steps depending on the programming language.

If 𝑥 > 𝑦

output 𝑥

else

output 𝑦

No use of symbols

Just natural language

> greater than

= equal to

≥ equal to or greater than are not used.

b) Flowchart

It’s a diagrammatical representation of sequence of operations to solve a given problem

(algorithm).

It consists of several symbols each representing a given operation

The symbol are given as below:

i. Start and end of the algorithm.

It’s usually indicated as the first in an algorithm.

ii. parallelogram

Represents input and output

Mostly appears after the start symbols before the stop/end symbol.

iii. Diamond

This represents decision also a sole depending on a test.

If a test is true or false a given selection or direction is taken

Start/stop

Input x, y

Output S

Page 14: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 14

iv. Rectangle

It represents a process of a given operation

There can be several of them following each other depending on the solution.

e.g. Add

v. Arrows

Flow of control

It starts from one symbol to the other.

It indicates the sequence of operation

vi. Connector

Connects two or more arrows

vii. Rounded rectangle on one side

It represents displays of the output result

It’s not commonly used

Rules of flow chart

1. Should always have a start and stop

2. Top to bottom or left to right

3. Should occupy a single page

4. Only one arrow can come out of a process but several can go into it

5. Maximum number of symbols is 15 excluding start and stop

6. For decision symbol only one input

False True

𝑠 = 𝑥 + 𝑦

Page 15: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 15

Basic loop structure implementation

Apart from the iteration one can also have

a) Sequence

b) Selection

c) Iteration

i. Sequence

Process following each other

ii. Selection

Can be implemented using the diamond shape

One selects an output/sequence depending on a test

This implements the if then statement

iii. Iteration

Implementation done using a diamond symbol and rectangles.

Process

Process

False

True

if

x>y

Yes

No

If

𝑥

> 𝑦

Process

Start

Process

Process

Page 16: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 16

The loop goes back before the test is done

Can be used to implement: -

While

Do….while

For

Iteration

Advantages

1. Easy to understand

2. Communicates a lot of information in its representation

3. Easy to debug

4. Easy to code since it is close to the program code

5. Structured rules

6. Not wordy

7. Not ambiguous

Disadvantages

1. Modification requires an overhaul of the whole flowchart

2. Requires knowledge of the symbols to be able to understand

3. Due to its uniqueness its complicated

Example

Obtaining average of three numbers determine the flowchart

start

Input

x,y,z

Avg =ሾ𝑥 + 𝑦 + 𝑧ሿ 3Τ

Output

Avg

Stop

Page 17: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 17

1. Start

2. Obtain the input of the 3 numbers

3. Add the three numbers to obtain sum

4. Divide the sum by 3 to get average

5. Display the average

6. Stop

Exercise

a) Obtain a flow chart for the problem natural language

b) Obtain natural language and flow chart for a problem using :-

While

Do………while

For

Pseudo code

Pseudo code-false code: - sequence of instruction to perform a function or solve a problem

Pseudo code: this is a code which features between the natural language and programming

language to be used

Features

It has structured language

Uses most of the parameters applied in programming language including equal and other

mathematical symbols

Not executable

Does not follow syntax rules

It has inputs, outputs, computation and iteration

For computation the mathematical solutions are used

For selection the if else is used

For iteration one should use the for, while and do while control methods

Numbering the sequence of operations is important

It makes programming easier to understand

Reduces the errors

Increases the efficiency of the programming process

Example

Write/develop a pseudo code for a problem to determine the average of 3 numbers

Solution

1. Start

Page 18: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 18

2. Input: x, y, z

3. Get the sum = x +y + z

4. Average= 𝑠𝑢𝑚3⁄

5. Output the average

6. Stop

Exercise

a) Develop a pseudo code to determine the larger number of two numbers

1. Start

2. Input x, y

3. If x>y

4. Output x

else

Output y

5. stop

b) Develop a pseudo code to display looping ten times.

1. Start

2. For(a=10; a<20; a++)

3. Output looping

4. end looping

5. Stop

c) Three numbers output the larger

1. Start

2. Input : x, y, z

3. If x > y & x > z

out x

if z > x & z >x

output z

4. Stop

While loop

1. Start

2. X = 1

3. While ( x < = 10 )

4. Output : looping

x= x +1

end

5. Stop

Nassi schneiderman chart

This is also a diagrammatical representation of a sequence of instructions to solve a given problem

Page 19: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 19

It is characterized by a larger rectangle with smaller rectangles inside the larger rectangle

It uses the following symbols.

1. Rectangle

It represents a process just like for the flowchart

2. Decision box

3. A test is done if yes instructions are performed otherwise instructions under false are

implemented

false true

Has several triangles and rectangles given above

4. Looping rectangle

Characteristics

Fewer symbols

Difficult to change to programming language

Does not have a line to represent flow

Example 1

For average of 3 numbers

Rectangle

Process

While true for condition

Instructions to be

performed

Start

Sum = x + y + z

Input x,y,z

Output: Avg

Avg = 𝑠𝑢𝑚3⁄

Stop

?

Instruction for

false

Instruction for

false

Page 20: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 20

Example 2

Example 3

Exercise:

Develop an algorithm using:

Pseudo code

Schneiderman chart

State diagram

Program using java

State diagrams

A diagrammatic representation of an algorithm

They are represented using circles and lines only

Circle- represents a state with processes

Also input and output as well

Start

Input x , y

No Ye

Output y Output x

If x > y

Stop

X= 1

Start

Stop

While x <= 10

Output looping

x = x + 1

Page 21: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 21

The arrow indicates the flow or sequence of processes

Features

Have only two symbols

Simpler and easy to understand

Example

Average of 3 numbers

For selection

Yes No

if x>y

End

start

Input

a,b,c

aver

age

Sum

a,b,c

outp

ut

Page 22: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 22

Determining the larger number of three numbers

Looping

Loop

Loop

Program

A sequence of instructions in a given language to perform a task or solve a problem

start

Input

x,y,z

Output

y

If

x>y&z

If

y>x&z

Output

x

end

Output

z

If

z>x&

y

start

process

end

process

state

Page 23: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 23

The instruction are executed so that the system (digital) can be able to perform a task or solve a

problem

Can be in high-level language or low level language –machine (0,1) assembly language

Features

1. Follow syntax rules

2. Executable

3. Can be Compiled

4. Computers use these programs to solve problem. (computer language)

5. Have outputs – information

6. Have inputs – data

7. Have processes : -

Computation

Sequence

Branches

Selections

8. Most programs require initialization

Types of programs

a) Unstructured

b) Structured

c) Object-oriented programs

Unstructured

Small programs which can perform a simple/single task

They work on their own without involving other programs

Structured

Large programs to solve a large program

They are broken into smaller parts which can work independently and later their solution

combined to solve the bigger problems

Object oriented programs (OOP)

Similar to structured and the difference being that the codes can be used in solving other

problems/programs

This is by having a few modifications

Examples

C, C++, java, php, VB.Net, Ruby, Python, C # etc.

Page 24: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 24

For(int a=0; a<10; a++)

double a= math.random ( )

System.out.println(“random no is ” +a)

Exercise

Develop a program to generate the following: -

4.2 0<0.2

4.3 0.2<0.5

4.4 0.5<0.8

4.5 0.8<1

Page 25: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 25

ALGORITHM DESIGN STEPS

Design – process of analyzing and developing algorithm to be used to solve problems.

There are several steps that can be used but the most commonly applied are given by:-

Problem definition

Develop model

Specification development

Design of algorithm

Check errors

Analysis

Implementation

Test the algorithm

Documentation

They are much similar to system design and analysis

A. Problem definition

The process of making the problem understandable

The problem is put up clearly in well-defined steps on how it can be solved

B. Model development

It’s a representation of the actual object or a formula to solve a problem

It’s also a sequence of how a problem will be solved

C. Specification development

Specification – these are the algorithm requirements

They can also be capabilities of the algorithm when in operation e.g. data can be processed the

operations and processes capability

D. Design the algorithm

The process of planning on how the algorithm is to be developed

One can use simulation software to perform the design

E. Check for errors

Go through the algorithm to check for errors for instance logical errors or if a program can check

for syntax errors

F. Analysis

This involves the determination of the resources requirement for the algorithm e.g. time and

memory

Page 26: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 26

G. Implementation

The algorithm is developed in a given language

For a code, use the different available programming language.

Other methods include flowcharts e.t.c.

Mainly use programs to implement the algorithm

H. Test

This can be achieved by compiling or executing the algorithm

One can determine whether it’s working using the methods above

I. Documentation

The process of putting the steps and the algorithm itself in a file which can be saved

Can serve as a manual which can be referred to later

It’s important in case an update is required or any modification is required later

Algorithm types/development methods

There are several methods that can be used which include: -

a) Randomized method

b) Greedy method

c) Divide and conquer method

d) Approximation method

e) Dynamic programming solutions

Randomized method

These are based on statistical parameters of a given problem that can make use of a random

numbers. For instance one can use mean, mode, variance or standard deviation.

One can also use some of the distribution functions which include: - normal, binomial, bernoulli,

Poisson hyper geometric etc

An example of a problem is when arranging documents in a given order, one can start from the

approximate median later manage develop the required order.

Specifically- scripts – registration numbers; check bounced

Approximation of Pi using Monte Carlo (obtain in several values & determine the mean)

Divide and conquer

This uses the concept of dividing the problem into smaller parts/sections then developing

algorithms for each and later combining them.

The most commonly applied is where the problem is halved e.g. divided into two equal sections

continue dividing to the lowest possible level then work with each section

Example :- for bounced checks to be sorted

Page 27: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 27

Dynamic programming methods

Similar to divide and conquer method and the difference being that whatever is being sorted has

non unique elements

Divide and conquer is based on sorting unique elements or independent elements

Greedy method

Commonly applied in obtaining local solutions as opposed to global solutions

Usually picks on the solution that appear first might not be the overall solution.

An example is the Huffman coding techniques

Its characterized by determining the path in graphical problem

Approximation

These algorithms do not give enough or accurate solutions to problems

The solution is close to the actual value commonly applied in case where the methods for

obtaining the actual value is very complex and time consuming e.g. when using regression method

basic fitting method, artificial intelligence based solutions and neural networks

Specific-approximating sales in a given day, year or month

Process of determining the efficiency of an algorithm in terms of resource usage which include

time and memory

It’s very important to discuss about the same since we need to avoid algorithms that require large

amounts of these resources

For instance an algorithm to solve a problem- gives solution in 10m instead of 20m can be said to

be an efficient algorithm

Same cases apply to one which will take less memory space

The analysis can be done using

Best case

Average case

Worst case

Worst case scenario is commonly applied since it results to the ultimate or the upper limit in case

of a given resource

Page 28: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 28

BIG O-NOTATION

It gives/represents algorithms in different forms based on the time they take to run

The mathematical representations are in an equation form

The equations include;

Constant

Scaling factor

Quadratic

Exponential

Logarithmic

Factorial

a) Constant

Have a value e.g. 5 or 20 that is added or subtracted e.g. -5 or -20

=20

b) Scaling factor

A number is used before a variable e.g. 2n

=5n

c) Quadratic form

Variable to a given power e.g. 𝑛2

2 is a power

One can have a power greater than 2 which is not a quadratic but a power of a larger value

=10𝑛2 + 5 𝑛 + 10

d) Exponential

A number to the power of the variable e.g. 9𝑛 where n > 1

=2𝑛

e) Logarithmic

Representation where logarithm to base is used e.g. log2 𝑛 𝑛 log2 𝑛

f) Factorial

Obtains the factorial of the variable e.g. n!

3! = 3 × 2 × 1

= n × (n – 1)…………………..

An algorithm’s time resource can be represented using the above expression

Example

T( 𝑛2) = 10𝑛2 + 5 𝑛 + 10

quadratic

Scaling factor

constant

Page 29: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 29

Where n = the number of times a command is executed

Rules for O-notation

For one to approximate the time it takes for an algorithm to complete the following rules should be

followed

1. Ignore the constant because as the value of n becomes larger and larger eventually constant terms

will become insignificant

2. Ignore the scaling factors because they will became insignificant as the value of n increases

3. Ignore the lower order powers 𝑛𝑎 & 𝑛𝑏

a & b taken as 𝑛9

4. Exponential form is taken instead of the polynomial forms

𝑎𝑛 & 𝑛9 (𝑎𝑛 - is selected)

5. Ignore the logarithmic forms over the polynomial form 𝑎𝑛 + n log 𝑛 so n log 𝑛 is ignored at higher

values of n

Factorial is taken over the other terms n1 > 𝑎𝑛 or 𝑛9 . Ignore other terms

Example

Given T( 𝑛2) = 10𝑛2 + 10 𝑛 + 10

= 10𝑛2 => 𝑛2

This is why it is represented using T( 𝑛2)

T( 𝑛4) = 10𝑛4 + 100𝑛 3 + 20

Will take 𝑛4

Also

𝑎𝑛 + 𝑛 4 + 20𝑛 3

Will take 𝑎𝑛 since it dominates the expression

Example 4 running time

Running time for 3𝑛2 : 3(10)2/ 3(10) 2 + 10(10) +10 = 24.4 %

Running time for 10n: 10(10) / 3(10) 2 + 10(10) +10 = 24.4 %

Running time for 10: 10 / 3(10) 2 + 10(10) +10 = 2.4 %

Page 30: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 30

Growth rate

n=100

3𝑛2 : 3(100)2/ 3(100) 2 + 10(100) +10) = 96.7 %

10𝑛2 : 10(100)2/ 3(100) 2 + 10(100) +10 = 3.2 %

10: : 10 ( 3(100) 2 + 10(100) +10) = 0.1 %

Sections

Statements

Selections

Loops

A. Statements

The time taken by statement is given as a constant since there is no variable e.g. inputting data

int a, b;

processing without loop e.g.

sum = a + b ; => 𝑇2

outputting data

System.out.println(“sum = ” + sum);

=> 𝑇5

For average of 3 numbers

Total time = 𝑇1 + 𝑇2 + 𝑇3 + 𝑇4 …………..

Excluding the start and stop

B. Selection

If……………………else

If

Statement (2)

Else

Statement (4)

Time taken for a selection is actually the larger of the two statements indicating the worst case

scenario.

C. Loop

1. Do while

2. For loop

3. While loop

For (1=0; i < n; 1++)

The statement will be executed n times

Time taken will be given : total = nT

T is time taken to execute the statement -linear relation

For while and do while loop the time taken will be the same as for the for loop

Page 31: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 31

Exercise

Develop java program for algorithm with the following growth rates

a. T

b. Tn

c. 𝑇𝑛2

d. 𝑇𝑛3

e. 𝑇𝑛4

f. 𝑇𝑛

g. n!

h. 𝑛 log 𝑛

Determine the approximate time each of them takes

Public static void main string () arg) {

for(1=0; 1<n; 1++)

for(j=0; j<0; j++)

System.out.println(“display n” n);

Page 32: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 32

DATA STRUCTURES

INTRODUCTION

Methods used to represent data and storage of the same.

Variables are commonly used in storing numerical data.

There are several concepts which includes

Types

Data types

Abstract data types.

Types

These are values that are used in algorithm e.g. int, double, float etc.

Data types

This takes into account the value and all their operations

Those operations include:-

Arithmetic operations

Logic operations

Relational operations

Conditional operations

Assignment operations

Abstract data type

These are the different data types with no predefined implementation

They are implemented using programmer defined software.

They include: - Arrays, sets, lists, queues, stacks, trees, graphs.

Other characteristics are given by : -

Primitive data types

Non-primitive data types

a) Primitives

These include the types mentioned earlier: -

Integers

Doubles

Float

Boolean

Character

Byte

Page 33: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 33

Word

b) Non-primitive

There are two sections; linear and non-linear

i. Linear

The elements are represented in a linear form

They include:-

Arrays

Lists

Stacks

Queues

Sets

Elements appear one after the other

ii. Non-linear

Elements can appear below other elements

i.e. hierarchal, tree, graphs, tables

Additional functions commonly used in math i.e.

o cos math .cos x

o sin math. sin x

o power math.pow x,4

o Random math random ( )

o Number between (0-1)

o Square root math.sqrt (x)

Page 34: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 34

ARRAY

This is a variable which can store elements of the same type e.g. numerical will be able to

store an array of decimal numbers

Characteristics

Same type of element

Fixed -cannot add or subtract

Represented within brackets of different types depending on the language e.g. java { }

They can be in different dimensions for more than one dimensions { { { } } }

Arrays are a representation of matrixes

Operations of arrays

The usual operations i.e. arithmetic logic e.t.c. plus copy

Array initialization and declaration

An array is initialized using the following

int int array name () = { };

e.g.

int a [] = { 1, 5, 2, 6};

Double

Replace int with double e.g. double b [] = {2.2, 3.5, 6.8};

Indexing

Accessing the individual elements

The elements will be accessed in a systematical method where it is assumed that the elements

are in co locations (following each other).

The 1st element is stored in location 0 and the last element in location n-1 for n elements

e.g. int a [ ] = [2 5 4 0 1 2

83

]

Each element is rep as 𝑎ሾ0ሿ 𝑎ሾ1ሿ 𝑎ሾ2ሿ 2 5 4

𝑎ሾ3ሿ 8

If one requires to use the overall total number of elements can be rep using a length

A length – gives the number of elements in the array

To access the arrays one can also use the looping methods e.g.

Page 35: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 35

for ( i=0; i<=a.length; i ++)

System.out.println(“element a[i ]= “+ a[i ] );

This shows all the elements in array

Exercise

Develop a code to determine the smallest and largest elements in the array

Determine the sum and product of the array

0 1 2 3

Int [3a] = {2,5,6,9} ;

Indexing

a [i]

a.length = 4 elements

a[0] = 2

a[3] = 9

System.out.println ( a[2] );

Loop

int [ ]a= {2, 5, 6, 9 };

for int I = 0; i < a.length; 1++) {

system.out.println {a [i ] }

Sum of elements

Int [ a] = {2, 5, 6, 9 };

int sum= 0 ;

for ( int i<0; i<a.length; i++){

System.out.println (a[i]);

average = sum(a.length);

Page 36: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 36

System.out.println(“sum is” +sum);

Multidimensional array

These have several dimensional elements depending on the dimension number

For two dimensions the array will be in a form of matrix with rows and columns.

Declaring a dimension array

int [ ] [ ] a ={ {2, 5, 9} ; {8, 6, 0} ; {7, 3, 1} }

Indexing

int [row] [column]=> [i] [j]

The first element is represented using a [0] [0] = 2,

int [ ] [ ] a={ {2, 5, 9}; {8, 6, 0}, {7, 3, 1} };

int sum = a[0] [2]+ a [1] [2] + a [2] [2] ;

System.out.println(“sum is” +sum);

0

2

3

0 1 2 3

2

8

7

1

5

0

9

6

1 3

7

7

7

Rows

Column

s

Page 37: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 37

LINKED LIST

A list of elements which have links to each other.

The links are also called pointers.

There are several types including : -

a. Singly linked list

b. Doubly linked list

c. Multiply linked list

d. Circular linked list

e. Empty linked list

Singly linked list

The pointer are pointing in a single direction mostly forward

Diagram

Doubly linked list

It is linked in both directions i.e. forward and reverse

Multiply linked list

This is equivalent to a doubly linked list only that it can link more than one element

For instance a record with several properties

15 12

e1 e1 e1

pointers

e1 e1

Page 38: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 38

Circular linked list

The links form a loop such that the last element links to the last elements.

Diagram

Empty linked list

This is a list with no element

Features

Have a pointer pointing to the next element

Pointers occupies an extra memory

Easy to traverse in forward direction for singly linked list

Easy to implement delete and insert

Easily implement stacks and queues

Implementation

They are implemented using arrays with variable number of elements

In java it is implemented using linked list class

Operations

Add elements

Delete/remove elements

Traverse the element (list)

properties

object

e1 e1

Page 39: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 39

SET

This has several elements not in a particular order but unique- all elements are different

Represented as;

A = { 𝑒1, 𝑒2………………………………………..𝑒𝑛}

e.g.

The elements are represented using some letters

A = a elements. A = { 𝑎1, 𝑎2………… }

A subset has a few element of the set

Features

Have unique elements

The elements can be integers or names

Operations

They include: -

Arithmetic

Logic operations

Mostly the logic operations are applied

Logic operations

A. Intersection – AND – n\Λ

A= { 1 4 3 6}

B = { 2 5 3 1}

C = A n B = {1 3}

B. UNION- OR – U\V

A= {1 4 3 6 }

B = {2 5 3 1}

C = A U B = {1 2 3 4 5 6}

C. XOR – U or v

It’s either A OR B but not both

C = A 𝑈 B = { 2 4 5 6 }

Page 40: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 40

D. NEGATE ~

It inverts the set

C = 𝐴 => All the elements in the universe of discourse except the ones in A set.

Compare – relating the set for equality

Page 41: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 41

STACK

Implementation

Implementation using list linked lists

Most of the features of linked list

These can be implemented using trees or last in first out.

push pop

Features

Has element placed on top of the others

Last in first out

Length can vary

Operations

Push an element – add it to the top of the stack – one can use add or push

Pop – process of removing the last element placed on the stack

push pop

Application

In memory to place and remove data e.g. undo in world processing and back in web browsing

e1

e3

e3

e2

e4

……………

e1

e2

e3

e4

e1

e2

e3

e4 e5

Page 42: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 42

QUEUE

This implements the first in and out of list

Diagram

In out

Features

FIFO

Other features of the linked list

Implementation

Can be implemented using the list on linked list

Operations

Add elements – en queue

Remove elements – de queue

Compare elements

Applications

Applied in queuing systems

Banks

Hospital

Schools

Records

These are implementing of list with references.

Data about a given object can be obtained from files stored in a storage device

The properties of an object can be linked in a record

For instance a data has the following

Year

Month Record

Day

e1 e2 e3

Page 43: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 43

TREE

It’s an implementation of nodes that develop a tree format with leaves coming out of the

branches and stem i.e.

Branches

This has a parent and at most two children

One can also have a single child unbalanced: two children – balance

Operations

Search – one can search through the tress for an element

There are two types of searches

Breadth First Search (BFS)

It’s implemented by looking at the children first.

parent

children

Page 44: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 44

Depth First Search

One goes through the nodes to the last node in a branch before coming back to the others

1

0

1

5

2

0

Page 45: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 45

SORTING AND SEARCHING

Sorting

This is the process of organizing data in ascending or descending order

This can be done to an array of elements

There are several types which include: -

Insertion sort

Selection sort

Bubble sort

Merge sort

Pivot sort

Insertion sort

Characterized by picking each element and placing them in their proper position

Example

Sort the following elements using insertion sort.

a = {2 5 1 6} a= {8 5 6 2}

One can develop an algorithm for the above

One requires to insert each element in its rightful position

2 5 1 6

2 8 5 6

2 5 8 6

2 5 6 8

8 5 6 2

Sorted

1 2 5 6

1 2 5 6

1 2 5 6

Page 46: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 46

Selection sort

Swaps the element by taking an element to its position and swapping it with its position for the

one in the wrong position.

Burble sort

This is achieved by using the swap method where two consecutive elements are swapped to start

with the smallest

The process continues till all the elements are sorted

1. a = { 8 2 5 4 1 3 }

2. a = { 2 8 5 4 1 3 }

3. a = { 2 5 8 4 1 3 }

4. a = { 2 5 4 8 1 3 }

8 5 6 2

2 5 6 8

2 6 5 8

8 5 6 2

2 5 6 8

unsorted

sorted

unsorted

sorted

Page 47: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 47

5. a = { 2 5 4 1 8 3 }

6. a = { 2 5 4 1 3 8 }

7. a = { 2 4 5 1 3 8 }

8. a = { 2 4 1 5 3 8 }

9. a = { 2 4 1 3 5 8 }

10. a = { 2 1 4 3 5 8 }

11. a = { 2 1 3 4 5 8 }

12. a = { 1 2 3 4 5 8 }

Merge sort

It uses the divide and conquer method to sort the elements

The process starts by dividing the elements into halves till single elements are on their own

The elements are then merged two at a time while they are sorted

The process continues till all the elements are merged and sorted

Page 48: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 48

Example

a = 8 2 5 4 1 3

= { 8 2 5 } { 4 1 3 }

{ 8 } { 2 5 } 4 { 1 3 }

{ 8} { 2 } { 5} { 4 } {1} {3}

{ 2 5 8 } { 1 3 4 }

{ 1 2 3 4 5 8 }

Pivot sort

Takes an element as the pivot

One determines the element on the lower and upper part of the counter element

Example

a = {8 2 5 4}

Taking 5 as the pivot point determining the element on the lower and upper side of 5

8 2 5 4

4 2 5 8

2 4 5 8 sorted

Search methods

These are methods used to traverse through the elements in an array

There are two main types: -

i. Linear

ii. Binary

Linear

It goes through the element one by one to locate the element being searched for.

Page 49: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 49

Example

a = {8 2 5 4}

search for 5

Steps

Check for each element comparing with five(5)

Go through the 1st element which is not equal to 5

Then the next element 2 which is not equal

The next element 5 is the same

At this point one can display that the element has been found

One can either continue with the search or stop at this point {Natural language}

Binary search

This is characterized by searching the element after sorting and dividing the element into two

sections.

If the element is not in the lower half it will appear on the upper

The remaining elements are again divided into half and processes continues till the element is

found

Example

a = {8 2 5 4}

Process

Divide the element into two

{8 2} {5 4}

Check the element in one of the two sections depending on the elements at the middle

Continue dividing and checking till the elements are sorted

{ 8 2 5 4 }

sort: { 2 4 5 8 }

Page 50: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 50

ICS 2105 Data Structures and Algorithms.

Pre-requisite: SMA 2100 Foundation of Mathematics

Course Description

Abstract data types, concepts, data models and analysis of algorithms. Elementary data structures

such as arrays, lists, records, sets. Stacks, queues and binary trees. Sorting and searching

algorithm. Recursion.

Course Objective:

By the end of this course the student should be able to;

Understand History of Data Structures and Algorithms.

Understand basics of Data Structures and Algorithms

Course Content:

1. Abstract data types,:

Week 1 & 2

Abstract data types,

Week 3, 4 & 5 CAT 1

concepts, data models

2. analysis of algorithms:

Week 6 & 7

analysis of algorithms

3. Elementary data structures:

Week 9 & 10 CAT 2

Elementary data structures, such as arrays

4. lists, records, sets:

Week 11 & 12

lists, records, sets

Page 51: DATA STRUCTURES AND ALGORITHMS INTRODUCTIONoomaelecturer.com/wp-content/uploads/edd/Data-structures-and... · Compiled by Omae Oteri ©2015 1 DATA STRUCTURES AND ALGORITHMS INTRODUCTION

Compiled by Omae Oteri ©2015 51

5. Stacks, queues and binary trees:

Week 13 CAT 3

Stacks, queues and binary trees

6. PRACTICALS:

Week 14

Sorting and searching algorithm. Recursion

Week 15 & 16

Final examination

Teaching Methodology:

The course will have two sessions of two (2) hours and one hour a week. Lectures, class and group

discussions and research assignments will be an integral part of this course.

Method of evaluation:

There will be three (3) CATs and two (2) assignments across the semester which will

account for 30% of the overall mark.

There will be final examination at the end of semester which will account for 70% of the

overall mark.

Reference books

Data Structures and Algorithm Analysis Edition 3.2 (Java Version) by Clifford A. Shaffer

Data Structures & Algorithms in Java Second Edition by Robert Lafore

Data Structures and Algorithm Analysis in C by Mark Allen Weiss

Data Structures and Algorithms Using Visual Basic.Net by Michael Mcmillan

Data Structures and Algorithms in Java Second Edition by Adam Drozdek

Data structures & problem solving using Java by Mark Allen Weiss 4th ed

Teach Yourself Data Structures and Algorithms in 24 Hours by Robert Lafore