Data structures with c programming 2nd edition anil kumar yadav

Page 1

Data Structures with C Programming 2nd Edition Anil Kumar Yadav

Visit to download the full and correct content document: https://textbookfull.com/product/data-structures-with-c-programming-2nd-edition-anil-k umar-yadav/

More products digital (pdf, epub, mobi) instant download maybe you interests ...

C++ Programming. Program Design including Data

Structures D.S. Malik

https://textbookfull.com/product/c-programming-program-designincluding-data-structures-d-s-malik/

Effective Robotics Programming with ROS Anil Mahtani

https://textbookfull.com/product/effective-robotics-programmingwith-ros-anil-mahtani/

Interpreting LISP: Programming and Data Structures 2nd Edition Gary D. Knott (Auth.)

https://textbookfull.com/product/interpreting-lisp-programmingand-data-structures-2nd-edition-gary-d-knott-auth/

Problem Solving in Data Structures Algorithms Using C Programming Interview Guide First Edition Hemant Jain

https://textbookfull.com/product/problem-solving-in-datastructures-algorithms-using-c-programming-interview-guide-firstedition-hemant-jain/

Problem Solving in Data Structures Algorithms Using C Programming Interview Guide 1st Edition Hemant Jain

https://textbookfull.com/product/problem-solving-in-datastructures-algorithms-using-c-programming-interview-guide-1stedition-hemant-jain/

Mathematical programming with data perturbations II 2nd Edition Fiacco.

https://textbookfull.com/product/mathematical-programming-withdata-perturbations-ii-2nd-edition-fiacco/

Data Structures Using C 2nd edition Reema Thareja

Oxford Publication 2014 Reema Thareja

https://textbookfull.com/product/data-structures-using-c-2ndedition-reema-thareja-oxford-publication-2014-reema-thareja/

Interpreting LISP. Programming and Data Structures Gary D. Knott

https://textbookfull.com/product/interpreting-lisp-programmingand-data-structures-gary-d-knott/

Data Structures and Algorithms with the C++ STL: A guide for modern C++ practitioners 1 / converted Edition John Farrier

https://textbookfull.com/product/data-structures-and-algorithmswith-the-c-stl-a-guide-for-modern-c-practitioners-1-convertededition-john-farrier/

DATA STRUCTURES WITH C PROGRAMMING

www.arclerpress.com

C
DATA STRUCTURES WITH
PROGRAMMING
Dr. Anil Kumar Yadav and Vinod Kumar Yadav
ARCLER P res s

Data Structures with C Programming

Arcler Press

2010 Winston Park Drive, 2nd Floor

Oakville, ON L6H 5R7 Canada

www.arclerpress.com

Tel: 001-289-291-7705

001-905-616-2116

Fax: 001-289-291-7601

Email: orders@arclereducation.com

e-book Edition 2019

ISBN: 978-1-77361-658-2 (e-book)

This book contains information obtained from highly regarded resources. Reprinted material sources are indicated and copyright remains with the original owners. Copyright for images and other graphics remains with the original owners as indicated. A Wide variety of references are listed. Reasonable efforts have been made to publish reliable data. Authors or Editors or Publishers are not responsible for the accuracy of the information in the published chapters or consequences of their use. The publisher assumes no responsibility for any damage or grievance to the persons or property arising out of the use of any materials, instructions, methods or thoughts in the book. The authors or editors and the publisher have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission has not been obtained. If any copyright holder has not been acknowledged, please write to us so we may rectify.

Notice: Registered trademark of products or corporate names are used only for explanation and identification without intent of infringement.

© 2019 Arcler Press

ISBN: 978-1-77361-337-6 (Hardcover)

Arcler Press publishes wide variety of books and eBooks. For more information about Arcler Press and its products, visit our website at www.arclerpress.com

ABOUT THE AUTHORS

