0% found this document useful (0 votes)
0 views49 pages

CSC 231-Data Structure and Algorithms

The document outlines the course content for CSC 231: Algorithm and Data Structure at Ebonyi State University, covering topics such as computer programming concepts, data structures, and algorithms. It details programming methodologies, types of programming languages, and the characteristics of variables and constants. Additionally, it discusses operators, expressions, and the importance of algorithm analysis in programming.

Uploaded by

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

CSC 231-Data Structure and Algorithms

The document outlines the course content for CSC 231: Algorithm and Data Structure at Ebonyi State University, covering topics such as computer programming concepts, data structures, and algorithms. It details programming methodologies, types of programming languages, and the characteristics of variables and constants. Additionally, it discusses operators, expressions, and the importance of algorithm analysis in programming.

Uploaded by

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

EBONYI STATE UNIVERSITY, ABAKALIKI

COMPUTER SCIENCE DEPARTMENT

COURSE CODE: CSC 231

COURSE TITLE: ALGORITHM AND DATA


STRUCTURE

DR. HENRY F. NWEKE


COURSE CONTENTS
 Computer programming concept
 Definition of computer programming
 Levels of computer programming languages
 Programming methodologies
 Variable and types
 Operators
 Expression and assignment statement
 Translator and Interpreter
 Feature of translator
 Fundamental of data structures
 Data types
 Data type declaration
 Abstraction in Computer programming
 Types of abstraction
 Abstract data type (ADT)
 Categories of abstract data type
 Advantages of ADT
 Data structure
 Introduction
 Data structure terminology
 The need for data structure in programming
 Common operation in data structure
 Advantages of data structure
 Classification of data structure
 Primitive
 Non-primitive
 Characteristics of data structure
 Linear and Non-Linear Data Structure
 Linear data structure
 Array
 Linked list
 Stack
 Queue
 Non-linear data structure
 Tree
 Graph
 Hashing
 Garbage collection
 Memory garbage collection
 Introduction to algorithm
 Overview of algorithm
 Property of algorithm
 Different types of algorithm
 Sequential and iterative algorithm
 Recursive algorithm
 Backtracking algorithm
 Divide and conquer algorithm
 Greedy algorithm
 Brute force algorithm
 Randomized algorithm
 Dynamic programming algorithm
 Sorting algorithm
 Bubble sort
 Search algorithm
 Linear search
 Analysis of algorithm
 Need for Algorithm analysis
 Types of algorithm analysis
 Worst case scenario
 Best case
 Average case
 Amortized
CHAPTER ONE: COMPUTER PROGRAMMING CONCEPT
1.1 Introduction
Computer programming is the art of defining a problem and expressing the steps to solve the
problem as instruction in any chosen programming language of your choice for computer to follow.
Programming is the process of giving machines a set of instructions that describe how a program
should be carried out. Programmers will spend their whole careers learning a variety of
programming languages and tools so they can effectively build computer programs. Programmer
use code editor or integrated development environment (IDE) to write source code. The source
code need to be converted into machine languages so computer can understand the instruction and
execute the program. The process of converting the source code into machine language is known
as compiling. Example of compiled language are C++, C, Java etc. there are other language that
do not use compiler. Instead, these language use an interpreter that will read and execute the source
code. Examples of interpreted language are JavaScript and PHP. Once the code is executed, then
the computer program can run. The different types of computer programs include word processors,
database systems, video games, and websites. Through programming language, we instruct the
computer to carry out some processes or tasks. Therefore, computer programming language is
designed in order to communicate ideas about algorithm between human being and computer.
While computer program is sequence of instruction given to the computer to perform a particular
task.
Examples of popular programming
 Python
 JavaScript
 C/C++
 Java
 C#
 Ruby
 PHP
 QBasic
 Visual Basic
 MATLAB
 PASCAL etc.
1.2 Levels of Programming Language
a. Machine language: a low level language that consist of 0's and 1's (binary). High level
languages are compiled into machine code so the code can be executed by the computer.
However, machine language is tedious and difficult to write. They are not user friendly as
user are required to remember list of codes, number, operations. Moreover, debugging the
code is difficult
b. Assembly language: a low level language that is compiled by an assembler. Assemblers
translate human code to machine code. Assembly language uses symbolic code
(Mnemonic) such as ADD for addition, SUB for subtraction, MUL for multiplication and
reference memory addresses of the computer. Translator is used to convert assembly
language into low level machine language. Therefore, translator is an assembler program
that takes assembly language and convert it into machine language. However, assembly
language is repetitive, tedious and error prone.
c. High level language: these are programming language that use near human language and
they are independent of the computer executing the program. high level languages deal
variables, arrays, object oriented approach, complex arithmetic, Boolean expression,
classes, block function, procedure etc. Examples of high level languages are JAVA, C++,
C#, Python, COBOL, PASCAL, C, Kotlin, Visual Basic, and QBasic etc.
1.3 Advantages of High level languages
 User friendly and easy to learn and write compare to machine language and assembly
language;
 It is very portable and can be used in any computer;
 Efficient and saves time and effort compare to other programming languages
mentioned earlier;
 Easy to debug code written in high level language.
1.4 Features of most programming languages
 Must have synthetic rules for forming variables, statement etc.
 Have vocabulary consist of letters of the alphabet
 Must have language structure that consist of statement, keywords, and expression
 May require a translator to be understood by the computer.
1.5 Programming Methodologies
 Procedural programming: execute statement in consistent sequence or series of steps.
These series of steps are used to perform calculation, retrieve input or produce output.
Examples include COBOL, QBasic, FORTRAN, PASCAL, C etc.
 Object oriented Programming: this works with the idea of building programs around
collections of objects. This is a collection of objects that interact with each other through
message passing. It uses object modelling, classification and inheritance to develop
program blocks. Examples of object-oriented programming are C++, JAVA, Python,
Kotlin, JavaScript, VB.Net etc.
 Functional programming: is functional program is a collection of mathematical functions
consisting of input (domain) and result (range). It uses functional composition and
recursive approaches to enable interaction and composition of functions. Examples are
LISP, Scheme, and Haskell.
 Declarative (logic) programming: this is a collection of logical declaration about what
outcome a function should accomplish rather than how that outcome should be performed.
Logical programming provide a natural vehicle for expressing non-deterministic values
and extensively used in artificial intelligence development. Example is Prolog
programming language.
 Event-driven programming: an event driven programming is a continuous loop that
responds to an event that are generated in an unpredicted order. These event originate from
user actions on the screen (mouse click or key stroke) or from other sources such as reading
from sensors on a robots. Examples of event driven are JAVA and VB
 Concurrent Programming: this is a collection of cooperating processes sharing information
with each other from time to time asynchronously. Examples are FORTRAN, Linda.
 Scripting language: these languages often times don't need to be compiled but rather
interpreted. Interpreted means an interpreter will read and execute the code instead of being
compiled into machine code. Examples are PHP and JavaScript.
1.6 Program Character Set
Program statement, keywords or reserved words are formed from a set of characters. These
characters are 26 English alphabet (Upper case and lower case) letters A to Z or a to z, ten digit
0 to 9, underscore etc.
1.7 Variables and Constants in Programming Languages
This is an alphabet or name that hold values or represent constant or an expression or another
variable. It is a symbolic memory location whose value can change during program execution.
Therefore, variable name can change when program is running. Examples of variable are
Studentname, Amount_paid, Regno, Department, interest_rate etc.
Rules for Naming Variable
 It must start with an English alphabet A-Z or a-z;
 You cannot use a space, period (.), exclamation mark(!), or the characters @, &, $,
