Algorithm Design & Data Structures
Algorithm Design & Data Structures
Algorithm Design & Data Structures
WHAT IS AN ALGORITHM?
Step1: Start
Step2: Read\input the Radius r of the Circle
Step3: Area PI*r*r // calculation of
area
Step4: Print Area
Step5: End
Write an algorithm to read two numbers and find their
sum.
Step6: End
Algorithms
can be designed though the use of flowcharts
or pseudocode.
FLOWCHART
Theflowchart is a diagram which visually presents the flow
of data through processing systems.
This
means by seeing a flow chart one can know the
operations performed and the sequence of these
operations in a system.
A flowchart, will describe the operations (and in what
sequence) required to solve a given problem.
Flowchart Symbols
Thebasic symbols commonly used in flowcharting of
assembly language Programs are: Terminal, Process,
input/output, Decision,Connector and Predefined
Process.
SYMBOL NAME FUNCTION
Process Indicates any type of internal operation
inside the Processor or Memory
Input/output Used for any Input / Output (I/O)
operation. Indicates that the computer is
to obtain data or output results
Decision Used to ask a question that can be
answered in a binary format (Yes/No,
True/False)
Connector Allows the flowchart to be drawn without
intersecting lines or without a reverse
flow
Predefined process Used to invoke a subroutine or Terminal
an Interrupt program.
Terminal Indicates the starting or ending of the
program, process, or interrupt program
READ r
AREA = 3.14*r*r
PRINT AREA
END
Flowchart for an algorithm which gets two
numbers and prints sum of their value
START
READ A, B
C A+B
PRINT C
END
Problem: Design an algorithm and the corresponding
flowchart for adding the 5 test scores:
Algorithm
1. Start
2. Sum = 0
3. Get a value
4. sum = sum + value
5. Go to step 3 to get next Value
6. Output the sum
7. Stop
Start
Sum = 0
Get a Value
Output Sum
End
PSEUDOCODE
Pseudocode is one of the tools that can be used to
write a preliminary plan that can be developed into
a computer program.
Pseudocode is a generic way of describing an
Algorithm without use of any specific programming
language syntax.
Pseudocode is an algorithmic tool that express a
solution to a problem using simple English like
statements that are closer to a programming
language.
CONTROL STRUCTURES OR LOGICAL STRUCTURES
Start
Get the sum
Average = sum / 6
Output the average
Stop
AVERAGE = SUM/6
OUTPUT AVERAGE
END
Decision Structure or Selection Structure
Thedecision structure or mostly commonly known as a
selection structure, is case where in the algorithm,
one has to make a choice of two alternatives by
making decision depending on a given condition.
Selection
structures are also called case selection
structures when there are two or more alternatives
to choose from.
In pseudocode form we get
If condition is true
Then do task A
else
Do Task-B
The program is to input a examination mark and test it
for the award of a grade. The mark is a whole number
between 1 and 100. Grades are awarded according to
the following criteria:
>= 80 Distinction
>= 60 Merit
>= 50 Pass
< 49 fail
The pseudo-code is
REPEAT
DISPLAY “Enter a number between 1 and 100”
ACCEPT number
UNTIL number < 1 OR number > 100
Note
Steps for solving a programing task
1. Problem analysis – separating the known from the
wanted and identifying any formulae and conditions
that are involved in solving the problem
2. Program designing-
a. Algorithms – presenting the sequence of steps one
should follow in solving the problem. Algorithms
are written in the designer’s language of choice.
b. Flowcharts – presenting the sequence of steps one
should follow in solving the problem using known
standard symbols.
c. Pseudocodes – presenting the sequence of steps one should
follow In solving the problem. Pseudocodes use a language
which is biased towards the programming language to be
used to implement the program.
3. Coding
4. Debbuging
a. Dry running – a check of the logic and coding of a
computer program in which the program’s operations are
followed and the results of each step are written down,
before the program is run.
b. Trace table - a technique used to test algorithms, in
order to make sure that no logical errors occur whilst the
5. Documentation
6. Deployment - installation
Dry run
1. Linear/Sequential
2. Binary/Half-interval.
LINEAR SEARCH
LINEAR SEARCH
A linear search is the simplest method of searching
a data set.
Starting at the beginning of the data set, each item
of data is examined until a match is made.
Thus searching is sequential.
Once the item is found, the search ends.
If there is no match, the algorithm must deal with
this (thus stopping the search and alerting the user).
Algorithm for Linear Search
1. Find out the length of the data set.
2. Set counter to 0.
3. Examine value held in the list at the counter position.
4. Check to see if the value at that position matches the value
searched for.
5. If it matches, the value is found. Send a message and end the
search.
6. If not, increment the counter by 1 and go back to step 3 until
there are no more items to search.
7. If all the items have been checked and no match is found, send a
message.
Example
Suppose we were to search for the value 8
in this list.
The search would start at position 0 and
check the value held there, in this case 2.
2 does not match 8, so we move on to the
next position.
Search ends at position 5 where the value
of 8 is found.
BINARY SEARCH
Binary Search
1. Bubble sort
2. Quick sort
3. Merge sort
4. Selection Sort
5. Insertion Sort
6. Merge Sort
BUBBLE SORT
Bubble sort
A bubble sort is the simplest sorting algorithm that works
by repeatedly swapping the adjacent elements if they are in
the wrong order.
Although it is simple to use, it is primarily used as an
educational tool because the performance of bubble sort is poor in
the real world. It is not suitable for large data sets.
However, it is an inefficient sort for anything but a small list
because of the number of comparisons required.
Bubble sort
Algorithm for bubble sort
1. Start at the beginning of the list.
2. Compare the first value in the list with the next one up. If the
first value is bigger, swap the positions of the two values.
3. Move to the second value in the list. Again, compare this
value with the next and swap if the value is bigger.
4. Keep going until the there are no more items to compare.
Note - the last item checked in the list is now sorted, so
ignore this next time.
5. Go back to the start of the list.
Example
To understand the working of bubble sort algorithm, let's take an unsorted array.
Let the elements of array are –
Quick sort
The quick sort algorithm is a faster and highly efficient sorting
algorithm.