Dr. Anil Kumar Yadav has done his B.Tech (CSE), M.Tech (I.T) and PhD (Computer Science and Engineering) in the area of Machine Learning specialization in Reinforcement Learning and Artificial Intelligence. He has filed patent on topic Machine Learning, “Method for Reinforcement Learning,”. He has been teaching data structures and Artificial intelligence over the last 13 years at under graduate and post graduate levels. With over a decade of teaching experience in the engineering institutions, he has exemplified his work at various designations. His research interests include data structures, deep learning , machine learning algorithms specially reinforcement learning, computational intelligence, computer vision and image processing, brain computer/machine interface, intelligent informatics,softcomputing in modeling and control, cognitive science. He has presented papers at national and international conferences and published several papersininternationaljournalsandalsoattendedseveralworkshops.He is alsoareviewerofsomeoftheinternationaljournalandconferences.

Vinod Kumar Yadav has done his B.Tech (I.T), M.Tech (CSE). His area of interest is Data mining, Artificial Intelligence and Machine Learning. He has been teaching data structures over the last 7 years at under graduate and post graduate levels. He is also GATE Qualified in Information Technology in 2007. He has published many papers at national, international conferences and international Journals. He also attended several national workshops.

List of Figures xi Preface........................................................................ ....xvii Chapter 1 Data Structures ......................................................................................... 1 1.1. Introduction 2 1.2. Data Structure Basic Terminology ....................................................... 2 1.3. Data Structure 3 1.4. Introduction To Algorithm ................................................................... 8 1.5. Basic Concept of Function ................................................................ 13 1.6. Basic Concept of Pointers 19 1.7. Introduction To Structure ................................................................... 22 1.8. Dynamic Memory Allocation In Data Structure 28 Chapter 2 Array ....................................................................................................... 35 2.1. Introduction 36 2.2. Application of Array .......................................................................... 36 2.3. Definition of Array ............................................................................ 37 2.4. Representation of Array 37 2.5. Ordered List...................................................................................... 56 2.6. Sparse Matrices 57 2.7. Garbage Collection........................................................................... 60 Chapter 3 Recursion ................................................................................................ 63 3.1. Introduction 64 3.2. Recursion ......................................................................................... 64 3.3. Tower of Hanoi 72 3.4. Backtracking .................................................................................... 74 TABLE OF CONTENTS
viii Chapter 4 Stack ....................................................................................................... 77 4.1. Introduction ...................................................................................... 78 4.2. Definition of Stack 78 4.3. Operations on Stack ......................................................................... 80 4.4. Disadvantages of Stack ..................................................................... 94 4.5. Applications of Stack 94 4.6. Expressions (Polish Notation) ............................................................ 95 4.7. Evaluation of Postfix Expression 98 4.8. Decimal To Binary Conversion .......................................................... 99 4.9. Reversing The String ........................................................................ 102 Chapter 5 Queue ................................................................................................... 105 5.1. Introduction .................................................................................... 106 5.2. Definition And Structure of Queue 106 5.3. Operations on Queue ..................................................................... 106 5.4. Circular Queue 114 5.5. D-Queue (Double Ended Queue) 119 5.6. Priority Queues............................................................................... 124 5.7. Application of Queue 128 Chapter 6 Linked List ............................................................................................. 129 6.1. Introduction 130 6.2. Definition And Structure of Linked List............................................ 130 6.3. Characteristics of Linked List ........................................................... 134 6.4. Types of Linked List 135 6.5 Polynomial Representation of Linked List ......................................... 158 Chapter 7 Tree ....................................................................................................... 161 7.1. Introduction .................................................................................... 162 7.2. Definition of Trees 162 7.3. Binary Tree 165 7.4. Binary Tree Representation.............................................................. 169 7.5. Binary Tree Traversal 172
Binary Search Tree (BST) ................................................................. 179 7.7. Height Balanced (AVL) Tree ............................................................ 196
7.6.
ix Chapter 8 Graph .................................................................................................... 213 8.1. Introduction 214 8.2. Definition of Graph ........................................................................ 214 8.3. Representation of Graphs ................................................................ 221 8.4. Graph Traversal 224 8.5. Spanning Tree ................................................................................. 229 8.6. Shortest Path Problem 240 8.7. Application Of Graph ..................................................................... 243 Chapter 9 Sorting................................................................................................... 245 9.1. Introduction 246 9.2. Types of Sorting .............................................................................. 246 9.3. Basic Terms of Sorting 246 9.4. Sorting Techniques .......................................................................... 247 Chapter 10 Searching and Hashing .......................................................................... 291 10.1. Introduction .................................................................................. 292 10.2. Searching ..................................................................................... 292 10.3. Hashing 296 10.4. Collision ....................................................................................... 300 10.5. Collision Handling Method 301 10.6. Rehashing ..................................................................................... 306 10.7. Application of Hashing ................................................................. 307 Index ..................................................................................................... 309

LIST OF FIGURES

Figure 1.1 Part of abstract data type.

Figure 1.2 Classification of data structures.

Figure 1.3 One-dimensional array.

Figure 1.4 Stack data structure

Figure 1.5 Queue structure.

Figure 1.6 Representation of link list.

Figure 1.7 Tree data structure.

Figure 1.8 A graph.

Figure 1.9 Big O notation.

Figure 1.10 Ω Notation.

Figure 1.11 ‘Ө’ Notation

Figure 1.12 Output a user-defined function.

Figure 1.13 Classification of function argument.

Figure 1.14 Output Call by Value to Pass Arguments.

Figure 1.15 Output of Call by Reference to Pass Arguments.

Figure 1.16 Output of Accessing variable through Pointers.

Figure 1.17 Output of Null Pointers.

Figure 1.18 Output of Structure Initialization.

Figure 1.19 Output structure pointer

Figure 1.20 Output of an Array of Structure.

Figure 1.21 Output of malloc and free function.

Figure 1.22 Output of calloc and free function.

Figure 1.23 Output of realloc Function.

Figure 2.1 One-dimensional Array.

Figure 2.2 Array initialization and accessing.

Figure 2.3 Output of Program Greater Number.

xi

Figure 2.4 Output of Program 2.3.

Figure 2.5 Array positions.

Figure 2.6 3 x 4 array two-dimensional.

Figure 2.7 Row major store in an array.

Figure 2.8 Column major store in an array.

Figure 2.9 Output of Program 2.4.

Figure 2.10 Output of Program 2.5

Figure 2.11 Output of Searching.

Figure 2.12 Output of Program Insertion.

Figure 2.13 Output of Program Deletion.

Figure 2.14 Output of Program Deletion.

Figure 2.15 Polynomial representation.

Figure 2.16 Sparse matrix.

Figure 2.17 Output of Sparse Matrix or not.

Figure 3.1 Output of a Factorial number Using Iteration Method.

Figure 3.2 Output of a Factorial number Using Recursion Method.

Figure 3.3 Output of Fibonacci Series.

Figure 3.4 Output for Check Prime number.

Figure 3.5 Initial Setup of Tower of Hanoi

Figure 3.6 After applying first Step position of Tower

Figure 3.7 After applying second step position of tower

Figure 3.8 After applying third step position of tower.

Figure 3.9 After applying fourth step position of tower.

Figure 3.10 Tree Example.

Figure 4.1 Stack Containing Items.

Figure 4.2 Stack using a 1-dimensional array.

Figure 4.3 Stack empty condition

Figure 4.4 Stack full condition.

Figure 4.5 Performing Push Operation.

Figure 4.6 Performing Pop Operation.

Figure 4.7 Stack Operation.

xii

Figure 4.8 Output Example 4.1.

Figure 4.9 Output of stack operation using linked list.

Figure 4.10 Output of Decimal to Binary

Figure 4.11 Output Reverse a String.

Figure 5.1 Queue structure.

Figure 5.2 Array Implementation of Queue.

Figure 5.3 (a) Queue in Memory.

Figure 5.3 (b) Queue after deleting the first element.

Figure 5.3 (c) Queue after inserting an element.

Figure 5.4 Output of Queue Operations.

Figure 5.5 Output of Queue operation using Linked List.

Figure 5.6 Linear Queue.

Figure 5.7 Circular Queue.

Figure 5.8 Output of Circular Queue Operations

Figure 5.9 D-Queue.

Figure 5.10 (a) Input-restricted D-queue.

Figure 5.10 (b) Output-restricted D-queue.

Figure 5.11 Output of D-Queue Operations.

Figure 5.12 Output of Priority Queue.

Figure 6.1 Structure of a Node

Figure 6.2 Representation of link list.

Figure 6.3 Output of Linked List.

Figure 6.4 A singly-linked list containing three integer values.

Figure 6.5 Output of Single linked list operations.

Figure 6.6 Output of Circular Linked List Operations.

Figure 6.7 Doubly Linked List.

Figure 6.8 Output of Doubly linked list Operations.

Figure 6.9 Structure of a polynomial node.

Figure 6.10 List structure of polynomial.

Figure 6.11 Linked representation of a polynomial two variable.

Figure 7.1 Tree.

Figure 7.2 a, b and c represent a forest.

xiii

Figure 7.3 An example of Tree.

Figure 7.4 Binary tree.

Figure 7.5 Strictly Binary Tree

Figure 7.6 Almost Complete.

Figure 7.7 Complete Binary Tree.

Figure 7.8 Extended Binary Tree.

Figure 7.9 (a) Left -skewed binary tree (b) Right – skewed binary tree.

Figure 7.10 General Tree.

Figure 7.11 Expression Tree.

Figure 7.12 Sequential representation of a binary tree.

Figure 7.13 Structure of a node binary tree.

Figure 7.14 Linked List Representation.

Figure 7.15 Binary Tree (a) and (b).

Figure 7.16 Binary Tree (a) and (b).

Figure 7.17 Binary Tree (a) and (b).

Figure 7.18 Finally Binary Tree.

Figure 7.19 Computing pointer in a binary tree.

Figure 7.20 A threaded binary tree.

Figure 7.21 Binary Search Tree.

Figure 7.22 Binary Search Tree.

Figure 7.23 Before deletion.

Figure 7.24 After deletion.

Figure 7.25 Before deletion.

Figure 7.26 After deletion of a node from the tree.

Figure 7.27 Before deletion.

Figure 7.28 After the deletion of a node from the tree.

Figure 7.29 Red-Black Tree.

Figure 7.30 AVL tree.

Figure 7.31 Not an AVL tree.

Figure 7.32 Classification of Rotation.

Figure 7.33 Balanced AVL search tree.

Figure 7.34 AVL search tree after performing LL rotation.

xiv

Figure 7.35 Balanced AVL search tree.

Figure 7.36 AVL search tree after performing RR rotation

Figure 7.37 Balanced AVL search tree.

Figure 7.38 AVL search tree after performing LR rotation.

Figure 7.39 Balanced AVL search tree.

Figure 7.40 AVL search tree after performing RL rotation.

Figure 7.41 AVL tree Exercise of 7.4

Figure 7.42 Weight Balance Tree.

Figure 8.1 Simple Graph.

Figure 8.2 A Directed Graph.

Figure 8.3 An Undirected Graph.

Figure 8.4 A Complete Graph.

Figure 8.5 A Sub Graph.

Figure 8.6 A connected graph.

Figure 8.7 A Multigraph.

Figure 8.8 A Graph with four Vertices.

Figure 8.9 A directed graph with four Vertices.

Figure 8.10 A Null Graph.

Figure 8.11 An Isomorphic Graph.

Figure 8.12 A Homeomorphic Graph.

Figure 8.13 An Undirected Graph.

Figure 8.14 A Directed Graph.

Figure 8.15 Linked list representation of Figure 8.14.

Figure 8.16 An Undirected Graph.

Figure 8.17 Linked list of Figure 8.16.

Figure 8.18 Graph with six vertices.

Figure 8.19 An undirected graph.

Figure 8.20 An undirected graph.

Figure 8.21 An undirected graph.

Figure 8.22(a) Undirected Graph.

Figure 8.22(b) Spanning Tree.

Figure 8.23 Undirected Graph.

xv

Figure 8.24 Minimum spanning tree Graph of 8.23.

Figure 8.25 Undirected Graph G.

Figure 8.26 A Minimum Spanning tree of Figure 8.25.

Figure 8.27 Undirected Graph G.

Figure 8.28 Undirected Graph G.

Figure 8.29 A Minimum Spanning tree of Figure 8.27

Figure 8.30 Unweighted graph

Figure 8.31 A Graph.

Figure 9.1 Output of Bubble Sort.

Figure 9.2 Output of Insertion Sort.

Figure 9.3 Output of Selection Sort.

Figure 9.4 Output of Merge Sort element.

Figure 9.5 Output of merge sort for two unsorted list.

Figure 9.6 Quick Sort.

Figure 9.7 Output of Quick Sort element.

Figure 9.8 Output of Heap Sort.

Figure 9.9 Output of Radix sort elements.

Figure 10.1 Output for Binary search.

Figure 10.2 A small phone directory book as a hash table.

Figure 10.3 Chaining.

Figure 10.4 Rehashing.

xvi

PREFACE

The significant role of Data Structures is famous for Computer Science and Engineering. This book is about the structure, actions and the principle of a different data type that help improve the ability to write an efficient algorithm, program and Analysis algorithm and program complexity. In this book, we present the fundamental concepts, operations, and algorithms for different types of data structures. This makes an understanding of this subject more lucid and makes it more interesting. This book, intended for the first course in the data structure at the junior or senior undergraduate level. This book prepared according to the syllabus of various Universities and conceived as a textbook for the course of Bachelor of Engineering or Technology of different branches of computer science. The contents of the book have been thoroughly organized and spread over ten chapters. Each chapter begins with the basics and describes with syntax, diagrams, and examples. Each concept of data structure has implemented in the C programming language. The book not only covers the scope of the subject but also explains the philosophy of the subject.

We trust the textbook would meet the requirements of both the teachers and the students. We would very much appreciate receiving any suggestions for improvement in the book from teachers, students, and other readers. We wish to express our deep thanks to all those who helped in making this book a reality.

We express our gratitude to our publisher and the team of publications that have taken great pains in publishing the book with speed and accuracy.

1 CHAPTER CONTENTS 1.1. Introduction 2 1.2. Data Structure Basic Terminology ....................................................... 2 1.3. Data Structure 3 1.4. Introduction To Algorithm ................................................................... 8 1.5. Basic Concept of Function ................................................................ 13 1.6. Basic Concept of Pointers 19 1.7. Introduction To Structure ................................................................... 22 1.8. Dynamic Memory Allocation In Data Structure 28
DATA STRUCTURES

1.1. INTRODUCTION

In the Computer Programming or Software development, Data Structures is one of the most valuable roles for computer engineers. Use of appropriate data structures enables a computer system to perform its task more efficiently, by influencing the ability of computers to store and retrieve data from any location in its memory. A data structure is a method of how storing data on a computer so that it can be used efficiently. In Computer Programming we are using different types of data to perform, store, access and sent data and information. Computers cannot do without data, so organizing that data is very important. If that data is not organized effectively, it is very difficult to perform any task on that data. If it is organized effectively then any operation can be performed easily on that data.

1.2. DATA STRUCTURE BASIC TERMINOLOGY

1.2.1.

Data and Data Types

Data are a group of specific facts, numbers, character, and figures. A data item refers to a single unit of values. Data is an unprocessed form of information. Data is plural and datum is singular form. Data items that are divided into subitems are called group items; those are not called elementary items. For examples, an employee’s name may divide into three subitems: first name, middle and last name. Data is numerical, character, symbols or any other kind of information. A Data Types consists of a set of values and a set of operations. A data type is a name which refers to kinds of data that variables may hold in the programming language. The data is stored in the memory at some location. By using the name of the variable, one can access the data from that memory location easily. For example in ‘C’ language, the data types are int (integer value), float (decimal value), char (character), double (real value of large range) etc. Data types divided into categories: Built-in data types (Primitive Data) and User define data types (Non-primitive Data). Generally, a programming language supports a set of built-in data types and allow the user to define a new type those are called user-defined data types.

Information: Information is related to an attribute or a set of attributes of an object. Examples are the number of students in a class, length of the room, and the component of a computer. The fundamental component of information is the Data; “Information is a collection of data. When data

Data Structures with C Programming 2

are processed or organized it gives meaningful and logical knowledge it becomes information.”

Abstract Data Type: The abstract data type is a triple of D-Set of domains, F-Set of functions, A-Axioms in which only what is to be done is mentioned but how is to be done is not mentioned. At ADT, all the all the implementation details are hidden.

In short: ADT = Type + Function Names + Behaviors of Each function

An abstract data type can declare the data type and without explanation operations to how it will be implemented. Applications that use the data type are unaware of implementation; they only make use of the operations that defined abstractly. If we want to change implementations, we have to do is rewrite the operations. No matter how large our application is, the cost of changing implementations is the same. The abstract data type is written with the help of the instances and operations.

Implementation: The part that implements the abstract data type. These two pieces are completely independent. It should be possible to take the implementation developed for one application and use it for a completely different application with no changes.

1.3. DATA STRUCTURE

Definition: The data structure can be defined as the organization of data or elements and all possible operations, which are required for those set of data. It is a logical or mathematical representation and organization of any data is known as a data structure. Some of the data structures are arrays, stacks, queues, linked lists, trees, and graphs, etc.

Data Structures
3
Figure 1.1: Part of abstract data type

“The well-organized collection of data and all feasible operations on data is called a data structure.”

Data Structure = Organized data (mathematical or logical) + Acceptable operations

1.3.1. Basic Operation of Data Structures

The following operations play a major role in data processing on data structures:

• Traversing: Accessing each record exactly once so that certain items in the record may be processed. This process is also called visiting the record.

• Inserting: adding a new record to the structure.

• Deleting: Removing a record from the structure.

• Searching: Finding the location of record with a given key value, or finding the locations of all records which satisfy one or more conditions.

• Sorting: Arranging the records in some logical order.

• Merging: Combining the records in two different files into a single file.

1.3.2. Classification of Data Structures

Figure 1.2: Classification of data structures.

The data structures are generally divided into two categories.

1. Primitive Data type: These are basic data that are directly operated upon machine instructions. These have different

Data Structures with C Programming 4

representations on different computers. Such as int, float, char, double which are defined by programming language itself.

a) Integer Data Type: This type of data is used to store the integer value in a variable. The value should not contain any fraction data.

Example: int a;

a = 10; is acceptable but a = 10.05 is not acceptable.

b) Float Data Type: This type of data is used to store the real values in a variable. The value may have some fractional or decimal data.

Example: float a;

a = 10; is acceptable but it will be interpreted as a = 10.0 a = 10.05 is acceptable.

c) Character Data Type: This type of data is used to store some text or alphabets in a variable. The character is always written within single quotes.

Example: char answer; answer = “T”;

d) Double Data Type: This type of data is used to store the numeric value in the variable. It is same as of float data type but the capability of this data type is larger than the float data type size.

Example: double a; a = 10.0000000100

2. Non-Primitive Data type: These are more sophisticated data, which are derived from the primitive data. The user defines data emphasize the structuring of a group of homogeneous or heterogeneous data items.

Linear Data Structure: Linear data structures are the data structures in which data is arranged or accessed (read or write) in straight sequence or the consecutive way one by one in a list.

Examples: Arrays, Stacks, Queues, and List.

a) Array: it is a collection of all the elements with similar data types. The array is the collection of a finite number of homogenous data element such that the elements of the array are referenced respectively by an index

Data
5
Structures

set consisting of ‘n’ consecutive numbers and stored respectively in successive memory locations.

Example: int A[4]; The computer reserved four storage locations as shown below:

Figure 1.3: One-dimensional array.

b) Stack: it is a data structure which follows last in first out (LIFO) mechanism. It means the first element will insert is to remove the last one. Example: if a stack of elements 40, 60, 80, and 100. Here 20 will be the bottommost element and 100 will be the topmost element in a stack. A stack is shown in Figure 1.4.

Figure: 1.4: Stack data structure.

Figure 1.5: Queue structure.

c) Queue: It is a data structure, which follows, first in first out (FIFO) mechanism. It means the first element inserted is the first one to remove. Queue uses two variables rear and front.

d) Linked List: A linked list is a set of nodes where each node has two fields: an information or data and link or next address field. The ‘data’ field store actual piece of

Data Structures with C Programming 6

information, which may be an integer, a character, a string or even a large record and ‘link’ field is used to point to next node. Hence link list of integer 20, 40, 60, 80 is shown in Figure 1.6.

Figure 1.6: Representation of link list.

Non-Linear data structures: Non-Linear data structures are the data in which data may be arranged in unordered or hierarchical manner; for example, Trees, Graphs.

a) Tree: it is a non-linear data structure in which data are arranged in a sorted sequence. It is used to represent the hierarchical relationship of several data items.

Figure 1.7: Tree data structure.

b) Graph: A graph is a non-linear data structure which consists of set of nodes called vertices V and set of edges E which links vertices. From Figure 1.8, V (G) = {1, 2, 3, 4,5,6}, E(G) = {(1,2),(2,1),(2,3), (3,2), (1,4), (4,1), (4,5), (5,4), (5,6), (6,5), (3,6),(6,3)}

Figure 1.8: A graph.

Data Structures 7

1.4. INTRODUCTION TO ALGORITHM

An algorithm is composed of a finite set of steps, each of which may require one or more operations. An algorithm is a finite set of instructions that, if followed, accomplished a particular task. Example: The algorithm makes the sum of two numbers 20 and 30.

Step 1: Start

Step 2: Store 20 into X and 30 into Y.

Step 3: Calculate SUM = X + Y.

Step 4: Print the value of SUM.

Step 5: Stop.

Basic Characteristics of An algorithm:

• Input: an algorithm should have zero or more inputs are externally supplied.

• Output: At least one quantity is produced.

• Definiteness: Each instruction is clear and unambiguous.

• Finiteness: if we trace out the instructions of the algorithm, then for all cases, the algorithm terminates after a finite number of steps.

• Effectiveness: Every instruction must be very basic so that it can be carried out, in principle, by a person using only a pencil and paper.

A program is the expression of an algorithm in a programming language. Sometimes words such as procedure, function, and subroutine are used synonymously for the program.

Program = Data structure + Algorithms

1.4.1. Analysis of Programs

The analysis of the program does not mean simply working of the program, but to check whether for all possible situations program works or not. The analysis also involves working of the program efficiently. Efficiency in the sense,

1. The program requires less amount of storage space.

2. The programs get executed in very less amount of time.

Data Structures with C Programming 8

The time and space are factors which determine the efficiencies of the program. The time required for execution of the program cannot be computed in terms of seconds because of the following factors.

1. The hardware of the machine.

2. The amount of time required by each machine instruction.

3. The amount of time required by the compilers to execute the instruction.

4. The instruction set.

1.4.2. Complexity of an Algorithm

The analysis of algorithms is a major task in computer science. In order to compare algorithms, there must be some criteria for the major effects of algorithms. The algorithm can be evaluated by a variety of criteria. The rate of growth of the time or space required to solve larger and larger instance of a program. There are three conditions for complexity theory is as follows:

• Worst case: The worst-case time complexity is the function defined by the maximum amount of time needed by the algorithm for an input of size, ‘n.’ Therefore, it is a function which defined by the maximum number of steps taken for any instance that size ‘n.’

• Average case: The average case time complexity is the execution of an algorithm having typical input data of size ‘n.’ Therefore, it is a function which defined by the average number of steps taken for any instance that size ‘n.’

• Best case: The best case time complexity is the minimum amount of time that an algorithm requires for an input of size ‘n.’ Therefore, it is a function which defined by the minimum number of steps taken for any instance that size ‘n.’

Space Complexity: The space complexity of a program is the amount of memory it needs to run to completion. The space needed by a program is the sum of the following components-

• A fixed part that includes space for the code, space for simple variable and fixed size component variables.

• The variable part that consists of the space needed by the component variable where size is dependent on the particular problem.

Data Structures 9

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.