# in the name;
 Name can’t exceed 255 characters in length;
 You can’t repeat names within the same level of scope;
 It cannot be a visual basic reserve words such as functions, statements, methods.
Some of the visual basic reserve words are Byte, ByVal, Case, Catch, CInt, Dim,
Boolean, Integer, Event, Exit, Global, Local, IsNot, Module, String, Loop, REM,
Double etc.
 It is always good programming practice to use names that are descriptive
Attributes of Variables
1. Name: a tag used to identify the variable
2. Address: location in the memory where the variable is stored
3. Type: the type of data the variable may store. The type determines the possible range of values
the variable can store, as well as how much space in memory the variable will occupy.
4. Value: the contents of the memory starting at the address and having the size as determined by
the type.
5. Scope: where in a program a variable is visible, i.e. where it can be referenced;
6. Lifetime: when a program a variable value is visible, i.e. when it can be referenced.
Constants in programing language
Constant is used to store values that cannot change during program execution. The values of
constant is initialized during data type declarations, and it is quite useful when the data values do
not change often in a program. Examples include the tax rate, pi, etc. While declaring a constants,
the keyword Const is used instead of Dim, and the actual value must be assigned. The general form
is:
Const Identifier [As Datatype] = value
Where identifier is the variable name, datatype is the type of data, and value is the actual value to
be assigned to the variable. For examples
Const strCompanyName As String = “Cookies and Tasty”
Const strCompanyAddress As String = “Computer Science Department, Caritas
University”
Const intLength as Integer = 25
Const dblAmount As Double = 86.43
1.8 Operators in Programming Languages
Operators are symbols or characters for arithmetic and comparison operation. It specifies what
operations to perform on operand or variables. Operators in visual basic can be categorized into
Arithmetic, Logical, Comparison, Concatenation, and assignment operators.
 Arithmetic Operators: these are used for arithmetic operations such addition, subtraction,
multiplication, division and exponentiation. Arithmetic operators are shown in Table 2
below
Table 2: Arithmetic operators in Visual Basic
Operators Meaning Functions Examples
() Bracket Grouping A*(B+C)
+ Addition Add two numbers a+b
- Subtraction Subtract two or more operands a-b
* Multiplication Multiply two or more operands a*b
/ Division Divide two operands a/b
\ Integer division It divides two numbers and returns an 5/2 = 2
integer result
Mod Modulus It divides two numbers and return only 5mod2 = 1
the remainder 4mod 2 = 0
8mod 4 = 0
^ Exponentiation Raises to the power of another number 3^2 = 9
4^3 = 64

 Logical Operators: These are used to perform logical operations or comparison of two or
more operands to result to Boolean values (true or false). Table 3 below shows the logical
operators in Visual Basic.
Table 3: Logical operators
Operators Description Examples (A = True or 1, B
= False or 0)
AND It will return true if both operands are non-zero A AND A = True, A AND B
(1) = False
OR It will return true if any one operand become a A OR B = True, B Or B =
non-zero False
NOT It will return the reverse of a logical state of that
means if both operand are non-zero, it will
return false.

 Relational Operators: These are used for comparisons among operands or variables and
other types of information in a program. The values return are either true or false. The
relations or comparison operators are shown in Table 4 below
Table 4: Relational operators
Operators Description Examples (a = 10,
b = 5)
< It returns true if the right operand is greater than the left a < b = False
operand
<= It will return true if the right operand is greater than or equal a <= b = False
to the left operand.
> It will return true if the left operand is greater than the right a > b = True
operand.
>= It will return true if the left operand is greater than or equal a >= b = True
to the right operand.
= It will return true if both operands are equal. a = b = False
<> It will return true if both operands are not equal. a <> b = True
Is It will return true if two object references refer to the same
object.
IsNot It will return true if two object references refer to different
objects.

 Assignment Operators: This is used to assign new values to operands. Typical assignment
operators and examples are shown in Table 5 below.
Table 5: List of Assignment operators
Operators Descriptions Examples
= It will assign a value to a variable or property. a = 10
+= It will add left and right operands and assign a result a += 10 equals to a = a +
to the left operand. 10
-= It will subtract left and right operands and assign a a -= 10 equals to a = a - 10
result to the left operand.
*= It will multiply left and right operands and assign a a *= 10 equals to a = a *
result to the left operand. 10
/= It will divide left and right operands and assign the a /= 10 equals to a = a / 10
floating-point result to the left operand.
\= It will divide left and right operands and assign the a \= 10 equals to a = a \ 10
integer result to the left operand.
^= It will raise the value of a variable to the power of a ^= 10 equals to a = a ^ 10
expression and assign the result back to the variable.
++ Increment operators that increase the value of a++ equal to a = a +1, 10 +
operand by 1 1 = 11
-- Decrement operator that decrease the value of a-- equal to a = a -1, 10 - 1
operand by 1 =9
 Concatenation Operators: Concatenation operators are useful to concatenate or join
