You are on page 1of 9

KUVEMPU UNIVERSITY

INFORMATION TECHNOLOGY PROGRAMMES


Bachelor of Science in Information Technology - B.Sc.(IT)
Master of Science in Information Technology - M.Sc. (IT)

B.Sc.(IT) - 4th Semester

BSIT - 41
BSIT - 42

Directorate of Distance Education

Kuvempu University

Shankaraghatta, Shimoga District, Karnataka

Algorithms
Java Programming

In
collaboration
with

Universal Education Trust


Bangalore

II

Titles in this Volume :


BSIT - 41 Algorithms
BSIT - 42 Java Programming

Prepared by UNIVERSAL EDUCATION TRUST (UET)


Bangalore

First Edition
Second Edition

: May 2005
: May 2012

Copyright by UNIVERSAL EDUCATION TRUST, Bangalore


All rights reserved

No Part of this Book may be reproduced


in any form or by any means without the written
permission from Universal Education Trust, Bangalore.

All Product names and company names mentioned


herein are the property of their respective owners.

NOT FOR SALE


For personal use of Kuvempu University
IT - Programme Students only.

Corrections & Suggestions


for Improvement of Study material
are invited by Universal Education Trust, Bangalore.
E-mail : info@uetb.org

Printed at :

Pragathi Print Communications


Bangalore - 20
Ph : 080-23340100

III

ALGORITHMS
(BSIT - 41)

: Contributing Authors :

Dr. D.S. Guru


Professor
Department of Studies in Computer Science
University of Mysore
Mysore

T.N. Vikram
Department of Studies in Computer Science
University of Mysore
Mysore
&

P.B. Mallikarjuna
Department of Studies in Computer Science
University of Mysore
Mysore

IV

Blank Page

V
a

Pr ef ac e

n this course material, we address the issues related to problem solving and algorithm design. This
material provides you an insight into the field of designing algorithms.

Algorithms are a specific way by which we formulate the problems before we code them in to a
program. i.e. we are converting the problem from the way human beings look at to the manner in which
a computer can look at it.
However, there is no unique way of designing the algorithms. Given a problem to be executed on a
computer, different people can convert it into algorithms in different ways. Then, what is it that the
algorithms course is about? Even though it may not be possible to exactly teach the art of algorithms, it
is possible to teach the science portion of the same. There are some basic concepts, which can hold
good irrespective of the problem domain and these can be used as off the shelf pieces of information
while devising the algorithm. In this course, you are being introduced to some of the basic algorithms we
follow.
Once the algorithm has been designed, there should also be a mechanism to decide on the goodness
of the algorithm. This becomes more important because often more than one method of solving the given
problem is available so, we should be able to compare one solution with another.
An algorithm not only depends on its own nature, but depends on the supporting data structures too.
Thus, in this material we also cover some of the preliminary aspects of related data structures. In some of
the parts of the material, we have assumed that the reader has a background of at least one conventional
programming language such as Pascal, C, C++ etc. and as well a little knowledge of data structures.
The material has been organized as a collection of 6 chapters. Chapter 1 presents in detail the necessity
of taking up a course on algorithms through exploration of significance of algorithms in the field of computer

VI

Preface

science. The essential knowledge on the prerequisite data structures has been, to some extent, covered in
Chapter 2. The chapter 3, deals with some simple problems and associated algorithms. The concept of
searching and sorting is been introduced in chapter 4 while the concept of recursion is presented in
chapter 5. In chapter 6 we have covered the concept of binary tree representation techniques and binary
tree traversal schemes.

VII
a

Co n t en t s

Chapter 1
REVIEW

1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7

1
1
2
3
4
5
5
5
7
7

Objectives........................................................................................
Concept of algorithm.........................................................................
Characteristics of Algorithm...............................................................
Problem - Solving Aspect...................................................................
How to devise the algorithms .........................................................
How to validate the algorithms...........................................................
How to Test the algorithms................................................................
Algorithmic Notations........................................................................
Summary..........................................................................................
Exercise...........................................................................................

Chapter 2
ELEMENTARY DATA STRUCTURES
2.0
2.1
2.2

2.3
2.4

Objectives........................................................................................
Fundamentals...................................................................................
Linear Data Structures.....................................................................
2.2.1
Array and its representation................................................
2.2.2
Stacks................................................................................
2.2.3
Queues..............................................................................
2.2.4
Circular Queue...................................................................
Linked Lists......................................................................................
Non-Linear data structures................................................................

8
8
8
9
9
11
13
17
19
21

Contents

VIII
2.4.1

Introduction to Graph theory................................................


2.4.1.1 Finite and infinite graphs.......................................
2.4.1.2 Incidence and degree............................................
2.4.1.3 Isolated vertex, pendent vertex and Null Graph.......
2.4.1.4 Walk, Path and Connected Graph...........................
2.4.2
Matrix representation of Graphs..........................................
2.4.2.1 Adjacency Matrix.................................................
2.4.2.2 Incidence Matrix..................................................
2.4.3
Trees.................................................................................
2.4.3.1 Some properties of trees.......................................
Summary..........................................................................................
Exercise...........................................................................................

21
22
22
23
23
24
24
25
26
26
27
27

Chapter 3
SOME SIMPLE ALGORITHMS

28

3.0
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
3.10

28
29
29
29
32
33
35
37
38
39
40
41
42

Objectives........................................................................................
Addition of two numbers....................................................................
Exchanging the values of two variables...............................................
Input three numbers and output them in ascending order......................
To find the Quadrant of a given Co-ordinate position...........................
To Find the Roots of a Quadratic Equation.........................................
Checking for Prime...........................................................................
Factorial of a Number.......................................................................
To Generate Fibonacci Series............................................................
Sum of N Numbers and Average.....................................................
To Add Two Matrices.......................................................................
Summary..........................................................................................
Exercise...........................................................................................

Chapter 4
SEARCHING AND SORTING
4.0 Objectives........................................................................................
4.1 Searching.........................................................................................
4.1.1
Sequential Search...............................................................
4.1.2
Binary Search....................................................................
4.2 Sorting.............................................................................................
4.2.1
Insertion Sorting.................................................................
4.2.2
Selection Sorting.................................................................
4.2.3
Bubble Sort.........................................................................
Summary..........................................................................................
Exercise...........................................................................................

43
43
43
43
44
46
46
48
50
52
52

Contents

IX
Chapter 5
RECURSION

54

5.0
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11

54
54
55
55
56
57
58
58
59
61
63
66
66
69
70
70

Objectives........................................................................................
What is Recursion?...........................................................................
Why do we need Recursion?.............................................................
When to use Recursion?...................................................................
Factorial of a Positive Number...........................................................
Finding the nth Fibonacci Number........................................................
Sum of First N Integers.....................................................................
Binary Search ..................................................................................
Maximum and Minimum in the given list of N Elements.......................
Merge Sort.......................................................................................
Quick Sort .......................................................................................
The Towers of Hanoi Problem...........................................................
5.11.1 The Recursive Algorithm.....................................................
5.12 Demerits of Recursion......................................................................
Summary..........................................................................................
Exercise...........................................................................................

Chapter 6
REPRESENTATION AND TRAVERSAL OF A BINARY TREE

72

6.0
6.1
6.2

Objectives........................................................................................
Binary Tree......................................................................................
Representation of a Binary Tree........................................................
6.2.1
Adjacency Matrix Represntation.........................................
6.2.2
Single Dimensional Array Representation.............................
6.2.3
Linked Representation of Binary Trees.................................
6.2.4
Binary Tree as a Data Structure...........................................
Traversal of a Binary Tree.................................................................
6.3.1
Traversal of a Binary Tree Represented in an Adjacency
Matrix ..............................................................................
6.3.2
Binary Tree Traversal of a Binary Tree From One
Dimensional Array Representation......................................
6.3.3
Binary Tree Traversal in Linked Representation...................
Operations on Binary Tree.................................................................
Summary..........................................................................................
Exercise...........................................................................................

72
72
74
74
75
76
79
80

References............................................................................. .

94

6.3

6.4

81
83
85
86
93
93

You might also like