defined operators based on requirements. Examples of concatenation operators are shown
in the table 6 below.
Table 6: Concatenation operators in Visual Basic
Operators Descriptions Examples (a = “Hello”, b =
“world”
& It will concatenate given two expression a & b = “Helloworld”
+ It is useful to add two numbers or concatenate two a + b = “Helloworld”
string expression

Operator Precedence in Programming Languages


When several operators occur in an expression, each part is evaluated and resolved in
predetermined order called precedence. Therefore, when expression contain operators from more
than one category, they are evaluated according to the following rules
 The arithmetic and concatenation operators have greater precedence than comparison,
logical, and assignment operators
 All comparison operators have equal precedence, and all have greater precedence than
logical, but lower precedence than the arithmetic and concatenation operators;
 The logical operators have lower precedence than arithmetic, concatenation and
comparison operators
 Operators with equal precedence are evaluated left to right in order in which they appear
in the expression.
1.9 Expression in programming languages
Expression is a combination of variable or/ constant connected with one or more operators. The
following mathematical expression can be converted to Visual Basic (VB) expression.
𝑥+𝑦
1. 3(𝑥−𝑦)
𝑎.𝑏(𝑡+1)
2. ℎ.𝑡+𝑣

3. (𝑞 − 𝑥)2
𝑎+𝑏 𝑒−𝑓
4. 𝑐−𝑑
÷ 𝑔+ℎ

5. (𝑎 + 𝑏 + 𝑐)/2
𝜋 𝑈𝑎𝑣
6. ( 100 )
4

−𝑏±√𝑏 2 −4𝑎𝑐
7. 2𝑎

The Visual Basic equivalent of the above mathematical expression are


1. (X + Y)/((3*(X – Y))
2. ((A * B)*(T + 1))/((H * T) + V)
3. (Q – X)^2
4. ((A + B)/(C – D))/((E – F)/(G + H))
5. (A + B + C)/2
6. (3.142/4)*((U*A*V)/100)
7. (-B+SQR(B^2-4*A*C))/(2*A)
Assignment Statement
Assignment statement is an expression assigned to a variable. An assignment statement is a simple
command that causes something to happen. Examples are:
A = 4*(X-3)
C=A+B
M = Sin (D)
X = 2*X
1.10 Translator
A translator is a program that translate another program written in a any programming language
other than machine language to an understandable set of codes for the computer. Translator is
necessary because compiler only understand machine language. Example of translator are
Assembler that convert program written in assembly language to machine language, and interpreter
and compiler that convert program in high level language into machine language. Compiler
convert entire source code (program) into object code before execution while interpreter translate
the source program line by line. Therefore, interpreted program is slower.
Features of translator
 Enhances program understanding by computer
 There are different translator for different types of programming language
 Program cannot execute without translator.
CHAPTER TWO: FUNDAMENTAL OF DATA STRUCTURE
2.1 Data Type and Data type declaration
Programs are designed to manipulate data in order to produce desired outputs. Data sources as
input to program, and comes in different forms. These include:
 Integer: Consist of positive and negative whole numbers e.g. 500, 200, 2, 6, -3, 77, -44.
Major integer data type are byte, short, and long.
 Real Number: These are numbers with fractional points. Examples are 268.1, 132.52, and
0.001. Standard real number data type are float, simple, double, extended.
 Non-numeric data types: These are data type whose values are not numbers such as
character and strings. Character consist representation of single alphabetic letter or
character using American standard code for information interchange (ASCII) characters.
While string data type are sequence of characters enclosed in double quotes.
 Complex data type: complex data types are nested data structure composed of primitive
data type such as integer, float, strings etc. complex data type can also be composed of
other complex data types such as struct(row), array, list, map and union. The data type is
supported by most programming languages including Python, C++ and Java. In addition,
they are supported by databases like PostgreSQL, which introduced composite (struct)
type. As earlier stated, some examples of complex data type are map, array, and struct.
Map data types are key-valued pairs where type is associated a key with a value. Example,
a city could be associated with zipcode, or an address with a phone number. Struct is a
composite data type that can contain other complex and simple data types.
Data type is useful to define the type of data the variable can hold such as integer, float, string etc.
As a good practice in programming language such as Visual Basic, variables are declared before
they are used in a program. A data type is a set of values and set of operations defined on the
values. There are twelve (12) primitive (built-in) data type in Visual Basic. These include Integer,
Byte, Boolean, Date, Double, Decimal, Long, Char, Date, Object, Single, String. Among these
data type, integer, byte, short, long are numeric values of whole numbers, while double, decimal
and single are real fraction values, char for individual character (letter), string for text, Boolean for
true or false, and object for storing object variables. Table 7 below depict some of these data types
and their ranges
Table 7: Visual Basic data types, sizes and range
Data Type Size Range
Integer 4 bytes -2,147,483,648 to 2,147,483,647
Long 8 bytes -9,223,372,036,854,775,808to
9,223,372,036,854,775,807
Byte 1 byte 0 to 255
Short 2 bytes -32,768 to 32,767
Double 8 bytes -1.79769313486232e308 to 1.79769313486232e308
Decimal 16 bytes (+ or -)1.0 x 10e-28 to 7.9 x 10e28
Single 4 bytes -3.4028235E+38 through -1.401298E-45 † for negative
values; 1.401298E-45 through 3.4028235E+38 † for
positive values
Char 2 bytes 0 to 65535
String Depend on platform 0 to approximately 2 billion Unicode characters
Boolean It depend on True or false
platform
Object 4 bytes on a 32-bit Any type can be stored in a variable of type object
platform, 8 bytes on
a 64-bit platform

2.2 Data Type Declaration


This is the process of assigning predefined data type to a variable or constant in a program. This
is done by appending the type of data to be stored in variable to the variable name using the Dim
statement, which means dimension. Examples include:
Dim Student_Name as String
Dim Length as Integer
Dim Double Amount
Dim Flag as Boolean
Dim Gender as Char
You can also prefix the data type to the variable to for easy identification. For examples,
Dim intAge As Integer
Dim intSum As Integer
Dim strFirstName As String
Dim intAge, intSum As Integer
Dim dblAmount as Double

2.3 Abstraction in Computer Programming


Abstraction is a mechanism for separating the properties of an object and restricting the focus on
those relevant in the current context. It is used to manage complex problem and complex data type.
The programmer does not have to understand all the details in order to use object in abstraction
but only pay attention to those relevant to the current task or problem. In object-oriented
programming, abstraction is one of the three principles (along with encapsulation and inheritance)
through which a programmer hides all but the relevant data about an object in order to reduce
complexity and increase efficiency in programming. The object that remain is represented as the
original with unwanted details omitted. The resulting object itself can be referred to as an
abstraction, meaning a named entity made up of selected attributes and behaviour specific to
particular usage of the originating entity. Abstraction is related to encapsulation and data hiding.
Types of abstraction
a. Procedural abstraction: is the use of function or method knowing what the method does
but ignoring how it is accomplished. Examples are the use of different functions such as
LEN, SQRT, MAX, MIN, INT etc.
b. Data abstraction: this is the separation of properties of a data type (its value and operation)
from the implementation of a data type. Example are string, float, integer etc.
2.4 Abstract data type (ADT)
Abstract data type is a collection of data object characterised by how the object are accessed.
These maybe a list, integer, instance of a class or integer. A data type is abstract because it is
independent of various concrete implementation of data type in the program. Abstract data type
are supported by object oriented programming language such as JAVA, Python, C++ etc., and
provide explicit support for expressing abstract data type using classes. Examples of abstract data
types are List, Array, Queue, Stack and Tree.
By definition, abstract data type is a programmer defined data type that specifies a set of values
and collection of well-defined operation that can be performed on those values. They are defined
independent of their implementation, allowing us to focus on the use of the data type instead of
how it is implemented. The separation is enforced by requiring interaction with the abstract data
type through an interface or defined set of operation. This approach is known as information
hiding. Information hiding requires ADT to be accessed through interface enable programmer
work with abstraction and focus on what functionality the ADT provides instead of how that
functionality is implemented.
ADT is a black box that enable use of programs that interact with instances of ADT by invoking
one of the several operations, defined interface as shown in figure 1. The implementation of the
various operations are hidden inside the black box, the contents of which we do not have to know
in order to utilize the ADT.

Figure 1: Separating the ADT definition and its implementation


These ADT operations include:
 Constructor: create and initialize new instances of abstract data type
 Assessor: return data contained in an instance without modifying it
 Mutator: modify the content of abstract data type instances
 Iterator: process individual data component sequentially
2.4.1 Advantages of ADT
 We can focus on solving the problem at hand instead of getting bogged down by
implementation details
 Helps to reduce logical error that occur from accidental misuse of storage structure and
data type by preventing direct access to the implementation
 The implementation can be changed without having to modify the program code that use
the ADT
 It is easy to manage and divide larger programs into smaller module, allowing different
members of a team to work on the separate modules
2.4.2 Types of abstract data type
 Simple abstract data type is composed of a single or several individually name data field
 Complex abstract data type is composed of a collection of data values such as the python
list or dictionary. Complex abstract data type are implemented in a particular
programming language
CHAPTER THREE: DATA STRUCTURE
3.1 Introduction
Data structure is the implementation of an abstract data type in particular programming language.
It is the physical representation of how data is organized and manipulated. Therefore, data structure
is characterised by how program store and organize the individual data element and what
operations are available for accessing and manipulating the data. Data Structure can be defined as
the group of data elements which provides an efficient way of storing and organizing data in the
computer so that it can be used efficiently. Some examples of Data Structures are arrays, Linked
List, Stack, Queue, etc. Data Structures are widely used in almost every aspect of Computer
Science i.e. operating System, Compiler Design, Artificial intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as they enable the
programmers to handle the data in an efficient way. It plays a vital role in enhancing the
performance of a software or a program as the main function of the software is to store and retrieve
the user's data as fast as possible. Common data structure include arrays, linked list, stack, queue,
tree etc. The main feature of data structure is the ability to store a collection of values, however,
each value differs in how they organize the individual items and by what operations can be applied
to manage the collection.
3.2 Data structure terminologies
Data structures are the building blocks of any program or the software. Choosing the appropriate
data structure for a program is the most difficult task for a programmer. Following terminology is
used as far as data structures are concerned.
 Data: Data can be defined as an elementary value or the collection of values, for example,
student's name and its id are the data about the student.
 Group Items: Data items which have subordinate data items are called Group item, for
example, name of a student can have first name and the last name.
 Record: Record can be defined as the collection of various data items, for example, if we
talk about the student entity, then its name, address, course and marks can be grouped
together to form the record for the student.
 File: A File is a collection of various records of one type of entity, for example, if there are
60 employees in the class, then there will be 20 records in the related file where each record
contains the data about each employee.
 Attribute and Entity: An entity represents the class of certain objects. It contains various
attributes. Each attribute represents the particular property of that entity.
 Field: Field is a single elementary unit of information representing the attribute of an
entity.

3.3 The need for data structure in programming


As applications are getting complexed and amount of data is increasing day by day, there may
arise the following problems:
 Processor speed: To handle very large amount of data, high speed processing is required,
but as the data is growing day by day to the billions of files per entity, processor may fail
to deal with that much amount of data.
 Data Search: Consider an inventory size of 106 items in a store. If our application needs
to search for a particular item, it needs to traverse 106 items every time, results in slowing
down the search process.
 Multiple requests: If thousands of users are searching the data simultaneously on a web
server, then there are the chances that a very large server can be failed during that process
in order to solve the above problems, data structures are used. Data is organized to form a
data structure in such a way that all items are not required to be searched and required data
can be searched instantly.
3.4 Common Operations in Data Structure
The major or the common operations that can be performed on the data structures are:
 Searching: We can search for any element in a data structure.
 Sorting: We can sort the elements of a data structure either in an ascending or
descending order.
 Insertion: We can also insert the new element in a data structure.
 Updating: We can also update the element, i.e., we can replace the element with
another element.
 Deletion: We can also perform the delete operation to remove the element from the
data structure.
 Merging: When two lists List A and List B of size M and N respectively, of similar
type of elements, clubbed or joined to produce the third list, List C of size (M+N), then
this process is called merging
 Traversing: Every data structure contains the set of data elements. Traversing the data
structure means visiting each element of the data structure in order to perform some
specific operation like searching or sorting.
3.5 Advantages of data structure
Efficiency: Efficiency of a program depends upon the choice of data structures. For example:
suppose, we have some data and we need to perform the search for a particular record. In that case,
if we organize our data in an array, we will have to search sequentially element by element. Hence,
using array may not be very efficient here. There are better data structures which can make the
search process efficient like ordered array, binary search tree or hash tables.
Reusability: Data structures are reusable, i.e. once we have implemented a particular data structure,
we can use it at any other place. Implementation of data structures can be compiled into libraries
which can be used by different clients.
Abstraction: Data structure is specified by the ADT which provides a level of abstraction. The
client program uses the data structure through interface only, without getting into the
implementation details.
Disadvantages of data structure
 A user who has deep knowledge about the functionality of the data structure can make
changes to it.
 If there is an error in the data structure, an expert can detect the bug. The original user
cannot help themselves solve the problem and fix it.
3.6 Classification of data structure
Data structure is broadly categorized into primitive and non-primitive data structure as shown in
figure 2.
Figure 2: Classification of data structure (https://www.javatpoint.com/primitive-vs-non-
primitive-data-structure)

1. Primitive data structure: Primitive data structure is a data structure that can hold a single
value in a specific location whereas the non-linear data structure can hold multiple values
either in a contiguous location or random locations.
2. Non-primitive data structure: The non-primitive data structure is a kind of data structure
that can hold multiple values either in a contiguous or random location. The non-primitive
data types are defined by the programmer. The non-primitive data structure is further
classified into two categories, i.e., linear and non-linear data structure.
3.7 Differences between primitive data structure and non-primitive data structure
Table 8: difference between primitive and non-primitive data structure
Primitive Data structure Non-primitive data structure
Primitive data structure is a kind of data structure that Non-primitive data structure is a type of data structure
stores the data of only one type. that can store the data of more than one type.
Examples of primitive data structure are integer, Examples of non-primitive data structure are Array,
character, and float. Linked list, stack.
Primitive data structure will contain some value, i.e., it Non-primitive data structure can consist of a NULL
cannot be NULL. value.
The size depends on the type of the data structure. In case of non-primitive data structure, size is not fixed.
It starts with a lowercase character. It starts with an uppercase character.
Primitive data structure can be used to call the methods. Non-primitive data structure cannot be used to call the
methods.

3.8 Types of primitive data structure

 Integer: The integer data type contains the numeric values. It contains the whole numbers
that can be either negative or positive. When the range of integer data type is not large
enough then in that case, we can use long.
 Float: The float is a data type that can hold decimal values. When the precision of decimal
value increases then the Double data type is used.
 Boolean: It is a data type that can hold either a True or a False value. It is mainly used for
checking the condition.
 Character: It is a data type that can hold a single character value both uppercase and
lowercase such as 'A' or 'a'.

3.9 Types of Non-primitive data structure

a. Linear: elements are stored and accessed linearly in computer memory, and the elements
are connected to the previous and the next element. The element can be traversed and
accessed in single run. It is easier to implement linear data structure as the element are
organized sequentially in memory. Examples are array, list, stack, string and queue.
 Array: An array consists of data elements of a same data type. For example, if we
want to store the roll numbers of 10 students, so instead of creating 10 integer type
variables, we will create an array having size 10. Therefore, we can say that an
array saves a lot of memory and reduces the length of the code.
 Stack: It is linear data structure that uses the LIFO (Last In-First Out) rule in which
the data added last will be removed first. The addition of data element in a stack is
known as a push operation, and the deletion of data element form the list is known
as pop operation.
 Queue: It is a data structure that uses the FIFO rule (First In-First Out). In this rule,
the element which is added first will be removed first. There are two terms used in
the queue front end and rear. The insertion operation performed at the back end is
known as enqueue, and the deletion operation performed at the front end is known
as dequeue.
 Linked list: It is a collection of nodes that are made up of two parts, i.e., data
element and reference to the next node in the sequence.
 String: String is defined as an array of characters. The difference between the
character array and string is that the string data structure terminates with a 'NULL'
character, and it is denoted as a '\0'.
b. Non-linear: data elements are not store and accessed in sequence. Examples of nonlinear
data structure are Tree and graph.
 Tree: It is a non-linear data structure that consists of various linked nodes. It has a
hierarchical tree structure that forms a parent-child relationship.
 Graph: A graph is a non-linear data structure that has a finite number of vertices
and edges, and these edges are used to connect the vertices. The vertices are used
to store the data elements, while the edges represent the relationship between the
vertices. A graph is used in various real-world problems like telephone networks,
circuit networks, social networks like LinkedIn, Facebook. In the case of Facebook,
a single user can be considered as a node, and the connection of a user with others
is known as edges.

3.10 Types of Linear Data Structure

1. Static linear data structure: In Static data structure the size of the structure is fixed. The
content of the data structure can be modified but without changing the memory space
allocated to it. Static data structure provides easier access to data elements. Example of
static data structure is the array. Figure 3 shows static data structure process

Figure 3: Static data structure


2. Dynamic Data Structure: In Dynamic data structure shown in figure 4, the size of the
structure in not fixed and can be modified during the operations performed on it. Dynamic
data structures are designed to facilitate change of data structures in the run time. The
memory size of dynamic data structure can be randomly updated during run time which
may be considered efficient with respect to memory complexity of the program code.
Dynamic data structure is used to increase the memory limit during competitive
programming.

Figure 4: Dynamic data structure

3.11 Characteristics of Data structure


Data structure is defined the following characteristics
 Linear: A linear describes data characteristics whether the data items are arranged in
sequential form like an array.
 Non-Linear: A Non-Linear data structure describes the characteristics of data items that
are not in sequential form like a tree, graph.
 Static: It is a static data structure that describes the size and structures of a collection of
data items associated with a memory location at compile time that are fixed. Example -
Array.
 Homogenous: It is a characteristic of data structures representing whether the data type of
all elements is the same. Example- Array.
 Non-Homogenous: It is a characteristic of data structures representing whether the data
type elements may or may not be the same.
 Dynamic: It is a dynamic data structure that defines the shrinking and expanding of data
items at the run time or the program's execution. It is also related to the utilization of
memory location that can be changed at the program's run time. Example: Linked list.
 It has some rules that define how the data items are related to each other.
 It defines some rules to display the relationship between data items and how they interact
with each other.
 It has some operations used to perform on data items like insertion, searching, deletion,
etc.
 It helps in reducing the usage of memory resources.
 Time Complexity: The execution time of a program in a data structure should be minimum
as possible.
 Space Complexity: The memory usage through all data items in a data structure should be
as less as possible.
CHAPTER FOUR: LINEAR AND NON-LINEAR DATA STRUCTURE
In this chapter, examples of linear and non-linear data structure are explained. Typical linear data
structure include array, linked list, stack and queue while non-linear data structure include tree and
graph.
4.1 Linear data structure
As earlier explained in chapter three, in linear data, elements are stored and accessed linearly in
computer memory, and the elements are connected to the previous and the next element. Typical
examples are array linked list, stack and queue. This section highlight their features, advantages,
and their applications.
4.1.1 Arrays
This is a sequence of data objects all of the same type that are collectively referenced by the same
name. Each individual array elements (data items can be referred to by specifying the array name
followed by index (subscript), enclosed in parenthesis. Array can be one dimensional or multi-
dimensional. An array data structure is a collection of elements of the same data type used to store
data in contiguous memory locations. It has a fixed size collection of data elements that cannot be
changed at runtime of a program. It is mostly used in a computer program to organize the data so
that related items or values can be easily searched or sorted in a system.
In single dimensional array, entire contents of the array are identified by single name. Individual
element within the area can be accessed directly by specifying an integer subscript or index value
that indicate the offset from the start of the array. It is similar to mathematical notation (xi),
which allows multiple variable of the same name. However, programming language uses square
bracket following name to specify the subscript X[i]. For instance,
Studentname (10)
Studentregno (1 to 20)
Costofgoods (1 to 100)
Therefore. One-dimensional array is a collection of contiguous elements in which individual
elements are identified by a unique integer value. On the other hand, two-dimensional array is a
collection of elements into rows and columns, and elements are refereed/referenced by specifying
the specific row and column indices (r,c) and both start form zero. Figure 5 shows the diagram
depicting array process.
A. Reasons for studying array data structures
 Required for a problem with sequence in which the maximum number of the elements are
known before hand
 It provide fixed storage space for implementation of a problem

Figure 5: Arrays
B. Advantages of Array data structure
 A convenient way to store large amounts of similar data.
 It is used to represent multiple items of similar nature using a single name.
 It allows us to store data in multi-dimensional arrays
 It is used to create other data structures like heaps, linked lists, etc.
 You need not access elements sequentially, random access is allowed.
 Since elements are stored in consecutive blocks hence they are useful to use in iterations.

 Since the size of the array is known during compile-time, there is no chance of memory
runout for arrays.
C. Disadvantages of array data structure
 Difficult to modify the array size during program execution due to its fixed size
 Inserting and deleting elements from an array is tedious as it involves shifting data
elements of the array and memory management
 No element can be appended at the end of array
 Lead to memory wastage as array is declared during compile time
 Insertion and deletion operations are time-consuming
D. Application of Array data structure
 Implementation of other data structure such as linked lists etc.
 Uses in lockup table in program application
 Use to implement database record
 It effectively executes memory addressing logic wherein indices act as addresses to the
one-dimensional array of memory
 Used in mathematical problem such as matrices, indices etc.
4.1.2 Linked List
It is a collection of data links called nodes where each node contains a data value and the address
of the next link. All elements of the linked list are not stored in neighboring memory locations. In
simple words, it is a sequence of data nodes that connect through links. Each node of a list is
consists of two items: the basic idea behind the uses of a linked list in a data structure is that each
node within the structure includes a data part where we store values, and a pointer indicates the
next node can be found. The linked list's starting point denotes the head of the list, and the
endpoints represent the node's tail.
Also, list is a sequence of zero or more elements called nodes, each containing two kind of
information-some data and one or more links to a pointer to the node of the link list. The nodes are
connected in sequence to for a linear list. The last node in the list is called tail node, and it is
indicated by a null link reference. In addition, the first node in the list must be named or referenced
by an external variable as it provide the entry point into the linked list. The variable is commonly
called the head point or head reference. A linked list can be empty which is indicated when the
head reference is null. Figure 6 provides an example of a linked list consisting of four nodes.
A. Application of Linked List
 Implementation of image viewer where previous and next image are linked
 Enable access of previous and next web browser by pressing back or next button since
they are linked as linked list
 In music player to access next and previous song in play list
 Implementation of stacks and queues
 Implementation of graphs
 Dynamic memory allocation: We use linked list of free memory blocks.
 Maintaining directory of names
 Performing arithmetic operations on long integers
 Manipulation of polynomials by storing constants in the node of linked list representing
sparse matrices.
Figure 6: Linked List
4.1.3 Stack
It is a linear data structure that enables the elements to be inserted and deleted from one end, called
the Top of Stack (TOS). A stack data structure follows the last in first out (LIFO) operation to
insert and remove an element from the stack list. It is an abstract data type with two possible
operations to insert and delete elements in the stack: push and pop. A push operation is used in the
stack to insert elements at the top of the list, hide those elements that already available in the list,
or initialize the stack if it is empty. The pop operation is used in the stack data structure to delete
a data item from the top of the list. Adding and removing items in a stack is restricted to one end
of the stack called the top of the stack. An empty stack is the one containing no items.
Common stack operations are:
 Stack(): Create empty stack
 Push(): insert element at the top of the stack list
 Pop(): delete data element form the top of the list
 Peek (): Return a reference to the item on top of a non-empty stack without removing it.
Peek, which cannot be done on empty stack does not modify the stack contents.
 isFull(): check if stack is full
 isEmpty(): return a Boolean value indicating if the stack is empty.
 Length():Return the number of items in the stack
A. Applications of stack
 Reversing words
 Checking parenthesis matching in expression
 Expression evaluation
 Conversion from one form of expression to another
 Memory management
 Backtracking problem
Figure 7: Stack data structure
(https://guides.codepath.com/compsci/Stacks-and-Queues)

4.1.4 Queue
It is a linear data structure that enables the insertion at one end of the list, known as the rear, and
the deletion of the elements occurred at another end of the list, called the front. It is a sequential
collection of data elements based on the First in First out (FIFO) data structure, which means the
first inserted elements in a queue will be removed first from the queue list. Queue is suited for a
problem in computer science that require data to be processed in order in which it was received.
Examples are in computer simulation, CPU process scheduling, and shared printer management.
In queue data structure, new items are inserted at the back and existing items are removed from
the front. Items are maintained in the order in which they are added to the structure.
Important queue functions are described below:
 Queue(): create new empty queue, that is, queue containing no item.
 Enqueue(): It is a queue operation used to insert an element to the queue.
 Dequeue(): It is a queue operation used to delete an item from the queue.
 Length(): returns the length of items currently in the queue
 IsEmpty(): return a Boolean value indicating whether the queue is empty.
Example code snippets below show examples for creating and adding items to a given queue.
Q = Queue()
Q.enqueue(20)
Q.enqueue(19)
Q.enqueue(45)
Q.enqueue(13)
Q.enqueue(9)

Figure 8: Queue data structure


A. Application of Queue
 Queue data structure is used where first in first out (FIFO) is required. Some include:
 Managing request on a single shared resources such as CPU scheduling and disk scheduling
 Handling hardware on real-time system interrupts
 Handling website traffic
 Router and switch networking
 Maintaining the playlist in media player
B. Implementation of Queue data structure
 Sequential allocation: It can be implemented using an array. A queue implemented using
an array can organize only a limited number of elements.
 Linked list allocation: It can be implemented using a linked list. A queue implemented
using a linked list can organize an unlimited number of elements.
4.2 Non-linear data structure
Data elements in non-linear data structure are neither stored nor accessed in sequence.
Representative examples of non-linear data structure are tree and graph structure. Elements in non-
linear data structure follows a hierarchical order and arranged in multiple levels.
4.2.1 Tree structure
It is a non-linear data structure that consists of various linked nodes. It has a hierarchical tree
structure that forms a parent-child relationship. The order of data elements in tree structure is not
important. Figure 9 represent the typical examples of three data structure.

Figure 9: Tree data structure


Tree data structure is made up of different components as explained below.
 Root: root of a tree is the node with no parent. There can be at most one root node in a tree
(Node A in the example above)
 Edge: edge refers to the link from parent to child
 Node with no children is called leaf node ( E, J, K, H, and I)
 Children of same parent are called siblings (B, C, D are siblings of A, and E, F, are siblings
of B)
 Node P is an ancestor of node Q if there exist a path from root to Q and P appears on the
path. The node Q is called descendent of P. for examples, A, C, and G are the ancestor of
K.
 The set of all nodes at a given depth is called the level of the three (B, C, and D are the
same level). The root node is at level zero
 The depth of a node is the length of the path from the root to the node (depth of G is 2, A-
C-G)
 The height of a node is the length of the path from that node to the deepest node. The height
of a tree is the length of the path from the root to the deepest node in the tree. A rooted tree
with only one node (the root) has a height of zero. In the previous example, the height of
B is 2 (B-F-J)
 The height of the tree is the maximum height among all the node in the tee
 Depth of the tree is the maximum depth among all the node in the tree. For a given tree,
the depth and height returns the same value.
 The size of a node is the number of descendants it has including itself (the size of the
subtree C is 3).
 If every node in a tree has only one child (except leaf nodes) then we can call such tree
skew tree. If every node has only left child then we call them left skew tree and if every
node has only right child then we call them right skew trees. Examples is shown in figure
10 below,

Figure 10: types of leaf node in tree structure


Binary tree: A tree is called binary tree if each node has zero child, one child or two children.
Empty tree is also a valid binary tree. We can visualize a binary tree as consisting of a root and
two disjoint binary trees, called the left and right subtrees of the root. Binary tree can be strict, full
or complete. Strict binary tree has two or no children in each node. Full binary tree has exactly
two children in each node and all leaf node are at the same level. In complete binary tree, all the
leaf nodes are at height h or h-1 and also without any missing number in the sequence. Here, h is
the height of the binary tree.
A. Application of Tree data structure
 To implement priority queue
 Use for scanning, parsing, generation of code and evaluation of arithmetic expression in
compiler construction through syntax tree
 Use to implement dictionaries with prefix lookup
 Serves as workflow for composition of digital image for visual effect
 To implement decision tree based machine learning
 To organize chart in a large organization
 Manipulate hierarchical data
4.2.2 Graph Data structure
A graph is a non-linear data structure that involve collection of objects or entities known as nodes
that are connected to each other through a set of edges. A graph contains a set of edges and nodes,
and edges can connect the nodes in any possible way.
Mathematically, it can be defined as an ordered pair of a set of vertices, and a set of nodes where
vertices are represented by 'V' and edges are represented by 'E'.
Therefore graph is represented mathematically as G= (V, E). Here we are referring to an ordered
pair because the first object must be the set of vertices, and the second object must be a set of
edges. Typical representation of graph data structure sample is given in figure 11.

Figure 11: Graph Data structure


As shown in figure 11, the set of vertices V = {0, 1, 2, 3, 4} and the set of edges E = {01, 12, 23, 34,
04, 14, 13}.
A. Common Graph Terminologies
 Vertex: each node of the graph is represented as a vertex. In figure 11, the labeled circle
represent the vertex. Thus 1 to 4 are vertices.
 Edge: each represents path between tow vertices or a line between two vertices. In figure 11,
the line from 0 to 2, 2 to 3 etc. represent the edge of the graph. The edge is represented using
two-dimensional array such as 01 where 0 represent the row and 1 the column of the graph.
 Undirected edge: bidirectional edge
 Directed edge: unidirectional edge
 Weighted edge: An edge with value (cost) on it.
 Adjacency: two nodes or vertices are adjacent if they are connected to each other through an
edge. In the figure above, 0 is adjacent to 1, and 1 is adjacent to 2 etc.
 Path: path represents a sequence of edges between two vertices. Here, 0123 represent path
from o to 3.
 Degree: total number of edge connected to a vertex in a graph.
 Indegree: total number of incoming edges connected a vertex.
 Outdegree: total number of outgoing edge connected to a vertex.

B. Graph operations
Following are basic primary operations of a Graph −
 Add Vertex: Adds a vertex to the graph.
 Add Edge: Adds an edge between the two vertices of the graph.
 Display Vertex: Displays a vertex of the graph.
 Find the path from one vertex to another vertex
C. Types of Graph
a. Directed Graph: In a directed graph, nodes are connected by directed edges – they only go
in one direction. For example, if an edge connects node 1 and 2, but the arrow head points
towards 2, we can only traverse from node 1 to node 2 – not in the opposite direction.
Figure 12: directed graph
b. Undirected Graph: In an undirected graph, nodes are connected by edges that are all
bidirectional. For example if an edge connects node 1 and 2, we can traverse from node 1
to node 2, and from node 2 to 1.

Figure 13: Undirected graph


c. Weighted Graph: A graph that has a value associated with every edge. The values
corresponding to the edges are called weights. A value in a weighted graph can represent
quantities such as cost, distance, and time, depending on the graph. Weighted graphs are
typically used in modeling computer networks.
An edge in a weighted graph is represented as (u, v, and w), where:
 u is the source vertex
 v is the destination vertex
 w represents the weight associated to go from u to v
Figure 14: Weighted graph
d. Unweighted graph: A graph in which there is no value or weight associated with the edge.
All the graphs are unweighted by default unless there is a value associated.
An edge of an unweighted graph is represented as (u, v), where:
 u represents the source vertex
 v is the destination vertex
D. Applications
 To represent networks. The network may include the path in a city or telephone network
or circuit network
 To represent relationship in social network such as LinkedIn, Facebook and Twitter
 Help to define the flow of computation of software program
 Used in Google map for building transportation systems. Google algorithm map uses graph
to calculate the shortest distance between two vertices
 Used in operating systems for efficient resources allocation where every process and
resources act as a node while edge are drawn from resources to the allocated process
 Used in the World Wide Web where the web pages represent the nodes.
 Used in data modeling
 Other application areas are knowledge graph, biological networks, neural networks and for
product recommendation
CHAPTER FIVE: INTRODUCTION TO ALGORITHM
5.1 Introduction
Computer algorithm is an integral part of programming and data structure. It describe set of well-
defined steps to solve a particular problem. This chapter describes algorithms, properties and types
of algorithms. In addition, the chapter describe specific application of computer algorithm in items
sorting and searching process. In sorting processes, algorithms such as bubble sort, merge sort,
selection sort, insertion sort and quick sort algorithms are explained. Moreover, the chapter
describe searching algorithms such as linear and binary search algorithms.

5.2 Definition of algorithm


Algorithm is an explicit, precise, unambiguous, mechanically-executable sequence of elementary
instructions, usually intended to accomplish a specific purpose. Starting from an initial state, the
instruction describe the process or computational process that when executed, proceed through a
finite number of well-defined successive states, eventually producing output and terminate at a
final ending state. Algorithm is the building blocks of computer programming and allow devices
such as computers, smartphones and website to function and make decisions. Therefore, algorithm
defines set of instruction created to execute well thought instructions in a certain order to get the
desired output. They are generally created independent of underlying languages in which it will be
implemented. From the data point of view, algorithms can be categorized into:
 Search: algorithm to search for an item in a data structure. Examples include the binary
search, sequential search, interval search and linear search algorithms.
 Sort: algorithm to sort items in a certain order and categories such as bubble sort, merge
sort, selection sort etc.
 Insert: algorithm to insert item in a data structure
 Update: algorithm to update an existing item in a data structure
 Delete: algorithm to delete an existing item from data structure

5.3 Examples of Algorithm


Example 1: Algorithm to multiply two numbers and print the result

Solution:
Step 1: Start
Step 2: Get the knowledge of input.
Here we need 3 variables a and b will be the user input and c will hold result.
Step 3: Declare a, b, c variables.
Step 4: Take input for a and b variable from the user.
Step 5: Know the problem and find the solution using operators’ data structures and logic. We
need to multiply a and b variables so we use * operator and assign result to c.
That is c <- a * b
Step 6: Check how to give output
Here we need to print the output. So write print c
Step 7: End

Example 2: Write an algorithm to find the maximum of all the elements present in the
array.
Solution:
Step 1: Start
Step 2: Declare a variable max with value of first element of array.
Step 3: Compare the max with other elements using loop.
Step 4: If max < array element value change max to new max.
Step 5: If no element left return or print max otherwise go to step 3.
Step 6: End

Example 3: Write an algorithm to find the average of 6 subjects.

Solution:
Step 1: Start
Step 2: Declare a variable sum with value 0.
Step 3: Get sum of all the values in sum variable using loop.
Step 4: Divide sum by 6 and assign it to avg variable.
Step 5: Print avg
Step 6: End

5.4 Characteristics of algorithm


 Finiteness: an algorithm should have finite number of steps and it should end after a finite
time
 Input: it requires some input values. An algorithm should have 0 or more well defined
input.
 Output: algorithm should have 1 or more well-defined outputs, and should match well
desired output
 Unambiguous: a perfect algorithm is defined as unambiguous, which means that its
instructions should be clear, straightforward and concise.
 Effectiveness: each step of the algorithm must be simple and should take a finite amount
of time
 Independent: an algorithm should have step by step direction, which should be independent
of any programming language code. It should be such that it could be run on any of the
programming language
 Correctness: every input and output produce by the algorithm must be correct. Also, the
algorithm sequence must be corrected.
5.5 Advantages of computer algorithm
 Easy to understand: Since it is a step-wise representation of a solution to a given
problem, which makes it easy to understand.
 Language independent: It does not dependent on any programming language, so it can
easily understand by anyone.
 Debug / Error Finding: Every step is independent / in a flow so it will be to spot and fix
the error.
 Sub-Problems: It is written in a flow so now the programmer can divide the tasks which
will be easier to code
5.6 Disadvantages of algorithm
 Writing an algorithm takes a long time, so it is time-consuming
 Branching and looping statements are difficult to show in algorithms

5.7 Factors to Consider when designing an algorithms


 Modularity: This feature was perfectly designed for the algorithm if you are given a problem
and break it down into small-small modules or small-small steps, which is a basic definition of
an algorithm.
 Correctness: An algorithm's correctness is defined as when the given inputs produce the desired
output, indicating that the algorithm was designed correctly. An algorithm's analysis has been
completed correctly.
 Maintainability: It means that the algorithm should be designed in a straightforward, structured
way so that when you redefine the algorithm, no significant changes are made to the algorithm.
 Functionality: It takes into account various logical steps to solve a real-world problem.
 Robustness: Robustness refers to an algorithm's ability to define your problem clearly.
 User-friendly: If the algorithm is difficult to understand, the designer will not explain it to the
programmer.
 Simplicity: If an algorithm is simple, it is simple to understand.
 Extensibility: Your algorithm should be extensible if another algorithm designer or programmer
wants to use it.

5.8 Types of algorithm


 Divide and conquer algorithms
 Brute force algorithm
 Randomized algorithm
 Recursive algorithm
 Greedy algorithms
 Backtracking algorithms
 Dynamic programming algorithms
 Branch and bound algorithm
These types of algorithms are explained below:
1. Divide and conquer algorithms: in this types of algorithms, the problem is divide into
smaller sub-problem of the same type, then, the smaller problems are solved and combined
together to produce the final solution to the problem. Divide and conquer algorithms are
implemented in quick and merge sort algorithms.
2. Brute force algorithm: try every possible solutions to the problem until optimal or
satisfactory solution is found. Brute force algorithm can be optimizing –where best solution
can be found, however, it require finding all solutions, or if a value for the best solution is
known, it may stope when any best solution is found. It is also satisficing- Stop as soon as
a solution is found that is good enough. Example: Finding a travelling salesman path that
is within 10% of optimal. Often, brute force algorithm require exponential time. Various
heuristic and optimization can be used to decide which possibilities to look at first and
ways to eliminate certain possibilities without fully exploring them.
3. Randomize algorithm: randomize algorithm utilize random number in its logic to make
decision. Randomize algorithm are applied in Quick sort where random number is used to
pick a pivot, factoring a large prime number require choosing random number as possible
divisors.
4. Recursive algorithm: this type of algorithm uses recursive approach, thereby break problem
into sub-problems of the same type and calling own self repeatedly until the problem is
solved. Recursive algorithm is used to solve problem related to factorial of a number,
Fibonacci series, calculating the sum of an array of numbers, sorting a list or an array of
number etc. in some case, the problem to be solved by recursive algorithm is reduced into
a simpler instances of the same problem. To count the number of element in a list, recursive
algorithm is implemented. Here, if the list is empty, zero value is returned, otherwise, step
past the first element and count the remaining element in the list, and then add one to the
list.
5. Greedy algorithm: this is an optimization problem where the result is to find not just a
solution to a problem but the best solution that produce result. In greedy algorithm, all
possible solutions that can provide the best solution depending on the problem are selected.
Next part in greedy algorithm is selected based on the decision it will produce immediate
benefit, and choices selected previously is not selected. Examples of greedy algorithm are
Prim’s algorithm. Kruskat’s algorithm and Huffman Coding, and the traveling salesman.
6. Backtracking algorithm: Backtracking algorithm solve problem using incremental
approach. In this case, the problem is solved by building a solution one piece at a time,
remove solutions that fails to satisfy the constraint of the problem at any point of time.
Each time backtracking algorithm decide between several alternatives for the next part of
the problem, it recursively evaluates each alternative to choose the best solution for the
problem. Therefore, it is used to make sequence of decisions to build recursively defined
structure satisfying certain constraints. Examples of backtracking algorithms are game tree,
text summarization, binary search, and subset sum.
7. Dynamic programming algorithm: dynamic programming is commonly used in
optimization techniques. The algorithm approach divide complex problem into smaller
overlapping sub-problem and store the result for future use. The results of the overlapping
sub-problem is remembered by the algorithm and used to find new results. In this case,
multiple solutions exist, need to find the “best” one and requires “optimal substructure”
and “overlapping sub-problems”. Optimal substructure contains optimal solutions to sub-
problems while overlapping sub-problems can be stored and reused in a bottom-up fashion.
Dynamic programming algorithm techniques differ from Divide and Conquer, where sub-
problems generally need not overlap. Examples of dynamic programming are finding the
Fibonacci number, weighted job scheduling, find the shorted path in network.
8. Branch and bound algorithm: this is another algorithm for solving combinatorial
optimization problems. Combinatorial optimization problem are typically exponential in
term of complexity and may require exploring all possible permutation in worst case. It
build a tree of sub-problem as the algorithm progress where the original problem is
considered the root and sub-problem as the branches. Branch and bound algorithm is used
to construct upper and lower bound for a given problem, the upper bound represent the root
and lower bound represent the node. At each node, bounding method is applied if the
bounds match, it is considered feasible solution to that particular sub-problem. If the
bounds do not match, the problem is partition the problem represented by that problem and
make two sub-problems into children nodes. The procedure continue, using the best known
solution to trim the sections of the tree, until all the nodes have been solved or trimmed.
Example of branch and bound algorithm is the traveling salesman problem where salesman
has to visit each of n cities (at least) once each, and wants to minimize total distance
traveled. The salesman has to find the shorted route through a set of cities visiting each city
once. This can be divided into two: shortest route visiting city A first and shortest route not
visiting city A first. This process continue subdividing similarly as the city grows.

5.9 Sorting Algorithms

Sorting algorithms are computer science algorithms used to rearrange element in a list in order.
The arrangement is done according to a comparison operator on the elements which decide the
new order of the element in the respective data structure. In computer science, we have different
sorting algorithms used to arrange items in ascending or descending order. Some the sorting
algorithms include bubble sort, insertion sort, merge sort, quick sort, selection sort and shell sort.

5.9.1 Sorting strategies

 Enumeration sorting: consider all items. If we know that there are N items which are
smaller than the one we are currently considering, then its final position will be at number
N+1.
 Exchange sorting: if two items are found to be out of order, exchange them. Repeat till all
items are in order
 Selection sorting: find the smallest item, put it in the first position, find the smallest of the
remaining items, and put it in the second position. This process is repeated until the whole
item is sorted.
 Divide and conquer: recursively split the number problem into smaller sub-problems till
you just have single items that are trivial to sort. Then, put the sorted part back together in
a way that preserve the sorting.

All the strategies are based on comparing items and then rearranging them accordingly. These are
known as comparison-based sorting algorithms. The sorting approach is based on the assumption
that the items to be sorted can fit into computer memory, and they are referred to as internal sorting
algorithms.

5.9.2 Sorting algorithm

This subsection explains bubble sorting algorithm as an aspect or sorting algorithm.

A. Bubble sort

Bubble sort is a simplest sorting algorithm. The sorting algorithm is comparison-based algorithm
in which each pair of adjacent item elements is compared with the next element and the elements
are then swapped if they are out of order. Using this approach, bubble algorithms makes (n-1)
passes to completely sort set of items. During the first pass, R1 and R2 are compared, and if R1 is
greater than R2, then R1 and R2 are interchanged. The process is repeated for R2, R3, R4 etc., in
the same passes. The procedure will cause elements with smaller values to move up (bubble up)
after the first pass, and the element with the largest value will be in the nth position (last position).
Therefore, on the subsequent passes, the element with the next largest values will be placed in the
position (n-1), (n-2), (n-3),… 4, 3, 2, respectively, thereby resulting in sorted list. After each
passes, the subsequent passes will be performed on the first (n-ith passes) elements in the list since
all the elements, starting from the last position, up to the (n-i) position have taken their appropriate
positions.
After each pass through the list, a check could be made to determine whether or not any
interchanges were made during that pass. If no interchange was made, then the list must have been
sorted at that point and no further passes are required. Otherwise, we loop onto the next pass.
The algorithm for bubble sorting is given below.
1. Loop on the pass index, and repeat the steps 2 and 3 for j = 1, 2, 3, ….., n-1
2. Initialize interchange indicator and set CHECK = 0
3. Make passes
a. Repeat steps for j = 1, 2, 3, …, n-1
b. If Rj+1< Rj, then interchange Rj with Rj+1 and set CHECK = 1
c. If CHECK = 0, then sorting is done.
4. Sorting completed, and stop.
5. Print sorted list.
Bubble sort is the simplest sorting algorithm, however, it is not suitable for large dataset as its
average and worst case complexity are of Ο(n2) where n is the number of items.
The procedure is further illustrated in Table 2 below
Table 9: bubble sort procedure
Pass Number
j Rj 1 2 3 4 5 6 Sorted
1 40 21 21 9 9 9 9
2 21 40 9 21 21 21 21
3 72 9 40 40 40 34 34
4 9 63 56 56 34 40 40
5 63 56 63 34 56 56 56
6 56 72 34 63 63 63 63
7 92 85 72 72 72 72 72
8 34 92 85 85 85 85 85
9 97 85 92 92 92 92 92
10 85 97 97 97 97 97 97
5.10 Searching algorithm
Searching is the process of examining a set of items to find out those that have desired property.
Searching algorithms helps to find required information from a collection of items stored as
element in the computer memory. These set of items are in different forms such as array, linked
list, graph or tree. Therefore, desired items are located using specific characteristics in a collection
of items. Searching in data structure can be done by applying searching algorithms to check for or
extract an element from any form of stored data structure. Search algorithms are classified based
on operation they perform. Typical classification include:
 Sequential search: the list of array of elements is traversed sequentially while checking
every component of the set. Typical example is the linear search algorithm.
 Interval search: these include algorithms that are explicitly designed for searching in sorted
data structure. In terms of efficiency, these algorithms are better than linear search
algorithms. Examples are logarithm search and binary search.
Search algorithms are evaluated based on the time taken to search for an element matching the
search item in the data collection and given by best possible time, average time and worst-case
time. There are many searching algorithms in data structure, some these algorithms include linear
search, binary search, interpolation search, sublist search, exponential search, Fibonacci search
etc.
In this subsection, linear search algorithm, a representative searching algorithm is discussed.
5.10.1 Linear search
Linear search algorithm iteratively searches all elements in array. it is a simple algorithm that
check every items in the list sequentially to locate the desired element. The algorithm of linear
search is given below:
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

The pseudocode is given below


procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for

end procedure

5.11. Algorithm Analysis

Algorithm analysis is process of providing theoretical estimates for the resources needed by any
algorithm which solves a given computational problem. This estimates provide insight into
reasonable directions of search for efficient algorithms. Algorithm analysis provide common
estimate for their complexity function for arbitrary input which is measured by Big O notation,
Big-omega notation, and Big-theta notation. Algorithm of analysis is an important part of
computational complexity theory. Usually, the efficiency or running time of an algorithm is stated
as a function relating the input length to the number of steps known as time complexity, or volume
of memory know as space complexity.

5.12 Need for Algorithm analysis


 Enable program developer to estimate the memory usage or time taken by algorithm to
perform specific task
 Helps to analysis the problem solving capacity of the algorithm in terms of the time and
size required (size of memory for storage while implementation)
 Predict the behaviour of an algorithm without implementing it on a specific computer
 It is impossible to predict the exact behaviour of an algorithm. There are too many
influencing factors.
 Enable comparison of algorithm to determine the best one for our purpose.
5.13 Types of Algorithm Analysis
 Worst-case: the maximum number of step taken on any instance of running the algorithm
 Best-case: the minimum number of step taken on any instance of running the algorithm
 Average-case: A average number of step taken on any instance of running the algorithm
 Amortized: A sequence of operations applied to the input size average over time.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy