Movatterモバイル変換


[0]ホーム

URL:


Ashoka Vanjare, profile picture
Uploaded byAshoka Vanjare
311 views

Learn data structures algorithms tutorial

This document provides an overview and introduction to data structures and algorithms. It discusses the need for data structures to efficiently organize and store data as applications and data grow increasingly large and complex. It also covers some basic terminology used in data structures and algorithms. The document then discusses setting up both an online and local environment for writing and executing code in C programming language to work through examples.

Embed presentation

Downloaded 14 times
Data Structures & AlgorithmsiAbouttheTutorialData Structures are the programmatic way of storing data so that data can be usedefficiently. Almost every enterprise application uses various types of data structures in oneor the other way.This tutorial will give you a great understanding on Data Structures needed to understandthe complexity of enterprise level applications and need of algorithms, and data structures.AudienceThis tutorial is designed for Computer Science graduates as well as Software Professionalswho are willing to learn data structures and algorithm programming in simple and easysteps.After completing this tutorial you will be at intermediate level of expertise from where youcan take yourself to higher level of expertise.PrerequisitesBefore proceeding with this tutorial, you should have a basic understanding of Cprogramming language, text editor, and execution of programs, etc.CopyrightandDisclaimer© Copyright 2016 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.com
Data Structures & AlgorithmsiiCompile&ExecuteOnlineFor most of the examples given in this tutorial you will find Try it option, so just make useof this option to execute your programs on the spot and enjoy your learning.Try the following example using the Try it option available at the top right corner of thefollowing sample code box −#include <stdio.h>int main(){/* My first program in C */printf("Hello, World! n");return 0;}
Data Structures & AlgorithmsiiiTableofContentsAbout the Tutorial ............................................................................................................................................iAudience...........................................................................................................................................................iPrerequisites.....................................................................................................................................................iCopyright and Disclaimer .................................................................................................................................iCompile & Execute Online............................................................................................................................... iiTable of Contents ........................................................................................................................................... iiiBASICS.........................................................................................................................................11. Overview ..................................................................................................................................................2Characteristics of a Data Structure..................................................................................................................2Need for Data Structure ..................................................................................................................................2Execution Time Cases ......................................................................................................................................3Basic Terminology ...........................................................................................................................................32. Environment Setup ...................................................................................................................................4Try it Option Online.........................................................................................................................................4Local Environment Setup.................................................................................................................................4Installation on UNIX/Linux...............................................................................................................................5Installation on Mac OS.....................................................................................................................................5Installation on Windows..................................................................................................................................6ALGORITHM................................................................................................................................73. Algorithms ─ Basics...................................................................................................................................8Characteristics of an Algorithm .......................................................................................................................8How to Write an Algorithm? ...........................................................................................................................9Algorithm Analysis.........................................................................................................................................10Algorithm Complexity....................................................................................................................................11Space Complexity ..........................................................................................................................................11Time Complexity............................................................................................................................................114. Asymptotic Analysis................................................................................................................................12Asymptotic Notations....................................................................................................................................12Common Asymptotic Notations ....................................................................................................................155. Greedy Algorithms..................................................................................................................................16Counting Coins...............................................................................................................................................166. Divide & Conquer....................................................................................................................................18Divide/Break..................................................................................................................................................18Conquer/Solve...............................................................................................................................................18Merge/Combine ............................................................................................................................................197. Dynamic Programming............................................................................................................................20
Data Structures & AlgorithmsivDATA STRUCTURES ...................................................................................................................218. Basic Concepts ........................................................................................................................................22Data Definition ..............................................................................................................................................22Data Object....................................................................................................................................................22Data Type.......................................................................................................................................................22Basic Operations............................................................................................................................................239. Arrays .....................................................................................................................................................24Array Representation ....................................................................................................................................24Basic Operations............................................................................................................................................25Insertion Operation .......................................................................................................................................25Array Insertions .............................................................................................................................................27Insertion at the Beginning of an Array ..........................................................................................................28Insertion at the Given Index of an Array .......................................................................................................30Insertion After the Given Index of an Array ..................................................................................................32Insertion Before the Given Index of an Array................................................................................................34Deletion Operation........................................................................................................................................36Search Operation...........................................................................................................................................37Update Operation..........................................................................................................................................39LINKED LIST...............................................................................................................................4110. Linked List ─ Basics..................................................................................................................................42Linked List Representation ............................................................................................................................42Types of Linked List .......................................................................................................................................42Basic Operations............................................................................................................................................43Insertion Operation .......................................................................................................................................43Deletion Operation........................................................................................................................................44Reverse Operation.........................................................................................................................................45Linked List Program in C ................................................................................................................................4611. Doubly Linked List...................................................................................................................................55Doubly Linked List Representation................................................................................................................55Basic Operations............................................................................................................................................55Insertion Operation .......................................................................................................................................56Deletion Operation........................................................................................................................................57Insertion at the End of an Operation.............................................................................................................57Doubly Linked List Program in C....................................................................................................................5812. Circular Linked List..................................................................................................................................67Singly Linked List as Circular..........................................................................................................................67Doubly Linked List as Circular........................................................................................................................67Basic Operations............................................................................................................................................67Insertion Operation .......................................................................................................................................68Deletion Operation........................................................................................................................................68Display List Operation....................................................................................................................................69Circular Linked List Program in C...................................................................................................................69
Data Structures & AlgorithmsvSTACK & QUEUE........................................................................................................................7413. Stack.......................................................................................................................................................75Stack Representation.....................................................................................................................................75Basic Operations............................................................................................................................................76peek().............................................................................................................................................................76isfull().............................................................................................................................................................77isempty()........................................................................................................................................................77Push Operation..............................................................................................................................................78Pop Operation ...............................................................................................................................................79Stack Program in C.........................................................................................................................................8114. Expression Parsing ..................................................................................................................................85Infix Notation.................................................................................................................................................85Prefix Notation ..............................................................................................................................................85Postfix Notation.............................................................................................................................................85Parsing Expressions .......................................................................................................................................86Postfix Evaluation Algorithm .........................................................................................................................87Expression Parsing Using Stack......................................................................................................................8715. Queue.....................................................................................................................................................93Queue Representation ..................................................................................................................................93Basic Operations............................................................................................................................................93peek().............................................................................................................................................................94isfull().............................................................................................................................................................94isempty()........................................................................................................................................................95Enqueue Operation .......................................................................................................................................96Dequeue Operation.......................................................................................................................................97Queue Program in C ......................................................................................................................................99SEARCHING TECHNIQUES........................................................................................................10316. Linear Search ........................................................................................................................................104Linear Search Program in C .........................................................................................................................10517. Binary Search........................................................................................................................................108How Binary Search Works? .........................................................................................................................108Binary Search Program in C .........................................................................................................................11118. Interpolation Search .............................................................................................................................115Positioning in Binary Search ........................................................................................................................115Position Probing in Interpolation Search.....................................................................................................116Interpolation Search Program in C ..............................................................................................................11819. Hash Table ............................................................................................................................................120Hashing........................................................................................................................................................120Linear Probing..............................................................................................................................................121Basic Operations..........................................................................................................................................122Data Item.....................................................................................................................................................122
Data Structures & AlgorithmsviHash Method...............................................................................................................................................122Search Operation.........................................................................................................................................122Insert Operation ..........................................................................................................................................123Delete Operation.........................................................................................................................................124Hash Table Program in C .............................................................................................................................125SORTING TECHNIQUES............................................................................................................13020. Sorting Algorithm..................................................................................................................................131In-place Sorting and Not-in-place Sorting ...................................................................................................131Stable and Not Stable Sorting......................................................................................................................131Adaptive and Non-Adaptive Sorting Algorithm...........................................................................................132Important Terms..........................................................................................................................................13221. Bubble Sort Algorithm ..........................................................................................................................134How Bubble Sort Works?.............................................................................................................................134Bubble Sort Program in C ............................................................................................................................13822. Insertion Sort........................................................................................................................................142How Insertion Sort Works? .........................................................................................................................142Insertion Sort Program in C .........................................................................................................................14523. Selection Sort........................................................................................................................................149How Selection Sort Works? .........................................................................................................................149Selection Sort Program in C.........................................................................................................................15224. Merge Sort Algorithm ...........................................................................................................................155How Merge Sort Works? .............................................................................................................................155Merge Sort Program in C.............................................................................................................................15825. Shell Sort ..............................................................................................................................................160How Shell Sort Works? ................................................................................................................................160Shell Sort Program in C................................................................................................................................16426. Quick Sort .............................................................................................................................................168Partition in Quick Sort .................................................................................................................................168Quick Sort Pivot Algorithm ..........................................................................................................................168Quick Sort Pivot Pseudocode ......................................................................................................................169Quick Sort Algorithm ...................................................................................................................................169Quick Sort Pseudocode................................................................................................................................170Quick Sort Program in C ..............................................................................................................................170GRAPH DATA STRUCTURE.......................................................................................................17427. Graphs ..................................................................................................................................................175Graph Data Structure ..................................................................................................................................175Basic Operations..........................................................................................................................................177
Data Structures & Algorithmsvii28. Depth First Traversal.............................................................................................................................178Depth First Traversal in C ............................................................................................................................18129. Breadth First Traversal..........................................................................................................................186Breadth First Traversal in C .........................................................................................................................188TREE DATA STRUCTURE ..........................................................................................................19430. Tree ......................................................................................................................................................195Important Terms..........................................................................................................................................195Binary Search Tree Representation.............................................................................................................196Tree Node....................................................................................................................................................196BST Basic Operations...................................................................................................................................197Insert Operation ..........................................................................................................................................197Search Operation.........................................................................................................................................199Tree Traversal in C.......................................................................................................................................20031. Tree Traversal .......................................................................................................................................206In-order Traversal........................................................................................................................................206Pre-order Traversal......................................................................................................................................207Post-order Traversal....................................................................................................................................208Tree Traversal in C.......................................................................................................................................20932. Binary Search Tree................................................................................................................................215Representation............................................................................................................................................215Basic Operations..........................................................................................................................................216Node............................................................................................................................................................216Search Operation.........................................................................................................................................216Insert Operation ..........................................................................................................................................21733. AVL Trees..............................................................................................................................................219AVL Rotations ..............................................................................................................................................22034. Spanning Tree.......................................................................................................................................224General Properties of Spanning Tree ..........................................................................................................224Mathematical Properties of Spanning Tree.................................................................................................225Application of Spanning Tree ......................................................................................................................225Minimum Spanning Tree (MST)...................................................................................................................225Minimum Spanning-Tree Algorithm............................................................................................................225Kruskal's Spanning Tree Algorithm..............................................................................................................226Prim's Spanning Tree Algorithm..................................................................................................................22935. Heaps....................................................................................................................................................233Max Heap Construction Algorithm..............................................................................................................234Max Heap Deletion Algorithm.....................................................................................................................235RECURSION.............................................................................................................................236
Data Structures & Algorithmsviii36. Recursion ─ Basics.................................................................................................................................237Properties ....................................................................................................................................................237Implementation...........................................................................................................................................238Analysis of Recursion...................................................................................................................................238Time Complexity..........................................................................................................................................238Space Complexity ........................................................................................................................................23937. Tower of Hanoi .....................................................................................................................................240Rules ............................................................................................................................................................240Algorithm.....................................................................................................................................................244Tower of Hanoi in C .....................................................................................................................................24738. Fibonacci Series ....................................................................................................................................251Fibonacci Iterative Algorithm ......................................................................................................................252Fibonacci Interactive Program in C..............................................................................................................252Fibonacci Recursive Algorithm ....................................................................................................................254Fibonacci Recursive Program in C................................................................................................................254
Data Structures & Algorithms1Basics
Data Structures & Algorithms2Data Structure is a systematic way to organize data in order to use it efficiently. Followingterms are the foundation terms of a data structure. Interface − Each data structure has an interface. Interface represents the set ofoperations that a data structure supports. An interface only provides the list ofsupported operations, type of parameters they can accept and return type of theseoperations. Implementation − Implementation provides the internal representation of adata structure. Implementation also provides the definition of the algorithms usedin the operations of the data structure.CharacteristicsofaDataStructure Correctness − Data structure implementation should implement its interfacecorrectly. Time Complexity − Running time or the execution time of operations of datastructure must be as small as possible. Space Complexity − Memory usage of a data structure operation should be aslittle as possible.NeedforDataStructureAs applications are getting complex and data rich, there are three common problems thatapplications face now-a-days. Data Search − Consider an inventory of 1 million(106) items of a store. If theapplication is to search an item, it has to search an item in 1 million(106) itemsevery time slowing down the search. As data grows, search will become slower. Processor Speed − Processor speed although being very high, falls limited if thedata grows to billion records. Multiple Requests − As thousands of users can search data simultaneously on aweb server, even the fast server fails while searching the data.To solve the above-mentioned problems, data structures come to rescue. Data can beorganized in a data structure in such a way that all items may not be required to besearched, and the required data can be searched almost instantly.1. Overview
Data Structures & Algorithms3ExecutionTimeCasesThere are three cases which are usually used to compare various data structure's executiontime in a relative manner. Worst Case − This is the scenario where a particular data structure operationtakes maximum time it can take. If an operation's worst case time is ƒ(n) thenthis operation will not take more than ƒ(n) time, where ƒ(n) represents functionof n. Average Case − This is the scenario depicting the average execution time of anoperation of a data structure. If an operation takes ƒ(n) time in execution, thenm operations will take mƒ(n) time. Best Case − This is the scenario depicting the least possible execution time of anoperation of a data structure. If an operation takes ƒ(n) time in execution, thenthe actual operation may take time as the random number which would bemaximum as ƒ(n).BasicTerminology Data − Data are values or set of values. Data Item − Data item refers to single unit of values. Group Items − Data items that are divided into sub items are called as GroupItems. Elementary Items − Data items that cannot be divided are called as ElementaryItems. Attribute and Entity − An entity is that which contains certain attributes orproperties, which may be assigned values. Entity Set − Entities of similar attributes form an entity set. Field − Field is a single elementary unit of information representing an attributeof an entity. Record − Record is a collection of field values of a given entity. File − File is a collection of records of the entities in a given entity set.
Data Structures & Algorithms4TryitOptionOnlineYou really do not need to set up your own environment to start learning C programminglanguage. Reason is very simple, we already have set up C Programming environmentonline, so that you can compile and execute all the available examples online at the sametime when you are doing your theory work. This gives you confidence in what you arereading and to check the result with different options. Feel free to modify any exampleand execute it online.Try the following example using the Try it option available at the top right corner of thesample code box −#include <stdio.h>int main(){/* My first program in C */printf("Hello, World! n");return 0;}For most of the examples given in this tutorial, you will find Try it option, so just makeuse of it and enjoy your learning.LocalEnvironmentSetupIf you are still willing to set up your environment for C programming language, you needthe following two tools available on your computer, (a) Text Editor and (b) The C Compiler.Text EditorThis will be used to type your program. Examples of few editors include Windows Notepad,OS Edit command, Brief, Epsilon, EMACS, and vim or vi.The name and the version of the text editor can vary on different operating systems. Forexample, Notepad will be used on Windows, and vim or vi can be used on Windows as wellas Linux or UNIX.The files you create with your editor are called source files and contain program sourcecode. The source files for C programs are typically named with the extension ".c".Before starting your programming, make sure you have one text editor in place and youhave enough experience to write a computer program, save it in a file, compile it, andfinally execute it.2. Environment Setup
Data Structures & Algorithms5The C CompilerThe source code written in the source file is the human readable source for your program.It needs to be "compiled", to turn into machine language so that your CPU can actuallyexecute the program as per the given instructions.This C programming language compiler will be used to compile your source code into afinal executable program. We assume you have the basic knowledge about a programminglanguage compiler.Most frequently used and free available compiler is GNU C/C++ compiler. Otherwise, youcan have compilers either from HP or Solaris if you have respective Operating Systems(OS).The following section guides you on how to install GNU C/C++ compiler on various OS.We are mentioning C/C++ together because GNU GCC compiler works for both C and C++programming languages.InstallationonUNIX/LinuxIf you are using Linux or UNIX, then check whether GCC is installed on your system byentering the following command from the command line −$ gcc -vIf you have GNU compiler installed on your machine, then it should print a message suchas the following −Using built-in specs.Target: i386-redhat-linuxConfigured with: ../configure --prefix=/usr .......Thread model: posixgcc version 4.1.2 20080704 (Red Hat 4.1.2-46)If GCC is not installed, then you will have to install it yourself using the detailed instructionsavailable at http://gcc.gnu.org/install/This tutorial has been written based on Linux and all the given examples have beencompiled on Cent OS flavor of Linux system.InstallationonMacOSIf you use Mac OS X, the easiest way to obtain GCC is to download the Xcode developmentenvironment from Apple's website and follow the simple installation instructions. Once youhave Xcode setup, you will be able to use GNU compiler for C/C++.Xcode is currently available at developer.apple.com/technologies/tools/
Data Structures & Algorithms6InstallationonWindowsTo install GCC on Windows, you need to install MinGW. To install MinGW, go to the MinGWhomepage, www.mingw.org, and follow the link to the MinGW download page. Downloadthe latest version of the MinGW installation program, which should be named MinGW-<version>.exe.While installing MinWG, at a minimum, you must install gcc-core, gcc-g++, binutils, andthe MinGW runtime, but you may wish to install more.Add the bin subdirectory of your MinGW installation to your PATH environment variable,so that you can specify these tools on the command line by their simple names.When the installation is complete, you will be able to run gcc, g++, ar, ranlib, dlltool, andseveral other GNU tools from the Windows command line.
Data Structures & Algorithms7Algorithm
Data Structures & Algorithms8Algorithm is a step-by-step procedure, which defines a set of instructions to be executedin a certain order to get the desired output. Algorithms are generally created independentof underlying languages, i.e. an algorithm can be implemented in more than oneprogramming language.From the data structure point of view, following are some important categories ofalgorithms − Search − Algorithm to search an item in a data structure. Sort − Algorithm to sort items in a certain order. 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 a data structure.CharacteristicsofanAlgorithmNot all procedures can be called an algorithm. An algorithm should have the followingcharacteristics − Unambiguous − Algorithm should be clear and unambiguous. Each of its steps(or phases), and their inputs/outputs should be clear and must lead to only onemeaning. Input − An algorithm should have 0 or more well-defined inputs. Output − An algorithm should have 1 or more well-defined outputs, and shouldmatch the desired output. Finiteness − Algorithms must terminate after a finite number of steps. Feasibility − Should be feasible with the available resources. Independent − An algorithm should have step-by-step directions, which shouldbe independent of any programming code.3. Algorithms ─ Basics
Data Structures & Algorithms9HowtoWriteanAlgorithm?There are no well-defined standards for writing algorithms. Rather, it is problem andresource dependent. Algorithms are never written to support a particular programmingcode.As we know that all programming languages share basic code constructs like loops(do, for, while), flow-control (if-else), etc. These common constructs can be used to writean algorithm.We write algorithms in a step-by-step manner, but it is not always the case. Algorithmwriting is a process and is executed after the problem domain is well-defined. That is, weshould know the problem domain, for which we are designing a solution.ExampleLet's try to learn algorithm-writing by using an example.Problem − Design an algorithm to add two numbers and display the result.step 1 − STARTstep 2 − declare three integers a, b & cstep 3 − define values of a & bstep 4 − add values of a & bstep 5 − store output of step 4 to cstep 6 − print cstep 7 − STOPAlgorithms tell the programmers how to code the program. Alternatively, the algorithmcan be written as −step 1 − START ADDstep 2 − get values of a & bstep 3 − c ← a + bstep 4 − display cstep 5 − STOPIn design and analysis of algorithms, usually the second method is used to describe analgorithm. It makes it easy for the analyst to analyze the algorithm ignoring all unwanteddefinitions. He can observe what operations are being used and how the process is flowing.Writing step numbers, is optional.We design an algorithm to get a solution of a given problem. A problem can be solved inmore than one ways.
Data Structures & Algorithms10Hence, many solution algorithms can be derived for a given problem. The next step is toanalyze those proposed solution algorithms and implement the best suitable solution.AlgorithmAnalysisEfficiency of an algorithm can be analyzed at two different stages, before implementationand after implementation. They are the following − A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of analgorithm is measured by assuming that all other factors, for example, processorspeed, are constant and have no effect on the implementation. A Posterior Analysis − This is an empirical analysis of an algorithm. The selectedalgorithm is implemented using programming language. This is then executed ontarget computer machine. In this analysis, actual statistics like running time andspace required, are collected.We shall learn about a priori algorithm analysis. Algorithm analysis deals with theexecution or running time of various operations involved. The running time of an operationcan be defined as the number of computer instructions executed per operation.
Data Structures & Algorithms11AlgorithmComplexitySuppose X is an algorithm and n is the size of input data, the time and space used by thealgorithm X are the two main factors, which decide the efficiency of X. Time Factor – Time is measured by counting the number of key operations suchas comparisons in the sorting algorithm. Space Factor − Space is measured by counting the maximum memory spacerequired by the algorithm.The complexity of an algorithm f(n) gives the running time and/or the storage spacerequired by the algorithm in terms of n as the size of input data.SpaceComplexitySpace complexity of an algorithm represents the amount of memory space required bythe algorithm in its life cycle. The space required by an algorithm is equal to the sum ofthe following two components − A fixed part that is a space required to store certain data and variables, that areindependent of the size of the problem. For example, simple variables andconstants used, program size, etc. A variable part is a space required by variables, whose size depends on the sizeof the problem. For example, dynamic memory allocation, recursion stack space,etc.Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed partand S(I) is the variable part of the algorithm, which depends on instance characteristic I.Following is a simple example that tries to explain the concept −Algorithm: SUM(A, B)Step 1 - STARTStep 2 - C ← A + B + 10Step 3 - StopHere we have three variables A, B, and C and one constant. Hence S(P) = 1+3. Now,space depends on data types of given variables and constant types and it will be multipliedaccordingly.TimeComplexityTime complexity of an algorithm represents the amount of time required by the algorithmto run to completion. Time requirements can be defined as a numerical function T(n),where T(n) can be measured as the number of steps, provided each step consumesconstant time.For example, addition of two n-bit integers takes n steps. Consequently, the totalcomputational time is T(n) = c*n, where c is the time taken for the addition of two bits.Here, we observe that T(n) grows linearly as the input size increases.
Data Structures & Algorithms12Asymptotic analysis of an algorithm refers to defining the mathematicalboundation/framing of its run-time performance. Using asymptotic analysis, we can verywell conclude the best case, average case, and worst case scenario of an algorithm.Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concludedto work in a constant time. Other than the "input" all other factors are considered constant.Asymptotic analysis refers to computing the running time of any operation in mathematicalunits of computation. For example, the running time of one operation is computed as f(n)and may be for another operation it is computed as g(n2). This means the first operationrunning time will increase linearly with the increase in n and the running time of the secondoperation will increase exponentially when n increases. Similarly, the running time of bothoperations will be nearly the same if n is significantly small.Usually, the time required by an algorithm falls under three types − Best Case − Minimum time required for program execution. Average Case − Average time required for program execution. Worst Case − Maximum time required for program execution.AsymptoticNotationsFollowing are the commonly used asymptotic notations to calculate the running timecomplexity of an algorithm. Ο Notation Ω Notation θ NotationBig Oh Notation, ΟThe notation Ο(n) is the formal way to express the upper bound of an algorithm's runningtime. It measures the worst case time complexity or the longest amount of time analgorithm can possibly take to complete.4. Asymptotic Analysis
Data Structures & Algorithms13For example, for a function f(n)Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n> n0. }Omega Notation, ΩThe notation Ω(n) is the formal way to express the lower bound of an algorithm's runningtime. It measures the best case time complexity or the best amount of time an algorithmcan possibly take to complete.
Data Structures & Algorithms14For example, for a function f(n)Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n> n0. }Theta Notation, θThe notation θ(n) is the formal way to express both the lower bound and the upper boundof an algorithm's running time. It is represented as follows −θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n >n0. }
Data Structures & Algorithms15CommonAsymptoticNotationsFollowing is a list of some common asymptotic notations:constant − Ο(1)logarithmic − Ο(log n)linear − Ο(n)n log n − Ο(n log n)quadratic − Ο(n2)cubic − Ο(n3)polynomial − nΟ(1)exponential − 2Ο(n)
Data Structures & Algorithms16An algorithm is designed to achieve optimum solution for a given problem. In greedyalgorithm approach, decisions are made from the given solution domain. As being greedy,the closest solution that seems to provide an optimum solution is chosen.Greedy algorithms try to find a localized optimum solution, which may eventually lead toglobally optimized solutions. However, generally greedy algorithms do not provide globallyoptimized solutions.CountingCoinsThis problem is to count to a desired value by choosing the least possible coins and thegreedy approach forces the algorithm to pick the largest possible coin. If we are providedcoins of € 1, 2, 5 and 10 and we are asked to count € 18 then the greedy procedure willbe − 1 − Select one € 10 coin, the remaining count is 8 2 − Then select one € 5 coin, the remaining count is 3 3 − Then select one € 2 coin, the remaining count is 1 3 − And finally, the selection of one € 1 coins solves the problemThough, it seems to be working fine, for this count we need to pick only 4 coins. But if weslightly change the problem then the same approach may not be able to produce the sameoptimum result.For the currency system, where we have coins of 1, 7, 10 value, counting coins for value18 will be absolutely optimum but for count like 15, it may use more coins than necessary.For example, the greedy approach will use 10 + 1 + 1 + 1 + 1 + 1, total 6 coins. Whereasthe same problem could be solved by using only 3 coins (7 + 7 + 1)Hence, we may conclude that the greedy approach picks an immediate optimized solutionand may fail where global optimization is a major concern.5. Greedy Algorithms
Data Structures & Algorithms17ExamplesMost networking algorithms use the greedy approach. Here is a list of few of them − Travelling Salesman Problem Prim's Minimal Spanning Tree Algorithm Kruskal's Minimal Spanning Tree Algorithm Dijkstra's Minimal Spanning Tree Algorithm Graph - Map Coloring Graph - Vertex Cover Knapsack Problem Job Scheduling ProblemThere are lots of similar problems that uses the greedy approach to find an optimumsolution.
Data Structures & Algorithms18In divide and conquer approach, the problem in hand, is divided into smaller sub-problemsand then each problem is solved independently. When we keep on dividing the sub-problems into even smaller sub-problems, we may eventually reach a stage where nomore division is possible. Those "atomic" smallest possible sub-problem (fractions) aresolved. The solution of all sub-problems is finally merged in order to obtain the solution ofan original problem.Broadly, we can understand divide-and-conquer approach in a three-step process.Divide/BreakThis step involves breaking the problem into smaller sub-problems. Sub-problems shouldrepresent a part of the original problem. This step generally takes a recursive approach todivide the problem until no sub-problem is further divisible. At this stage, sub-problemsbecome atomic in nature but still represent some part of the actual problem.Conquer/SolveThis step receives a lot of smaller sub-problems to be solved. Generally, at this level, theproblems are considered 'solved' on their own.6. Divide &Conquer
Data Structures & Algorithms19Merge/CombineWhen the smaller sub-problems are solved, this stage recursively combines them untilthey formulate a solution of the original problem. This algorithmic approach worksrecursively and conquer & merge steps works so close that they appear as one.ExamplesThe following computer algorithms are based on divide-and-conquer programmingapproach − Merge Sort Quick Sort Binary Search Strassen's Matrix Multiplication Closest Pair (points)There are various ways available to solve any computer problem, but the mentioned area good example of divide and conquer approach.
Data Structures & Algorithms20Dynamic programming approach is similar to divide and conquer in breaking down theproblem into smaller and yet smaller possible sub-problems. But unlike, divide andconquer, these sub-problems are not solved independently. Rather, results of thesesmaller sub-problems are remembered and used for similar or overlapping sub-problems.Dynamic programming is used where we have problems, which can be divided into similarsub-problems, so that their results can be re-used. Mostly, these algorithms are used foroptimization. Before solving the in-hand sub-problem, dynamic algorithm will try toexamine the results of the previously solved sub-problems. The solutions of sub-problemsare combined in order to achieve the best solution.So we can say − The problem should be able to be divided into smaller overlapping sub-problem. An optimum solution can be achieved by using an optimum solution of smaller sub-problems. Dynamic algorithms use memorization.ComparisonIn contrast to greedy algorithms, where local optimization is addressed, dynamicalgorithms are motivated for an overall optimization of the problem.In contrast to divide and conquer algorithms, where solutions are combined to achieve anoverall solution, dynamic algorithms use the output of a smaller sub-problem and then tryto optimize a bigger sub-problem. Dynamic algorithms use memorization to remember theoutput of already solved sub-problems.ExampleThe following computer problems can be solved using dynamic programming approach − Fibonacci number series Knapsack problem Tower of Hanoi All pair shortest path by Floyd-Warshall Shortest path by Dijkstra Project schedulingDynamic programming can be used in both top-down and bottom-up manner. And ofcourse, most of the times, referring to the previous solution output is cheaper than re-computing in terms of CPU cycles.7. Dynamic Programming
Data Structures & Algorithms21Data Structures
Data Structures & Algorithms22This chapter explains the basic terms related to data structure.DataDefinitionData Definition defines a particular data with the following characteristics. Atomic − Definition should define a single concept. Traceable − Definition should be able to be mapped to some data element. Accurate − Definition should be unambiguous. Clear and Concise − Definition should be understandable.DataObjectData Object represents an object having a data.DataTypeData type is a way to classify various types of data such as integer, string, etc. whichdetermines the values that can be used with the corresponding type of data, the type ofoperations that can be performed on the corresponding type of data. There are two datatypes − Built-in Data Type Derived Data TypeBuilt-in Data TypeThose data types for which a language has built-in support are known as Built-in Datatypes. For example, most of the languages provide the following built-in data types. Integers Boolean (true, false) Floating (Decimal numbers) Character and Strings8. Basic Concepts
Data Structures & Algorithms23Derived Data TypeThose data types which are implementation independent as they can be implemented inone or the other way are known as derived data types. These data types are normally builtby the combination of primary or built-in data types and associated operations on them.For example − List Array Stack QueueBasicOperationsThe data in the data structures are processed by certain operations. The particular datastructure chosen largely depends on the frequency of the operation that needs to beperformed on the data structure. Traversing Searching Insertion Deletion Sorting Merging
Data Structures & Algorithms24Array is a container which can hold a fix number of items and these items should be of thesame type. Most of the data structures make use of arrays to implement their algorithms.Following are the important terms to understand the concept of Array. Element − Each item stored in an array is called an element. Index − Each location of an element in an array has a numerical index, which isused to identify the element.ArrayRepresentationArrays can be declared in various ways in different languages. For illustration, let's take Carray declaration.Arrays can be declared in various ways in different languages. For illustration, let's take Carray declaration.As per the above illustration, following are the important points to be considered. Index starts with 0. Array length is 8 which means it can store 8 elements. Each element can be accessed via its index. For example, we can fetch an elementat index 6 as 9.9. Arrays
Data Structures & Algorithms25BasicOperationsFollowing are the basic operations supported by an array. Traverse − Prints all the array elements one by one. Insertion − Adds an element at the given index. Deletion − Deletes an element at the given index. Search − Searches an element using the given index or by the value. Update − Updates an element at the given index.In C, when an array is initialized with size, then it assigns defaults values to its elementsin following order.Data Type Default Valuebool falsechar 0int 0float 0.0double 0.0fvoidwchar_t 0InsertionOperationInsert operation is to insert one or more data elements into an array. Based on therequirement, a new element can be added at the beginning, end, or any given index ofarray.Here, we see a practical implementation of insertion operation, where we add data at theend of the array −AlgorithmLet Array be a linear unordered array of MAX elements.
Data Structures & Algorithms26ExampleResultLet LA be a Linear Array (unordered) with N elements and K is a positive integer suchthat K<=N. Following is the algorithm where ITEM is inserted into the Kthposition of LA −1. Start2. Set J=N3. Set N = N+14. Repeat steps 5 and 6 while J >= K5. Set LA[J+1] = LA[J]6. Set J = J-17. Set LA[K] = ITEM8. StopExampleFollowing is the implementation of the above algorithm −#include <stdio.h>main() {int LA[] = {1,3,5,7,8};int item = 10, k = 3, n = 5;int i = 0, j = n;printf("The original array elements are :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}n = n + 1;while( j >= k){LA[j+1] = LA[j];j = j - 1;}
Data Structures & Algorithms27LA[k] = item;printf("The array elements after insertion :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}}When we compile and execute the above program, it produces the following result −The original array elements are :LA[0]=1LA[1]=3LA[2]=5LA[3]=7LA[4]=8The array elements after insertion :LA[0]=1LA[1]=3LA[2]=5LA[3]=10LA[4]=7LA[5]=8For other variations of array insertion operation click hereArrayInsertionsIn the previous section, we have learnt how the insertion operation works. It is not alwaysnecessary that an element is inserted at the end of an array. Following can be a situationwith array insertion − Insertion at the beginning of an array Insertion at the given index of an array Insertion after the given index of an array Insertion before the given index of an array
Data Structures & Algorithms28InsertionattheBeginningofanArrayWhen the insertion happens at the beginning, it causes all the existing data items to shiftone step downward. Here, we design and implement an algorithm to insert an element atthe beginning of an array.AlgorithmWe assume A is an array with N elements. The maximum numbers of elements it canstore is defined by MAX. We shall first check if an array has any empty space to store anyelement and then we proceed with the insertion process.beginIF N = MAX, returnELSEN = N + 1For All Elements in AMove to next adjacent locationA[FIRST] = New_ElementendImplementation in C#include <stdio.h>#define MAX 5void main() {int array[MAX] = {2, 3, 4, 5};int N = 4; // number of elements in arrayint i = 0; // loop variableint value = 1; // new data element to be stored in array// print array before insertionprintf("Printing array before insertion −n");
Data Structures & Algorithms29for(i = 0; i < N; i++) {printf("array[%d] = %d n", i, array[i]);}// now shift rest of the elements downwardsfor(i = N; i >= 0; i--) {array[i+1] = array[i];}// add new element at first positionarray[0] = value;// increase N to reflect number of elementsN++;// print to confirmprintf("Printing array after insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %dn", i, array[i]);}}This program should yield the following output −Printing array before insertion −array[0] = 2array[1] = 3array[2] = 4array[3] = 5Printing array after insertion −array[0] = 0array[1] = 2array[2] = 3array[3] = 4array[4] = 5
Data Structures & Algorithms30InsertionattheGivenIndexofanArrayIn this scenario, we are given the exact location (index) of an array where a new dataelement (value) needs to be inserted. First we shall check if the array is full, if it is not,then we shall move all data elements from that location one step downward. This will makeroom for a new data element.AlgorithmWe assume A is an array with N elements. The maximum numbers of elements it canstore is defined by MAX.beginIF N = MAX, returnELSEN = N + 1SEEK Location indexFor All Elements from A[index] to A[N]Move to next adjacent locationA[index] = New_ElementendImplementation in C#include <stdio.h>#define MAX 5void main() {int array[MAX] = {1, 2, 4, 5};int N = 4; // number of elements in arrayint i = 0; // loop variableint index = 2; // index location to insert new valueint value = 3; // new data element to be inserted// print array before insertionprintf("Printing array before insertion −n");
Data Structures & Algorithms31for(i = 0; i < N; i++) {printf("array[%d] = %d n", i, array[i]);}// now shift rest of the elements downwardsfor(i = N; i >= index; i--) {array[i+1] = array[i];}// add new element at first positionarray[index] = value;// increase N to reflect number of elementsN++;// print to confirmprintf("Printing array after insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %dn", i, array[i]);}}If we compile and run the above program, it will produce the following result −Printing array before insertion −array[0] = 1array[1] = 2array[2] = 4array[3] = 5Printing array after insertion −array[0] = 1array[1] = 2array[2] = 3array[3] = 4array[4] = 5
Data Structures & Algorithms32InsertionAftertheGivenIndexofanArrayIn this scenario we are given a location (index) of an array after which a new data element(value) has to be inserted. Only the seek process varies, the rest of the activities are thesame as in the previous example.AlgorithmWe assume A is an array with N elements. The maximum numbers of elements it canstore is defined by MAX.beginIF N = MAX, returnELSEN = N + 1SEEK Location indexFor All Elements from A[index + 1] to A[N]Move to next adjacent locationA[index + 1] = New_ElementendImplementation in C#include <stdio.h>#define MAX 5void main() {int array[MAX] = {1, 2, 4, 5};int N = 4; // number of elements in arrayint i = 0; // loop variableint index = 1; // index location after which value will be insertedint value = 3; // new data element to be inserted// print array before insertionprintf("Printing array before insertion −n");
Data Structures & Algorithms33for(i = 0; i < N; i++) {printf("array[%d] = %d n", i, array[i]);}// now shift rest of the elements downwardsfor(i = N; i >= index + 1; i--) {array[i + 1] = array[i];}// add new element at first positionarray[index + 1] = value;// increase N to reflect number of elementsN++;// print to confirmprintf("Printing array after insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %dn", i, array[i]);}}If we compile and run the above program, it will produce the following result −Printing array before insertion −array[0] = 1array[1] = 2array[2] = 4array[3] = 5Printing array after insertion −array[0] = 1array[1] = 2array[2] = 3array[3] = 4
Data Structures & Algorithms34array[4] = 5InsertionBeforetheGivenIndexofanArrayIn this scenario we are given a location (index) of an array before which a new dataelement (value) has to be inserted. This time we seek till index-1, i.e., one locationahead of the given index. Rest of the activities are the same as in the previous example.AlgorithmWe assume A is an array with N elements. The maximum numbers of elements it canstore is defined by MAX.beginIF N = MAX, returnELSEN = N + 1SEEK Location indexFor All Elements from A[index - 1] to A[N]Move to next adjacent locationA[index - 1] = New_ElementendImplementation in C#include <stdio.h>#define MAX 5
Data Structures & Algorithms35void main() {int array[MAX] = {1, 2, 4, 5};int N = 4; // number of elements in arrayint i = 0; // loop variableint index = 3; // index location before which value will be insertedint value = 3; // new data element to be inserted// print array before insertionprintf("Printing array before insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %d n", i, array[i]);}// now shift rest of the elements downwardsfor(i = N; i >= index + 1; i--) {array[i + 1] = array[i];}// add new element at first positionarray[index + 1] = value;// increase N to reflect number of elementsN++;// print to confirmprintf("Printing array after insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %dn", i, array[i]);}}If we compile and run the above program, it will produce the following result −Printing array before insertion −array[0] = 1array[1] = 2
Data Structures & Algorithms36array[2] = 4array[3] = 5Printing array after insertion −array[0] = 1array[1] = 2array[2] = 4array[3] = 5array[4] = 3DeletionOperationDeletion refers to removing an existing element from the array and re-organizing allelements of an array.AlgorithmConsider LA is a linear array with N elements and K is a positive integer such that K<=N.Following is the algorithm to delete an element available at the Kthposition of LA.1. Start2. Set J=K3. Repeat steps 4 and 5 while J < N4. Set LA[J-1] = LA[J]5. Set J = J+16. Set N = N-17. StopExampleFollowing is the implementation of the above algorithm −#include <stdio.h>main() {int LA[] = {1,3,5,7,8};int k = 3, n = 5;int i, j;printf("The original array elements are :n");
Data Structures & Algorithms37for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}j = k;while( j < n){LA[j-1] = LA[j];j = j + 1;}n = n -1;printf("The array elements after deletion :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}}When we compile and execute the above program, it produces the following result −The original array elements are :LA[0]=1LA[1]=3LA[2]=5LA[3]=7LA[4]=8The array elements after deletion :LA[0]=1LA[1]=3LA[2]=7LA[3]=8SearchOperationYou can perform a search for an array element based on its value or its index.Algorithm
Data Structures & Algorithms38Consider LA is a linear array with N elements and K is a positive integer such that K<=N.Following is the algorithm to find an element with a value of ITEM using sequential search.1. Start2. Set J=03. Repeat steps 4 and 5 while J < N4. IF LA[J] is equal ITEM THEN GOTO STEP 65. Set J = J +16. PRINT J, ITEM7. StopExampleFollowing is the implementation of the above algorithm −#include <stdio.h>main() {int LA[] = {1,3,5,7,8};int item = 5, n = 5;int i = 0, j = 0;printf("The original array elements are :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}while( j < n){if( LA[j] == item ){break;}j = j + 1;}printf("Found element %d at position %dn", item, j+1);}When we compile and execute the above program, it produces the following result −
Data Structures & Algorithms39The original array elements are :LA[0]=1LA[1]=3LA[2]=5LA[3]=7LA[4]=8Found element 5 at position 3UpdateOperationUpdate operation refers to updating an existing element from the array at a given index.AlgorithmConsider LA is a linear array with N elements and K is a positive integer such that K<=N.Following is the algorithm to update an element available at the Kthposition of LA.1. Start2. Set LA[K-1] = ITEM3. StopExampleFollowing is the implementation of the above algorithm −#include <stdio.h>main() {int LA[] = {1,3,5,7,8};int k = 3, n = 5, item = 10;int i, j;printf("The original array elements are :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}LA[k-1] = item;printf("The array elements after updation :n");
Data Structures & Algorithms40for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}}When we compile and execute the above program, it produces the following result −The original array elements are :LA[0]=1LA[1]=3LA[2]=5LA[3]=7LA[4]=8The array elements after updation :LA[0]=1LA[1]=3LA[2]=10LA[3]=7LA[4]=8
Data Structures & Algorithms41Linked List
Data Structures & Algorithms42A linked list is a sequence of data structures, which are connected together via links.Linked List is a sequence of links which contains items. Each link contains a connection toanother link. Linked list is the second most-used data structure after array. Following arethe important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. Linked List − A Linked List contains the connection link to the first link calledFirst.LinkedListRepresentationLinked list can be visualized as a chain of nodes, where every node points to the nextnode.As per the above illustration, following are the important points to be considered. Linked List contains a link element called first. Each link carries a data field(s) and a link field called next. Each link is linked with its next link using its next link. Last link carries a link as null to mark the end of the list.TypesofLinkedListFollowing are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next andthe first element has a link to the last element as previous.10. Linked List ─ Basics
Data Structures & Algorithms43BasicOperationsFollowing are the basic operations supported by a list. Insertion − Adds an element at the beginning of the list. Deletion − Deletes an element at the beginning of the list. Display − Displays the complete list. Search − Searches an element using the given key. Delete − Deletes an element using the given key.InsertionOperationAdding a new node in linked list is a more than one step activity. We shall learn this withdiagrams here. First, create a node using the same structure and find the location whereit has to be inserted.Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C(RightNode). Then point B.next to C -NewNode.next −> RightNode;It should look like this −
Data Structures & Algorithms44Now, the next node at the left should point to the new node.LeftNode.next −> NewNode;This will put the new node in the middle of the two. The new list should look like this −Similar steps should be taken if the node is being inserted at the beginning of the list.While inserting it at the end, the second last node of the list should point to the new nodeand the new node will point to NULL.DeletionOperationDeletion is also a more than one step process. We shall learn with pictorial representation.First, locate the target node to be removed, by using searching algorithms.The left (previous) node of the target node now should point to the next node of the targetnode −LeftNode.next −> TargetNode.next;
Data Structures & Algorithms45This will remove the link that was pointing to the target node. Now, using the followingcode, we will remove what the target node is pointing at.TargetNode.next −> NULL;We need to use the deleted node. We can keep that in memory otherwise we can simplydeallocate memory and wipe off the target node completely.ReverseOperationThis operation is a thorough one. We need to make the last node to be pointed by thehead node and reverse the whole linked list.First, we traverse to the end of the list. It should be pointing to NULL. Now, we shall makeit point to its previous node −
Data Structures & Algorithms46We have to make sure that the last node is not the lost node. So we'll have some tempnode, which looks like the head node pointing to the last node. Now, we shall make all leftside nodes point to their previous nodes one by one.Except the node (first node) pointed by the head node, all nodes should point to theirpredecessor, making them their new successor. The first node will point to NULL.We'll make the head node point to the new first node by using the temp node.The linked list is now reversed. To see linked list implementation in C programminglanguage, please click here.LinkedListPrograminCA linked list is a sequence of data structures, which are connected together via links.Linked List is a sequence of links which contains items. Each link contains a connection toanother link. Linked list is the second most-used data structure after array.
Data Structures & Algorithms47Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>struct node{int data;int key;struct node *next;};struct node *head = NULL;struct node *current = NULL;//display the listvoid printList(){struct node *ptr = head;printf("n[ ");//start from the beginningwhile(ptr != NULL){printf("(%d,%d) ",ptr->key,ptr->data);ptr = ptr->next;}printf(" ]");}//insert link at the first locationvoid insertFirst(int key, int data){//create a link
Data Structures & Algorithms48struct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;//point it to old first nodelink->next = head;//point first to new first nodehead = link;}//delete first itemstruct node* deleteFirst(){//save reference to first linkstruct node *tempLink = head;//mark next to first link as firsthead = head->next;//return the deleted linkreturn tempLink;}//is list emptybool isEmpty(){return head == NULL;}int length(){int length = 0;struct node *current;
Data Structures & Algorithms49for(current = head; current != NULL; current = current->next){length++;}return length;}//find a link with given keystruct node* find(int key){//start from the first linkstruct node* current = head;//if list is emptyif(head == NULL){return NULL;}//navigate through listwhile(current->key != key){//if it is last nodeif(current->next == NULL){return NULL;}else {//go to next linkcurrent = current->next;}}//if data found, return the current Linkreturn current;}
Data Structures & Algorithms50//delete a link with given keystruct node* delete(int key){//start from the first linkstruct node* current = head;struct node* previous = NULL;//if list is emptyif(head == NULL){return NULL;}//navigate through listwhile(current->key != key){//if it is last nodeif(current->next == NULL){return NULL;}else {//store reference to current linkprevious = current;//move to next linkcurrent = current->next;}}//found a match, update the linkif(current == head) {//change first to point to next linkhead = head->next;}else {//bypass the current linkprevious->next = current->next;}return current;}
Data Structures & Algorithms51void sort(){int i, j, k, tempKey, tempData ;struct node *current;struct node *next;int size = length();k = size ;for ( i = 0 ; i < size - 1 ; i++, k-- ) {current = head ;next = head->next ;for ( j = 1 ; j < k ; j++ ) {if ( current->data > next->data ) {tempData = current->data ;current->data = next->data;next->data = tempData ;tempKey = current->key;current->key = next->key;next->key = tempKey;}current = current->next;next = next->next;}}}void reverse(struct node** head_ref) {struct node* prev = NULL;struct node* current = *head_ref;struct node* next;
Data Structures & Algorithms52while (current != NULL) {next = current->next;current->next = prev;prev = current;current = next;}*head_ref = prev;}main() {insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);insertFirst(5,40);insertFirst(6,56);printf("Original List: ");//print listprintList();while(!isEmpty()){struct node *temp = deleteFirst();printf("nDeleted value:");printf("(%d,%d) ",temp->key,temp->data);}printf("nList after deleting all items: ");printList();insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);
Data Structures & Algorithms53insertFirst(5,40);insertFirst(6,56);printf("nRestored List: ");printList();printf("n");struct node *foundLink = find(4);if(foundLink != NULL){printf("Element found: ");printf("(%d,%d) ",foundLink->key,foundLink->data);printf("n");}else {printf("Element not found.");}delete(4);printf("List after deleting an item: ");printList();printf("n");foundLink = find(4);if(foundLink != NULL){printf("Element found: ");printf("(%d,%d) ",foundLink->key,foundLink->data);printf("n");}else {printf("Element not found.");}printf("n");sort();printf("List after sorting the data: ");printList();reverse(&head);
Data Structures & Algorithms54printf("nList after reversing the data: ");printList();}If we compile and run the above program, it will produce the following result −Original List:[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]Deleted value:(6,56)Deleted value:(5,40)Deleted value:(4,1)Deleted value:(3,30)Deleted value:(2,20)Deleted value:(1,10)List after deleting all items:[ ]Restored List:[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]Element found: (4,1)List after deleting an item:[ (6,56) (5,40) (3,30) (2,20) (1,10) ]Element not found.List after sorting the data:[ (1,10) (2,20) (3,30) (5,40) (6,56) ]List after reversing the data:[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
Data Structures & Algorithms55Doubly Linked List is a variation of Linked list in which navigation is possible in both ways,either forward and backward easily as compared to Single Linked List. Following are theimportant terms to understand the concept of doubly linked list. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. Prev − Each link of a linked list contains a link to the previous link called Prev. Linked List − A Linked List contains the connection link to the first link calledFirst and to the last link called Last.DoublyLinkedListRepresentationAs per the above illustration, following are the important points to be considered. Doubly Linked List contains a link element called first and last. Each link carries a data field(s) and a link field called next. Each link is linked with its next link using its next link. Each link is linked with its previous link using its previous link. The last link carries a link as null to mark the end of the list.BasicOperationsFollowing are the basic operations supported by a list. Insertion − Adds an element at the beginning of the list. Deletion − Deletes an element at the beginning of the list. Insert Last − Adds an element at the end of the list. Delete Last − Deletes an element from the end of the list.11. Doubly Linked List
Data Structures & Algorithms56 Insert After − Adds an element after an item of the list. Delete − Deletes an element from the list using the key. Display forward − Displays the complete list in a forward manner. Display backward − Displays the complete list in a backward manner.InsertionOperationFollowing code demonstrates the insertion operation at the beginning of a doubly linkedlist.//insert link at the first locationvoid insertFirst(int key, int data) {//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;if(isEmpty()) {//make it the last linklast = link;}else {//update first prev linkhead->prev = link;}//point it to old first linklink->next = head;//point first to new first linkhead = link;}
Data Structures & Algorithms57DeletionOperationFollowing code demonstrates the deletion operation at the beginning of a doubly linkedlist.//delete first itemstruct node* deleteFirst() {//save reference to first linkstruct node *tempLink = head;//if only one linkif(head->next == NULL) {last = NULL;}else {head->next->prev = NULL;}head = head->next;//return the deleted linkreturn tempLink;}InsertionattheEndofanOperationFollowing code demonstrates the insertion operation at the last position of a doubly linkedlist.//insert link at the last locationvoid insertLast(int key, int data) {//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;
Data Structures & Algorithms58if(isEmpty()) {//make it the last linklast = link;}else {//make link a new last linklast->next = link;//mark old last node as prev of new linklink->prev = last;}//point last to new last nodelast = link;}To see the implementation in C programming language, please click here.DoublyLinkedListPrograminCDoubly Linked List is a variation of Linked list in which navigation is possible in both ways,either forward and backward easily as compared to Single Linked List.Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>struct node {int data;int key;struct node *next;struct node *prev;};
Data Structures & Algorithms59//this link always point to first Linkstruct node *head = NULL;//this link always point to last Linkstruct node *last = NULL;struct node *current = NULL;//is list emptybool isEmpty(){return head == NULL;}int length(){int length = 0;struct node *current;for(current = head; current != NULL; current = current->next){length++;}return length;}//display the list in from first to lastvoid displayForward(){//start from the beginningstruct node *ptr = head;//navigate till the end of the listprintf("n[ ");while(ptr != NULL){printf("(%d,%d) ",ptr->key,ptr->data);ptr = ptr->next;}
Data Structures & Algorithms60printf(" ]");}//display the list from last to firstvoid displayBackward(){//start from the laststruct node *ptr = last;//navigate till the start of the listprintf("n[ ");while(ptr != NULL){//print dataprintf("(%d,%d) ",ptr->key,ptr->data);//move to next itemptr = ptr ->prev;printf(" ");}printf(" ]");}//insert link at the first locationvoid insertFirst(int key, int data){//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;if(isEmpty()){//make it the last linklast = link;
Data Structures & Algorithms61}else {//update first prev linkhead->prev = link;}//point it to old first linklink->next = head;//point first to new first linkhead = link;}//insert link at the last locationvoid insertLast(int key, int data){//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;if(isEmpty()){//make it the last linklast = link;}else {//make link a new last linklast->next = link;//mark old last node as prev of new linklink->prev = last;}//point last to new last nodelast = link;}
Data Structures & Algorithms62//delete first itemstruct node* deleteFirst(){//save reference to first linkstruct node *tempLink = head;//if only one linkif(head->next == NULL){last = NULL;}else {head->next->prev = NULL;}head = head->next;//return the deleted linkreturn tempLink;}//delete link at the last locationstruct node* deleteLast(){//save reference to last linkstruct node *tempLink = last;//if only one linkif(head->next == NULL){head = NULL;}else {last->prev->next = NULL;}last = last->prev;//return the deleted linkreturn tempLink;}
Data Structures & Algorithms63//delete a link with given keystruct node* delete(int key){//start from the first linkstruct node* current = head;struct node* previous = NULL;//if list is emptyif(head == NULL){return NULL;}//navigate through listwhile(current->key != key){//if it is last nodeif(current->next == NULL){return NULL;}else {//store reference to current linkprevious = current;//move to next linkcurrent = current->next;}}//found a match, update the linkif(current == head) {//change first to point to next linkhead = head->next;}else {//bypass the current link
Data Structures & Algorithms64current->prev->next = current->next;}if(current == last){//change last to point to prev linklast = current->prev;}else {current->next->prev = current->prev;}return current;}bool insertAfter(int key, int newKey, int data){//start from the first linkstruct node *current = head;//if list is emptyif(head == NULL){return false;}//navigate through listwhile(current->key != key){//if it is last nodeif(current->next == NULL){return false;}else {//move to next linkcurrent = current->next;}}//create a linkstruct node *newLink = (struct node*) malloc(sizeof(struct node));newLink->key = key;
Data Structures & Algorithms65newLink->data = data;if(current == last) {newLink->next = NULL;last = newLink;}else {newLink->next = current->next;current->next->prev = newLink;}newLink->prev = current;current->next = newLink;return true;}main() {insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);insertFirst(5,40);insertFirst(6,56);printf("nList (First to Last): ");displayForward();printf("n");printf("nList (Last to first): ");displayBackward();printf("nList , after deleting first record: ");deleteFirst();displayForward();printf("nList , after deleting last record: ");
Data Structures & Algorithms66deleteLast();displayForward();printf("nList , insert after key(4) : ");insertAfter(4,7, 13);displayForward();printf("nList , after delete key(4) : ");delete(4);displayForward();}If we compile and run the above program, it will produce the following result −List (First to Last):[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]List (Last to first):[ (1,10) (2,20) (3,30) (4,1) (5,40) (6,56) ]List , after deleting first record:[ (5,40) (4,1) (3,30) (2,20) (1,10) ]List , after deleting last record:[ (5,40) (4,1) (3,30) (2,20) ]List , insert after key(4) :[ (5,40) (4,1) (4,13) (3,30) (2,20) ]List , after delete key(4) :[ (5,40) (4,13) (3,30) (2,20) ]
Data Structures & Algorithms67Circular Linked List is a variation of Linked list in which the first element points to the lastelement and the last element points to the first element. Both Singly Linked List andDoubly Linked List can be made into a circular linked list.SinglyLinkedListasCircularIn singly linked list, the next pointer of the last node points to the first node.DoublyLinkedListasCircularIn doubly linked list, the next pointer of the last node points to the first node and theprevious pointer of the first node points to the last node making the circular in bothdirections.As per the above illustration, following are the important points to be considered. The last link's next points to the first link of the list in both cases of singly as wellas doubly linked list. The first link's previous points to the last of the list in case of doubly linked list.BasicOperationsFollowing are the important operations supported by a circular list. insert − Inserts an element at the start of the list. delete – Deletes an element from the start of the list. display − Displays the list.12. Circular Linked List
Data Structures & Algorithms68InsertionOperationFollowing code demonstrates the insertion operation in a circular linked list based on singlelinked list.//insert link at the first locationvoid insertFirst(int key, int data) {//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data= data;if (isEmpty()) {head = link;head->next = head;}else {//point it to old first nodelink->next = head;//point first to new first nodehead = link;}}DeletionOperationFollowing code demonstrates the deletion operation in a circular linked list based on singlelinked list.//delete first itemstruct node * deleteFirst() {//save reference to first linkstruct node *tempLink = head;if(head->next == head){head = NULL;return tempLink;}
Data Structures & Algorithms69//mark next to first link as firsthead = head->next;//return the deleted linkreturn tempLink;}DisplayListOperationFollowing code demonstrates the display list operation in a circular linked list.//display the listvoid printList() {struct node *ptr = head;printf("n[ ");//start from the beginningif(head != NULL) {while(ptr->next != ptr) {printf("(%d,%d) ",ptr->key,ptr->data);ptr = ptr->next;}}printf(" ]");}To know about its implementation in C programming language, please click here.CircularLinkedListPrograminCCircular Linked List is a variation of Linked list in which the first element points to the lastelement and the last element points to the first element. Both Singly Linked List andDoubly Linked List can be made into a circular linked list.
Data Structures & Algorithms70Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>struct node {int data;int key;struct node *next;};struct node *head = NULL;struct node *current = NULL;bool isEmpty(){return head == NULL;}int length(){int length = 0;//if list is emptyif(head == NULL){return 0;}current = head->next;while(current != head){length++;current = current->next;}
Data Structures & Algorithms71return length;}//insert link at the first locationvoid insertFirst(int key, int data){//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;if (isEmpty()) {head = link;head->next = head;}else {//point it to old first nodelink->next = head;//point first to new first nodehead = link;}}//delete first itemstruct node * deleteFirst(){//save reference to first linkstruct node *tempLink = head;if(head->next == head){head = NULL;return tempLink;}
Data Structures & Algorithms72//mark next to first link as firsthead = head->next;//return the deleted linkreturn tempLink;}//display the listvoid printList(){struct node *ptr = head;printf("n[ ");//start from the beginningif(head != NULL){while(ptr->next != ptr){printf("(%d,%d) ",ptr->key,ptr->data);ptr = ptr->next;}}printf(" ]");}main() {insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);insertFirst(5,40);insertFirst(6,56);
Data Structures & Algorithms73printf("Original List: ");//print listprintList();while(!isEmpty()){struct node *temp = deleteFirst();printf("nDeleted value:");printf("(%d,%d) ",temp->key,temp->data);}printf("nList after deleting all items: ");printList();}If we compile and run the above program, it will produce the following result −Original List:[ (6,56) (5,40) (4,1) (3,30) (2,20) ]Deleted value:(6,56)Deleted value:(5,40)Deleted value:(4,1)Deleted value:(3,30)Deleted value:(2,20)Deleted value:(1,10)List after deleting all items:[ ]
Data Structures & Algorithms74Stack & Queue
Data Structures & Algorithms75A stack is an Abstract Data Type (ADT), commonly used in most programming languages.It is named stack as it behaves like a real-world stack, for example – a deck of cards or apile of plates, etc.A real-world stack allows operations at one end only. For example, we can place or removea card or plate from the top of the stack only. Likewise, Stack ADT allows all dataoperations at one end only. At any given time, we can only access the top element of astack.This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, theelement which is placed (inserted or added) last, is accessed first. In stack terminology,insertion operation is called PUSH operation and removal operation iscalled POP operation.StackRepresentationThe following diagram depicts a stack and its operations −A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stackcan either be a fixed size one or it may have a sense of dynamic resizing. Here, we aregoing to implement stack using arrays, which makes it a fixed size stack implementation.13. Stack
Data Structures & Algorithms76BasicOperationsStack operations may involve initializing the stack, using it and then de-initializing it. Apartfrom these basic stuffs, a stack is used for the following two primary operations − push() − Pushing (storing) an element on the stack. pop() − Removing (accessing) an element from the stack.When data is PUSHed onto stack.To use a stack efficiently, we need to check the status of stack as well. For the samepurpose, the following functionality is added to stacks − peek() − get the top data element of the stack, without removing it. isFull() − check if stack is full. isEmpty() − check if stack is empty.At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointeralways represents the top of the stack, hence named top. The top pointer provides topvalue of the stack without actually removing it.First we should learn about procedures to support stack functions −peek()Algorithm of peek() function −begin procedure peekreturn stack[top]end procedureImplementation of peek() function in C programming language −int peek() {return stack[top];}
Data Structures & Algorithms77isfull()Algorithm of isfull() function −begin procedure isfullif top equals to MAXSIZEreturn trueelsereturn falseendifend procedureImplementation of isfull() function in C programming language −bool isfull() {if(top == MAXSIZE)return true;elsereturn false;}isempty()Algorithm of isempty() function −begin procedure isemptyif top less than 1return trueelsereturn falseendifend procedure
Data Structures & Algorithms78Implementation of isempty() function in C programming language is slightly different. Weinitialize top at -1, as the index in array starts from 0. So we check if the top is below zeroor -1 to determine if the stack is empty. Here's the code −bool isempty() {if(top == -1)return true;elsereturn false;}PushOperationThe process of putting a new data element onto stack is known as a Push Operation. Pushoperation involves a series of steps − Step 1 − Checks if the stack is full. Step 2 − If the stack is full, produces an error and exit. Step 3 − If the stack is not full, increments top to point next empty space. Step 4 − Adds data element to the stack location, where top is pointing. Step 5 − Returns success.
Data Structures & Algorithms79If the linked list is used to implement the stack, then in step 3, we need to allocate spacedynamically.Algorithm for PUSH OperationA simple algorithm for Push operation can be derived as follows −begin procedure push: stack, dataif stack is fullreturn nullendiftop ← top + 1stack[top] ← dataend procedureImplementation of this algorithm in C, is very easy. See the following code −void push(int data) {if(!isFull()) {top = top + 1;stack[top] = data;}else {printf("Could not insert data, Stack is full.n");}}PopOperationAccessing the content while removing it from the stack, is known as a Pop Operation. Inan array implementation of pop() operation, the data element is not actually removed,instead top is decremented to a lower position in the stack to point to the next value. Butin linked-list implementation, pop() actually removes data element and deallocatesmemory space.A Pop operation may involve the following steps − Step 1 − Checks if the stack is empty. Step 2 − If the stack is empty, produces an error and exit.
Data Structures & Algorithms80 Step 3 − If the stack is not empty, accesses the data element at which top ispointing. Step 4 − Decreases the value of top by 1. Step 5 − Returns success.Algorithm for Pop OperationA simple algorithm for Pop operation can be derived as follows −begin procedure pop: stackif stack is emptyreturn nullendifdata ← stack[top]top ← top - 1return dataend procedure
Data Structures & Algorithms81Implementation of this algorithm in C, is as follows −int pop(int data) {if(!isempty()) {data = stack[top];top = top - 1;return data;}else {printf("Could not retrieve data, Stack is empty.n");}}For a complete stack program in C programming language, please click here.StackPrograminCWe shall see the stack implementation in C programming language here. You can try theprogram by clicking on the Try-it button. To learn the theory aspect of stacks, click on visitprevious page.Implementation in C#include <stdio.h>int MAXSIZE = 8;int stack[8];int top = -1;int isempty() {if(top == -1)return 1;elsereturn 0;}
Data Structures & Algorithms82int isfull() {if(top == MAXSIZE)return 1;elsereturn 0;}int peek() {return stack[top];}int pop() {int data;if(!isempty()) {data = stack[top];top = top - 1;return data;}else {printf("Could not retrieve data, Stack is empty.n");}}int push(int data) {if(!isfull()) {top = top + 1;stack[top] = data;}else {printf("Could not insert data, Stack is full.n");}}
Data Structures & Algorithms83int main() {// push items on to the stackpush(3);push(5);push(9);push(1);push(12);push(15);printf("Element at top of the stack: %dn" ,peek());printf("Elements: n");// print stack datawhile(!isempty()) {int data = pop();printf("%dn",data);}printf("Stack full: %sn" , isfull()?"true":"false");printf("Stack empty: %sn" , isempty()?"true":"false");return 0;}If we compile and run the above program, it will produce the following result −Element at top of the stack: 15Elements:15121953Stack full: false
Data Structures & Algorithms84Stack empty: true
Data Structures & Algorithms85The way to write arithmetic expression is known as a notation. An arithmetic expressioncan be written in three different but equivalent notations, i.e., without changing theessence or output of an expression. These notations are − Infix Notation Prefix (Polish) Notation Postfix (Reverse-Polish) NotationThese notations are named as how they use operator in expression. We shall learn thesame here in this chapter.InfixNotationWe write expression in infix notation, e.g. a-b+c, where operators are used in-betweenoperands. It is easy for us humans to read, write, and speak in infix notation but the samedoes not go well with computing devices. An algorithm to process infix notation could bedifficult and costly in terms of time and space consumption.PrefixNotationIn this notation, operator is prefixed to operands, i.e. operator is written ahead ofoperands. For example, +ab. This is equivalent to its infix notation a+b. Prefix notationis also known as Polish Notation.PostfixNotationThis notation style is known as Reversed Polish Notation. In this notation style, theoperator is postfixed to the operands i.e., the operator is written after the operands. Forexample, ab+. This is equivalent to its infix notation a+b.The following table briefly tries to show the difference in all three notations −Sr.No.Infix Notation Prefix Notation Postfix Notation1 a + b + a b a b +2 (a + b) * c * + a b c a b + c *3 a * (b + c) * a + b c a b c + *4 a / b + c / d + / a b / c d a b / c d / +14. Expression Parsing
Data Structures & Algorithms865 (a + b) * (c + d) * + a b + c d a b + c d + *6 ((a + b) * c) - d - * + a b c d a b + c * d -ParsingExpressionsAs we have discussed, it is not a very efficient way to design an algorithm or program toparse infix notations. Instead, these infix notations are first converted into either postfixor prefix notations and then computed.To parse any arithmetic expression, we need to take care of operator precedence andassociativity also.PrecedenceWhen an operand is in between two different operators, which operator will take theoperand first, is decided by the precedence of an operator over others. For example −As multiplication operation has precedence over addition, b * c will be evaluated first. Atable of operator precedence is provided later.AssociativityAssociativity describes the rule where operators with the same precedence appear in anexpression. For example, in expression a+b−c, both + and – have the same precedence,then which part of the expression will be evaluated first, is determined by associativity ofthose operators. Here, both + and − are left associative, so the expression will beevaluated as (a+b)−c.Precedence and associativity determines the order of evaluation of an expression.Following is an operator precedence and associativity table (highest to lowest) −Sr.No.Operator Precedence Associativity1 Exponentiation ^ Highest Right Associative2 Multiplication ( * ) & Division ( / ) Second Highest Left Associative3 Addition ( + ) & Subtraction ( − ) Lowest Left AssociativeThe above table shows the default behavior of operators. At any point of time in expressionevaluation, the order can be altered by using parenthesis. For example −
Data Structures & Algorithms87In a+b*c, the expression part b*c will be evaluated first, with multiplication asprecedence over addition. We here use parenthesis for a+b to be evaluated first,like (a+b)*c.PostfixEvaluationAlgorithmWe shall now look at the algorithm on how to evaluate postfix notation −Step 1 − scan the expression from left to rightStep 2 − if it is an operand push it to stackStep 3 − if it is an operator pull operand from stack and perform operationStep 4 − store the output of step 3, back to stackStep 5 − scan the expression until all operands are consumedStep 6 − pop the stack and perform operationTo see the implementation in C programming language, please click hereExpressionParsingUsingStackInfix notation is easier for humans to read and understand whereas for electronic machineslike computers, postfix is the best form of expression to parse. We shall see here a programto convert and evaluate infix notation to postfix notation −#include<stdio.h>#include<string.h>//char stackchar stack[25];int top = -1;void push(char item) {stack[++top] = item;}char pop() {return stack[top--];}
Data Structures & Algorithms88//returns precedence of operatorsint precedence(char symbol) {switch(symbol) {case '+':case '-':return 2;break;case '*':case '/':return 3;break;case '^':return 4;break;case '(':case ')':case '#':return 1;break;}}//check whether the symbol is operator?int isOperator(char symbol) {switch(symbol) {case '+':case '-':case '*':case '/':case '^':case '(':case ')':return 1;break;
Data Structures & Algorithms89default:return 0;}}//converts infix expression to postfixvoid convert(char infix[],char postfix[]) {int i,symbol,j = 0;stack[++top] = '#';for(i = 0;i<strlen(infix);i++) {symbol = infix[i];if(isOperator(symbol) == 0) {postfix[j] = symbol;j++;} else {if(symbol == '(') {push(symbol);}else {if(symbol == ')') {while(stack[top] != '(') {postfix[j] = pop();j++;}pop();//pop out (.} else {if(precedence(symbol)>precedence(stack[top])) {push(symbol);}else {while(precedence(symbol)<=precedence(stack[top])) {postfix[j] = pop();j++;}
Data Structures & Algorithms90push(symbol);}}}}}while(stack[top] != '#') {postfix[j] = pop();j++;}postfix[j]='0';//null terminate string.}//int stackint stack_int[25];int top_int = -1;void push_int(int item) {stack_int[++top_int] = item;}char pop_int() {return stack_int[top_int--];}//evaluates postfix expressionint evaluate(char *postfix){char ch;int i = 0,operand1,operand2;while( (ch = postfix[i++]) != '0') {if(isdigit(ch)) {
Data Structures & Algorithms91push_int(ch-'0'); // Push the operand}else {//Operator,pop two operandsoperand2 = pop_int();operand1 = pop_int();switch(ch) {case '+':push_int(operand1+operand2);break;case '-':push_int(operand1-operand2);break;case '*':push_int(operand1*operand2);break;case '/':push_int(operand1/operand2);break;}}}return stack_int[top_int];}void main() {char infix[25] = "1*(2+3)",postfix[25];convert(infix,postfix);printf("Infix expression is: %sn" , infix);printf("Postfix expression is: %sn" , postfix);printf("Evaluated expression is: %dn" , evaluate(postfix));}
Data Structures & Algorithms92If we compile and run the above program, it will produce the following result −Infix expression is: 1*(2+3)Postfix expression is: 123+*Result is: 5
Data Structures & Algorithms93Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queueis open at both its ends. One end is always used to insert data (enqueue) and the other isused to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., thedata item stored first will be accessed first.A real-world example of queue can be a single-lane one-way road, where the vehicle entersfirst, exits first. More real-world examples can be seen as queues at the ticket windowsand bus-stops.QueueRepresentationAs we now understand that in queue, we access both ends for different reasons. Thefollowing diagram given below tries to explain queue representation as data structure −As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers andStructures. For the sake of simplicity, we shall implement queues using one-dimensionalarray.BasicOperationsQueue operations may involve initializing or defining the queue, utilizing it, and thencompletely erasing it from the memory. Here we shall try to understand the basicoperations associated with queues − enqueue() − add (store) an item to the queue. dequeue() − remove (access) an item from the queue.15. Queue
Data Structures & Algorithms94Few more functions are required to make the above-mentioned queue operation efficient.These are − peek() − Gets the element at the front of the queue without removing it. isfull() − Checks if the queue is full. isempty() − Checks if the queue is empty.In queue, we always dequeue (or access) data, pointed by front pointer and whileenqueing (or storing) data in the queue we take help of rear pointer.Let's first learn about supportive functions of a queue −peek()This function helps to see the data at the front of the queue. The algorithm of peek()function is as follows −begin procedure peekreturn queue[front]end procedureImplementation of peek() function in C programming language −int peek() {return queue[front];}isfull()As we are using single dimension array to implement queue, we just check for the rearpointer to reach at MAXSIZE to determine that the queue is full. In case we maintain thequeue in a circular linked-list, the algorithm will differ. Algorithm of isfull() function −begin procedure isfullif rear equals to MAXSIZEreturn trueelse
Data Structures & Algorithms95return falseendifend procedureImplementation of isfull() function in C programming language −bool isfull() {if(rear == MAXSIZE - 1)return true;elsereturn false;}isempty()Algorithm of isempty() function −begin procedure isemptyif front is less than MIN OR front is greater than rearreturn trueelsereturn falseendifend procedureIf the value of front is less than MIN or 0, it tells that the queue is not yet initialized,hence empty.Here's the C programming code −bool isempty() {if(front < 0 || front > rear)return true;elsereturn false;}
Data Structures & Algorithms96EnqueueOperationQueues maintain two data pointers, front and rear. Therefore, its operations arecomparatively difficult to implement than that of stacks.The following steps should be taken to enqueue (insert) data into a queue − Step 1 − Check if the queue is full. Step 2 − If the queue is full, produce overflow error and exit. Step 3 − If the queue is not full, increment rear pointer to point the next emptyspace. Step 4 − Add data element to the queue location, where the rear is pointing. Step 5 − Return success.Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseensituations.
Data Structures & Algorithms97Algorithm for enqueue Operationprocedure enqueue(data)if queue is fullreturn overflowendifrear ← rear + 1queue[rear] ← datareturn trueend procedureImplementation of enqueue() in C programming language −int enqueue(int data)if(isfull())return 0;rear = rear + 1;queue[rear] = data;return 1;end procedureDequeueOperationAccessing data from the queue is a process of two tasks − access the data where front ispointing and remove the data after access. The following steps are taken toperform dequeue operation − Step 1 − Check if the queue is empty. Step 2 − If the queue is empty, produce underflow error and exit. Step 3 − If the queue is not empty, access the data where front is pointing. Step 4 − Increment front pointer to point to the next available data element. Step 5 − Return success.
Data Structures & Algorithms98Algorithm for dequeue Operationprocedure dequeueif queue is emptyreturn underflowend ifdata = queue[front]front ← front + 1return trueend procedureImplementation of dequeue() in C programming language −int dequeue() {if(isempty())return 0;int data = queue[front];front = front + 1;return data;}For a complete Queue program in C programming language, please click here.
Data Structures & Algorithms99QueuePrograminCWe shall see the stack implementation in C programming language here. You can try theprogram by clicking on the Try-it button. To learn the theory aspect of stacks, click on visitprevious page.Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>#define MAX 6int intArray[MAX];int front = 0;int rear = -1;int itemCount = 0;int peek(){return intArray[front];}bool isEmpty(){return itemCount == 0;}bool isFull(){return itemCount == MAX;}int size(){return itemCount;}void insert(int data){if(!isFull()){
Data Structures & Algorithms100if(rear == MAX-1){rear = -1;}intArray[++rear] = data;itemCount++;}}int removeData(){int data = intArray[front++];if(front == MAX){front = 0;}itemCount--;return data;}int main() {/* insert 5 items */insert(3);insert(5);insert(9);insert(1);insert(12);// front : 0// rear : 4// ------------------// index : 0 1 2 3 4// ------------------// queue : 3 5 9 1 12insert(15);// front : 0// rear : 5
Data Structures & Algorithms101// ---------------------// index : 0 1 2 3 4 5// ---------------------// queue : 3 5 9 1 12 15if(isFull()){printf("Queue is full!n");}// remove one itemint num = removeData();printf("Element removed: %dn",num);// front : 1// rear : 5// -------------------// index : 1 2 3 4 5// -------------------// queue : 5 9 1 12 15// insert more itemsinsert(16);// front : 1// rear : -1// ----------------------// index : 0 1 2 3 4 5// ----------------------// queue : 16 5 9 1 12 15// As queue is full, elements will not be inserted.insert(17);insert(18);// ----------------------// index : 0 1 2 3 4 5// ----------------------
Data Structures & Algorithms102// queue : 16 5 9 1 12 15printf("Element at front: %dn",peek());printf("----------------------n");printf("index : 5 4 3 2 1 0n");printf("----------------------n");printf("Queue: ");while(!isEmpty()){int n = removeData();printf("%d ",n);}}If we compile and run the above program, it will produce the following result −Queue is full!Element removed: 3Element at front: 5----------------------index : 5 4 3 2 1 0----------------------Queue: 5 9 1 12 15 16
Data Structures & Algorithms103Searching Techniques
Data Structures & Algorithms104Linear search is a very simple search algorithm. In this type of search, a sequential searchis made over all items one by one. Every item is checked and if a match is found then thatparticular item is returned, otherwise the search continues till the end of the datacollection.AlgorithmLinear Search ( Array A, Value x)Step 1: Set i to 1Step 2: if i > n then go to step 7Step 3: if A[i] = x then go to step 6Step 4: Set i to i + 1Step 5: Go to Step 2Step 6: Print Element x Found at index i and go to step 8Step 7: Print element not foundStep 8: ExitPseudocodeprocedure linear_search (list, value)for each item in the listif match item == valuereturn the item's location16. Linear Search
Data Structures & Algorithms105end ifend forend procedureTo know about linear search implementation in C programming language, please click-here.LinearSearchPrograminCHere we present the implementation of linear search in C programming language. Theoutput of the program is given after the code.Linear Search Program#include <stdio.h>#define MAX 20// array of items on which linear search will be conducted.int intArray[MAX] = {1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}printf("=n");}// this method makes a linear search.int find(int data){int comparisons = 0;int index = -1;int i;
Data Structures & Algorithms106// navigate through all itemsfor(i = 0;i<MAX;i++){// count the comparisons madecomparisons++;// if data found, break the loopif(data == intArray[i]){index = i;break;}}printf("Total comparisons made: %d", comparisons);return index;}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}printf("]n");}main(){printf("Input Array: ");display();printline(50);
Data Structures & Algorithms107//find location of 1int location = find(55);// if element was foundif(location != -1)printf("nElement found at location: %d" ,(location+1));elseprintf("Element not found.");}If we compile and run the above program, it will produce the following result −Input Array: [1 2 3 4 6 7 9 11 12 14 15 16 17 19 33 34 43 45 55 66 ]==================================================Total comparisons made: 19Element found at location: 19
Data Structures & Algorithms108Binary search is a fast search algorithm with run-time complexity of Ο(log n). This searchalgorithm works on the principle of divide and conquer. For this algorithm to work properly,the data collection should be in the sorted form.Binary search looks for a particular item by comparing the middle most item of thecollection. If a match occurs, then the index of item is returned. If the middle item isgreater than the item, then the item is searched in the sub-array to the right of the middleitem. Otherwise, the item is searched for in the sub-array to the left of the middle item.This process continues on the sub-array as well until the size of the subarray reduces tozero.HowBinarySearchWorks?For a binary search to work, it is mandatory for the target array to be sorted. We shalllearn the process of binary search with a pictorial example. The following is our sortedarray and let us assume that we need to search the location of value 31 using binarysearch.First, we shall determine half of the array by using this formula −mid = low + (high - low) / 2Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.Now we compare the value stored at location 4, with the value being searched, i.e. 31.We find that the value at location 4 is 27, which is not a match. As the value is greaterthan 27 and we have a sorted array, so we also know that the target value must be in theupper portion of the array.17. Binary Search
Data Structures & Algorithms109We change our low to mid + 1 and find the new mid value again.low = mid + 1mid = low + (high - low) / 2Our new mid is 7 now. We compare the value stored at location 7 with our target value31.The value stored at location 7 is not a match, rather it is less than what we are lookingfor. So, the value must be in the lower part from this location.Hence, we calculate the mid again. This time it is 5.We compare the value stored at location 5 with our target value. We find that it is a match.We conclude that the target value 31 is stored at location 5.Binary search halves the searchable items and thus reduces the count of comparisons tobe made to very less numbers.
Data Structures & Algorithms110PseudocodeThe pseudocode of binary search algorithms should look like this −Procedure binary_searchA ← sorted arrayn ← size of arrayx ← value ot be searchedSet lowerBound = 1Set upperBound = nwhile x not foundif upperBound < lowerBoundEXIT: x does not exists.set midPoint = lowerBound + ( upperBound - lowerBound ) / 2if A[midPoint] < xset lowerBound = midPoint + 1if A[midPoint] > xset upperBound = midPoint - 1if A[midPoint] = xEXIT: x found at location midPointend whileend procedureTo know about binary search implementation using array in C programming language,please click here.
Data Structures & Algorithms111BinarySearchPrograminCBinary search is a fast search algorithm with run-time complexity of Ο(log n). This searchalgorithm works on the principle of divide and conquer. For this algorithm to work properly,the data collection should be in a sorted form.Implementation in C#include <stdio.h>#define MAX 20// array of items on which linear search will be conducted.int intArray[MAX] = {1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}printf("=n");}int find(int data){int lowerBound = 0;int upperBound = MAX -1;int midPoint = -1;int comparisons = 0;int index = -1;while(lowerBound <= upperBound){printf("Comparison %dn" , (comparisons +1) ) ;printf("lowerBound : %d, intArray[%d] = %dn",lowerBound,lowerBound,intArray[lowerBound]);printf("upperBound : %d, intArray[%d] = %dn",upperBound,upperBound,intArray[upperBound]);
Data Structures & Algorithms112comparisons++;// compute the mid point// midPoint = (lowerBound + upperBound) / 2;midPoint = lowerBound + (upperBound - lowerBound) / 2;// data foundif(intArray[midPoint] == data){index = midPoint;break;}else {// if data is largerif(intArray[midPoint] < data){// data is in upper halflowerBound = midPoint + 1;}// data is smallerelse{// data is in lower halfupperBound = midPoint -1;}}}printf("Total comparisons made: %d" , comparisons);return index;}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}
Data Structures & Algorithms113printf("]n");}main(){printf("Input Array: ");display();printline(50);//find location of 1int location = find(55);// if element was foundif(location != -1)printf("nElement found at location: %d" ,(location+1));elseprintf("nElement not found.");}If we compile and run the above program, it will produce the following result −Input Array: [1 2 3 4 6 7 9 11 12 14 15 16 17 19 33 34 43 45 55 66 ]==================================================Comparison 1lowerBound : 0, intArray[0] = 1upperBound : 19, intArray[19] = 66Comparison 2lowerBound : 10, intArray[10] = 15upperBound : 19, intArray[19] = 66Comparison 3lowerBound : 15, intArray[15] = 34upperBound : 19, intArray[19] = 66Comparison 4lowerBound : 18, intArray[18] = 55upperBound : 19, intArray[19] = 66Total comparisons made: 4Element found at location: 19
Data Structures & Algorithms114
Data Structures & Algorithms115Interpolation search is an improved variant of binary search. This search algorithm workson the probing position of the required value. For this algorithm to work properly, the datacollection should be in a sorted form and equally distributed.Binary search has a huge advantage of time complexity over linear search. Linear searchhas worst-case complexity of Ο(n) whereas binary search has Ο(log n).There are cases where the location of target data may be known in advance. For example,in case of a telephone directory, if we want to search the telephone number of Morphius.Here, linear search and even binary search will seem slow as we can directly jump tomemory space where the names start from 'M' are stored.PositioninginBinarySearchIn binary search, if the desired data is not found then the rest of the list is divided in twoparts, lower and higher. The search is carried out in either of them.Even when the data is sorted, binary search does not take advantage to probe the positionof the desired data.18. Interpolation Search
Data Structures & Algorithms116PositionProbinginInterpolationSearchInterpolation search finds a particular item by computing the probe position. Initially, theprobe position is the position of the middle most item of the collection.If a match occurs, then the index of the item is returned. To split the list into two parts,we use the following method −mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo])where −A = listLo = Lowest index of the listHi = Highest index of the listA[n] = Value stored at index n in the listIf the middle item is greater than the item, then the probe position is again calculated inthe sub-array to the right of the middle item. Otherwise, the item is searched in the sub-array to the left of the middle item. This process continues on the sub-array as well untilthe size of subarray reduces to zero.Runtime complexity of interpolation search algorithm is Ο(log (log n)) as comparedto Ο(log n) of BST in favorable situations.AlgorithmAs it is an improvisation of the existing BST algorithm, we are mentioning the steps tosearch the 'target' data value index, using position probing −Step 1 − Start searching data from middle of the list.Step 2 − If it is a match, return the index of the item, and exit.Step 3 − If it is not a match, probe position.Step 4 − Divide the list using probing formula and find the new middle.Step 5 − If data is greater than middle, search in higher sub-list.Step 6 − If data is smaller than middle, search in lower sub-list.Step 7 − Repeat until match.
Data Structures & Algorithms117PseudocodeA → Array listN → Size of AX → Target ValueProcedure Interpolation_Search()Set Lo → 0Set Mid → -1Set Hi → N-1While X does not matchif Lo equals to Hi OR A[Lo] equals to A[Hi]EXIT: Failure, Target not foundend ifSet Mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo])if A[Mid] = XEXIT: Success, Target found at Midelseif A[Mid] < XSet Lo to Mid+1else if A[Mid] > XSet Hi to Mid-1end ifend ifEnd WhileEnd ProcedureTo know about the implementation of interpolation search in C programminglanguage, click here.
Data Structures & Algorithms118InterpolationSearchPrograminCInterpolation search is an improved variant of binary search. This search algorithm workson the probing position of the required value. For this algorithm to work properly, the datacollection should be in sorted and equally distributed form.It's runtime complexity is log2(log2 n).Implementation in C#include<stdio.h>#define MAX 10// array of items on which linear search will be conducted.int list[MAX] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44 };int find(int data) {int lo = 0;int hi = MAX - 1;int mid = -1;int comparisons = 1;int index = -1;while(lo <= hi) {printf("nComparison %d n" , comparisons ) ;printf("lo : %d, list[%d] = %dn", lo, lo, list[lo]);printf("hi : %d, list[%d] = %dn", hi, hi, list[hi]);comparisons++;// probe the mid pointmid = lo + (((double)(hi - lo) / (list[hi] - list[lo])) * (data - list[lo]));printf("mid = %dn",mid);// data foundif(list[mid] == data) {index = mid;break;}else {
Data Structures & Algorithms119if(list[mid] < data) {// if data is larger, data is in upper halflo = mid + 1;}else {// if data is smaller, data is in lower halfhi = mid - 1;}}}printf("nTotal comparisons made: %d", --comparisons);return index;}int main() {//find location of 33int location = find(33);// if element was foundif(location != -1)printf("nElement found at location: %d" ,(location+1));elseprintf("Element not found.");return 0;}If we compile and run the above program, it will produce the following result −Comparison 1lo : 0, list[0] = 10hi : 9, list[9] = 44mid = 6Total comparisons made: 1Element found at location: 7You can change the search value and execute the program to test it.
Data Structures & Algorithms120Hash Table is a data structure which stores data in an associative manner. In a hash table,data is stored in an array format, where each data value has its own unique index value.Access of data becomes very fast if we know the index of the desired data.Thus, it becomes a data structure in which insertion and search operations are very fastirrespective of the size of the data. Hash Table uses an array as a storage medium anduses hash technique to generate an index where an element is to be inserted or is to belocated from.HashingHashing is a technique to convert a range of key values into a range of indexes of an array.We're going to use modulo operator to get a range of key values. Consider an example ofhash table of size 20, and the following items are to be stored. Item are in the (key,value)format. (1,20) (2,70) (42,80) (4,25) (12,44) (14,32) (17,11) (13,78) (37,98)19. Hash Table
Data Structures & Algorithms121Sr. No. Key Hash Array Index1 1 1 % 20 = 1 12 2 2 % 20 = 2 23 42 42 % 20 = 2 24 4 4 % 20 = 4 45 12 12 % 20 = 12 126 14 14 % 20 = 14 147 17 17 % 20 = 17 178 13 13 % 20 = 13 139 37 37 % 20 = 17 17LinearProbingAs we can see, it may happen that the hashing technique is used to create an already usedindex of the array. In such a case, we can search the next empty location in the array bylooking into the next cell until we find an empty cell. This technique is called linear probing.Sr. No. Key Hash Array IndexAfter LinearProbing,Array Index1 1 1 % 20 = 1 1 12 2 2 % 20 = 2 2 23 42 42 % 20 = 2 2 34 4 4 % 20 = 4 4 45 12 12 % 20 = 12 12 126 14 14 % 20 = 14 14 14
Data Structures & Algorithms1227 17 17 % 20 = 17 17 178 13 13 % 20 = 13 13 139 37 37 % 20 = 17 17 18BasicOperationsFollowing are the basic primary operations of a hash table. Search − Searches an element in a hash table. Insert − inserts an element in a hash table. Delete − Deletes an element from a hash table.DataItemDefine a data item having some data and key, based on which the search is to beconducted in a hash table.struct DataItem {int data;int key;};HashMethodDefine a hashing method to compute the hash code of the key of the data item.int hashCode(int key){return key % SIZE;}SearchOperationWhenever an element is to be searched, compute the hash code of the key passed andlocate the element using that hash code as index in the array. Use linear probing to getthe element ahead if the element is not found at the computed hash code.
Data Structures & Algorithms123struct DataItem *search(int key){//get the hashint hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] != NULL){if(hashArray[hashIndex]->key == key)return hashArray[hashIndex];//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}return NULL;}InsertOperationWhenever an element is to be inserted, compute the hash code of the key passed andlocate the index using that hash code as an index in the array. Use linear probing forempty location, if an element is found at the computed hash code.void insert(int key,int data){struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));item->data = data;item->key = key;//get the hashint hashIndex = hashCode(key);//move in array until an empty or deleted cellwhile(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1){//go to next cell++hashIndex;
Data Structures & Algorithms124//wrap around the tablehashIndex %= SIZE;}hashArray[hashIndex] = item;}DeleteOperationWhenever an element is to be deleted, compute the hash code of the key passed andlocate the index using that hash code as an index in the array. Use linear probing to getthe element ahead if an element is not found at the computed hash code. When found,store a dummy item there to keep the performance of the hash table intact.struct DataItem* delete(struct DataItem* item){int key = item->key;//get the hashint hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] !=NULL){if(hashArray[hashIndex]->key == key){struct DataItem* temp = hashArray[hashIndex];//assign a dummy item at deleted positionhashArray[hashIndex] = dummyItem;return temp;}//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}return NULL;}
Data Structures & Algorithms125To know about hash implementation in C programming language, please click here.HashTablePrograminCHash Table is a data structure which stores data in an associative manner. In hash table,the data is stored in an array format where each data value has its own unique indexvalue. Access of data becomes very fast, if we know the index of the desired data.Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>#define SIZE 20struct DataItem {int data;int key;};struct DataItem* hashArray[SIZE];struct DataItem* dummyItem;struct DataItem* item;int hashCode(int key){return key % SIZE;}struct DataItem *search(int key){//get the hashint hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] != NULL){if(hashArray[hashIndex]->key == key)return hashArray[hashIndex];
Data Structures & Algorithms126//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}return NULL;}void insert(int key,int data){struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));item->data = data;item->key = key;//get the hashint hashIndex = hashCode(key);//move in array until an empty or deleted cellwhile(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1){//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}hashArray[hashIndex] = item;}struct DataItem* delete(struct DataItem* item){int key = item->key;//get the hash
Data Structures & Algorithms127int hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] != NULL){if(hashArray[hashIndex]->key == key){struct DataItem* temp = hashArray[hashIndex];//assign a dummy item at deleted positionhashArray[hashIndex] = dummyItem;return temp;}//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}return NULL;}void display(){int i = 0;for(i = 0; i<SIZE; i++) {if(hashArray[i] != NULL)printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);elseprintf(" ~~ ");}printf("n");}
Data Structures & Algorithms128int main(){dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));dummyItem->data = -1;dummyItem->key = -1;insert(1, 20);insert(2, 70);insert(42, 80);insert(4, 25);insert(12, 44);insert(14, 32);insert(17, 11);insert(13, 78);insert(37, 97);display();item = search(37);if(item != NULL){printf("Element found: %dn", item->data);}else {printf("Element not foundn");}delete(item);item = search(37);if(item != NULL){printf("Element found: %dn", item->data);}else {printf("Element not foundn");}}
Data Structures & Algorithms129If we compile and run the above program, it will produce the following result −~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~ (12,44)(13,78) (14,32) ~~ ~~ (17,11) (37,97) ~~Element found: 97Element not found
Data Structures & Algorithms130Sorting Techniques
Data Structures & Algorithms131Sorting refers to arranging data in a particular format. Sorting algorithm specifies the wayto arrange data in a particular order. Most common orders are in numerical orlexicographical order.The importance of sorting lies in the fact that data searching can be optimized to a veryhigh level, if data is stored in a sorted manner. Sorting is also used to represent data inmore readable formats. Following are some of the examples of sorting in real-lifescenarios: Telephone Directory – The telephone directory stores the telephone numbers ofpeople sorted by their names, so that the names can be searched easily. Dictionary – The dictionary stores words in an alphabetical order so thatsearching of any word becomes easy.In-placeSortingandNot-in-placeSortingSorting algorithms may require some extra space for comparison and temporary storageof few data elements. These algorithms do not require any extra space and sorting is saidto happen in-place, or for example, within the array itself. This is called in-place sorting.Bubble sort is an example of in-place sorting.However, in some sorting algorithms, the program requires space which is more than orequal to the elements being sorted. Sorting which uses equal or more space is called not-in-place sorting. Merge-sort is an example of not-in-place sorting.StableandNotStableSortingIf a sorting algorithm, after sorting the contents, does not change the sequence of similarcontent in which they appear, it is called stable sorting.20. Sorting Algorithm
Data Structures & Algorithms132If a sorting algorithm, after sorting the contents, changes the sequence of similar contentin which they appear, it is called unstable sorting.Stability of an algorithm matters when we wish to maintain the sequence of originalelements, like in a tuple for example.AdaptiveandNon-AdaptiveSortingAlgorithmA sorting algorithm is said to be adaptive, if it takes advantage of already 'sorted' elementsin the list that is to be sorted. That is, while sorting if the source list has some elementalready sorted, adaptive algorithms will take this into account and will try not to re-orderthem.A non-adaptive algorithm is one which does not take into account the elements which arealready sorted. They try to force every single element to be re-ordered to confirm theirsortedness.ImportantTermsSome terms are generally coined while discussing sorting techniques, here is a briefintroduction to them −Increasing OrderA sequence of values is said to be in increasing order, if the successive element is greaterthan the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every nextelement is greater than the previous element.Decreasing OrderA sequence of values is said to be in decreasing order, if the successive element is lessthan the current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every nextelement is less than the previous element.
Data Structures & Algorithms133Non-Increasing OrderA sequence of values is said to be in non-increasing order, if the successive element isless than or equal to its previous element in the sequence. This order occurs when thesequence contains duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasingorder, as every next element is less than or equal to (in case of 3) but not greater thanany previous element.Non-Decreasing OrderA sequence of values is said to be in non-decreasing order, if the successive element isgreater than or equal to its previous element in the sequence. This order occurs when thesequence contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasingorder, as every next element is greater than or equal to (in case of 3) but not less thanthe previous one.
Data Structures & Algorithms134Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-basedalgorithm in which each pair of adjacent elements is compared and the elements areswapped if they are not in order. This algorithm is not suitable for large data sets as itsaverage and worst case complexity are of O(n2) where n is the number of items.HowBubbleSortWorks?We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keepingit short and precise.Bubble sort starts with very first two elements, comparing them to check which one isgreater.In this case, value 33 is greater than 14, so it is already in sorted locations. Next, wecompare 33 with 27.We find that 27 is smaller than 33 and these two values must be swapped.21. Bubble Sort Algorithm
Data Structures & Algorithms135The new array should look like this −Next we compare 33 and 35. We find that both are in already sorted positions.Then we move to the next two values, 35 and 10.We know then that 10 is smaller 35. Hence they are not sorted.We swap these values. We find that we have reached the end of the array. After oneiteration, the array should look like this −
Data Structures & Algorithms136To be precise, we are now showing how an array should look like after each iteration. Afterthe second iteration, it should look like this −Notice that after each iteration, at least one value moves at the end.And when there's no swap required, bubble sorts learns that an array is completely sorted.Now we should look into some practical aspects of bubble sort.
Data Structures & Algorithms137AlgorithmWe assume list is an array of n elements. We further assume that swap function swapsthe values of the given array elements.begin BubbleSort(list)for all elements of listif list[i] > list[i+1]swap(list[i], list[i+1])end ifend forreturn listend BubbleSortPseudocodeWe observe in algorithm that Bubble Sort compares each pair of array element unless thewhole array is completely sorted in an ascending order. This may cause a few complexityissues like what if the array needs no more swapping as all the elements are alreadyascending.To ease-out the issue, we use one flag variable swapped which will help us see if anyswap has happened or not. If no swap has occurred, i.e. the array requires no moreprocessing to be sorted, it will come out of the loop.Pseudocode of BubbleSort algorithm can be written as follows −procedure bubbleSort( list : array of items )loop = list.count;for i = 0 to loop-1 do:swapped = falsefor j = 0 to loop-1 do:/* compare the adjacent elements */if list[j] > list[j+1] then/* swap them */swap( list[j], list[j+1] )
Data Structures & Algorithms138swapped = trueend ifend for/*if no number was swapped that meansarray is sorted now, break the loop.*/if(not swapped) thenbreakend ifend forend procedure return listImplementationOne more issue we did not address in our original algorithm and its improvisedpseudocode, is that, after every iteration the highest values settles down at the end of thearray. Hence, the next iteration need not include already sorted elements. For thispurpose, in our implementation, we restrict the inner loop to avoid already sorted values.To know about bubble sort implementation in C programming language, please click here.BubbleSortPrograminCWe shall see the implementation of bubble sort in C programming language here.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 10int list[MAX] = {1,8,4,6,0,3,5,2,7,9};void display(){int i;printf("[");
Data Structures & Algorithms139// navigate through all itemsfor(i = 0; i < MAX; i++){printf("%d ",list[i]);}printf("]n");}void bubbleSort() {int temp;int i,j;bool swapped = false;// loop through all numbersfor(i = 0; i < MAX-1; i++) {swapped = false;// loop through numbers falling aheadfor(j = 0; j < MAX-1-i; j++) {printf(" Items compared: [ %d, %d ] ", list[j],list[j+1]);// check if next number is lesser than current no// swap the numbers.// (Bubble up the highest number)if(list[j] > list[j+1]) {temp = list[j];list[j] = list[j+1];list[j+1] = temp;swapped = true;printf(" => swapped [%d, %d]n",list[j],list[j+1]);}else {printf(" => not swappedn");}
Data Structures & Algorithms140}// if no number was swapped that means// array is sorted now, break the loop.if(!swapped) {break;}printf("Iteration %d#: ",(i+1));display();}}main(){printf("Input Array: ");display();printf("n");bubbleSort();printf("nOutput Array: ");display();}If we compile and run the above program, it will produce the following result −Input Array: [1 8 4 6 0 3 5 2 7 9 ]Items compared: [ 1, 8 ] => not swappedItems compared: [ 8, 4 ] => swapped [4, 8]Items compared: [ 8, 6 ] => swapped [6, 8]Items compared: [ 8, 0 ] => swapped [0, 8]Items compared: [ 8, 3 ] => swapped [3, 8]Items compared: [ 8, 5 ] => swapped [5, 8]Items compared: [ 8, 2 ] => swapped [2, 8]Items compared: [ 8, 7 ] => swapped [7, 8]Items compared: [ 8, 9 ] => not swapped
Data Structures & Algorithms141Iteration 1#: [1 4 6 0 3 5 2 7 8 9 ]Items compared: [ 1, 4 ] => not swappedItems compared: [ 4, 6 ] => not swappedItems compared: [ 6, 0 ] => swapped [0, 6]Items compared: [ 6, 3 ] => swapped [3, 6]Items compared: [ 6, 5 ] => swapped [5, 6]Items compared: [ 6, 2 ] => swapped [2, 6]Items compared: [ 6, 7 ] => not swappedItems compared: [ 7, 8 ] => not swappedIteration 2#: [1 4 0 3 5 2 6 7 8 9 ]Items compared: [ 1, 4 ] => not swappedItems compared: [ 4, 0 ] => swapped [0, 4]Items compared: [ 4, 3 ] => swapped [3, 4]Items compared: [ 4, 5 ] => not swappedItems compared: [ 5, 2 ] => swapped [2, 5]Items compared: [ 5, 6 ] => not swappedItems compared: [ 6, 7 ] => not swappedIteration 3#: [1 0 3 4 2 5 6 7 8 9 ]Items compared: [ 1, 0 ] => swapped [0, 1]Items compared: [ 1, 3 ] => not swappedItems compared: [ 3, 4 ] => not swappedItems compared: [ 4, 2 ] => swapped [2, 4]Items compared: [ 4, 5 ] => not swappedItems compared: [ 5, 6 ] => not swappedIteration 4#: [0 1 3 2 4 5 6 7 8 9 ]Items compared: [ 0, 1 ] => not swappedItems compared: [ 1, 3 ] => not swappedItems compared: [ 3, 2 ] => swapped [2, 3]Items compared: [ 3, 4 ] => not swappedItems compared: [ 4, 5 ] => not swappedIteration 5#: [0 1 2 3 4 5 6 7 8 9 ]Items compared: [ 0, 1 ] => not swappedItems compared: [ 1, 2 ] => not swappedItems compared: [ 2, 3 ] => not swappedItems compared: [ 3, 4 ] => not swappedOutput Array: [0 1 2 3 4 5 6 7 8 9 ]
Data Structures & Algorithms142This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintainedwhich is always sorted. For example, the lower part of an array is maintained to be sorted.An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate placeand then it has to be inserted there. Hence the name, insertion sort.The array is searched sequentially and unsorted items are moved and inserted into thesorted sub-list (in the same array). This algorithm is not suitable for large data sets as itsaverage and worst case complexity are of Ο(n2), where n is the number of items.HowInsertionSortWorks?We take an unsorted array for our example.Insertion sort compares the first two elements.It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.Insertion sort moves ahead and compares 33 with 27.And finds that 33 is not in the correct position.22. Insertion Sort
Data Structures & Algorithms143It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we seethat the sorted sub-list has only one element 14, and 27 is greater than 14. Hence, thesorted sub-list remains sorted after swapping.By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.These values are not in a sorted order.So we swap them.However, swapping makes 27 and 10 unsorted.Hence, we swap them too.Again we find 14 and 10 in an unsorted order.
Data Structures & Algorithms144We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.This process goes on until all the unsorted values are covered in a sorted sub-list. Now weshall see some programming aspects of insertion sort.AlgorithmNow we have a bigger picture of how this sorting technique works, so we can derive simplesteps by which we can achieve insertion sort.Step 1 − If it is the first element, it is already sorted. return 1;Step 2 − Pick next elementStep 3 − Compare with all elements in the sorted sub-listStep 4 − Shift all the elements in the sorted sub-list that is greater than thevalue to be sortedStep 5 − Insert the valueStep 6 − Repeat until list is sortedPseudocodeprocedure insertionSort( A : array of items )int holePositionint valueToInsertfor i = 1 to length(A) inclusive do:/* select value to be inserted */valueToInsert = A[i]holePosition = i
Data Structures & Algorithms145/*locate hole position for the element to be inserted */while holePosition > 0 and A[holePosition-1] > valueToInsert do:A[holePosition] = A[holePosition-1]holePosition = holePosition -1end while/* insert the number at hole position */A[holePosition] = valueToInsertend forend procedureTo know about insertion sort implementation in C programming language, please clickhere.InsertionSortPrograminCThis is an in-place comparison-based sorting algorithm. Here, a sub-list is maintainedwhich is always sorted. For example, the lower part of an array is maintained to be sorted.An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate placeand then it is to be inserted there. Hence the name insertion sort.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}
Data Structures & Algorithms146printf("=n");}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}printf("]n");}void insertionSort(){int valueToInsert;int holePosition;int i;// loop through all numbersfor(i = 1; i < MAX; i++){// select a value to be inserted.valueToInsert = intArray[i];// select the hole position where number is to be insertedholePosition = i;// check if previous no. is larger than value to be insertedwhile (holePosition > 0 && intArray[holePosition-1] > valueToInsert){intArray[holePosition] = intArray[holePosition-1];holePosition--;printf(" item moved : %dn" , intArray[holePosition]);}
Data Structures & Algorithms147if(holePosition != i){printf(" item inserted : %d, at position : %dn" ,valueToInsert,holePosition);// insert the number at hole positionintArray[holePosition] = valueToInsert;}printf("Iteration %d#:",i);display();}}main(){printf("Input Array: ");display();printline(50);insertionSort();printf("Output Array: ");display();printline(50);}If we compile and run the above program, it will produce the following result −Input Array: [4 6 3 2 1 9 7 ]==================================================Iteration 1#:[4 6 3 2 1 9 7 ]item moved : 6item moved : 4item inserted : 3, at position : 0Iteration 2#:[3 4 6 2 1 9 7 ]item moved : 6item moved : 4item moved : 3item inserted : 2, at position : 0Iteration 3#:[2 3 4 6 1 9 7 ]item moved : 6
Data Structures & Algorithms148item moved : 4item moved : 3item moved : 2item inserted : 1, at position : 0Iteration 4#:[1 2 3 4 6 9 7 ]Iteration 5#:[1 2 3 4 6 9 7 ]item moved : 9item inserted : 7, at position : 5Iteration 6#:[1 2 3 4 6 7 9 ]Output Array: [1 2 3 4 6 7 9 ]==================================================
Data Structures & Algorithms149Selection sort is a simple sorting algorithm. This sorting algorithm is an in-placecomparison-based algorithm in which the list is divided into two parts, the sorted part atthe left end and the unsorted part at the right end. Initially, the sorted part is empty andthe unsorted part is the entire list.The smallest element is selected from the unsorted array and swapped with the leftmostelement, and that element becomes a part of the sorted array. This process continuesmoving unsorted array boundary by one element to the right.This algorithm is not suitable for large data sets as its average and worst case complexitiesare of O(n2), where n is the number of items.HowSelectionSortWorks?Consider the following depicted array as an example.For the first position in the sorted list, the whole list is scanned sequentially. The firstposition where 14 is stored presently, we search the whole list and find that 10 is thelowest value.So we replace 14 with 10. After one iteration 10, which happens to be the minimum valuein the list, appears in the first position of the sorted list.For the second position, where 33 is residing, we start scanning the rest of the list in alinear manner.We find that 14 is the second lowest value in the list and it should appear at the secondplace. We swap these values.23. Selection Sort
Data Structures & Algorithms150After two iterations, two least values are positioned at the beginning in a sorted manner.The same process is applied to the rest of the items in the array.Following is a pictorial depiction of the entire sorting process −
Data Structures & Algorithms151Now, let us learn some programming aspects of selection sort.AlgorithmStep 1 − Set MIN to location 0Step 2 − Search the minimum element in the listStep 3 − Swap with value at location MINStep 4 − Increment MIN to point to next elementStep 5 − Repeat until list is sortedPseudocodeprocedure selection sortlist : array of itemsn : size of listfor i = 1 to n - 1/* set current element as minimum*/min = i/* check the element to be minimum */for j = i+1 to nif list[j] < list[min] thenmin = j;end ifend for/* swap the minimum element with the current element*/if indexMin != i thenswap list[min] and list[i]end ifend forend procedureTo know about selection sort implementation in C programming language, please clickhere.
Data Structures & Algorithms152SelectionSortPrograminCSelection sort is a simple sorting algorithm. This sorting algorithm is an in-placecomparison-based algorithm in which the list is divided into two parts, the sorted part atthe left end and the unsorted part at the right end. Initially, the sorted part is empty andthe unsorted part is the entire list.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}printf("=n");}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ", intArray[i]);}
Data Structures & Algorithms153printf("]n");}void selectionSort(){int indexMin,i,j;// loop through all numbersfor(i = 0; i < MAX-1; i++){// set current element as minimumindexMin = i;// check the element to be minimumfor(j = i+1;j<MAX;j++){if(intArray[j] < intArray[indexMin]){indexMin = j;}}if(indexMin != i){printf("Items swapped: [ %d, %d ]n" , intArray[i],intArray[indexMin]);// swap the numbersint temp = intArray[indexMin];intArray[indexMin] = intArray[i];intArray[i] = temp;}printf("Iteration %d#:",(i+1));display();}}
Data Structures & Algorithms154main(){printf("Input Array: ");display();printline(50);selectionSort();printf("Output Array: ");display();printline(50);}If we compile and run the above program, it will produce the following result −Input Array: [4 6 3 2 1 9 7 ]==================================================Items swapped: [ 4, 1 ]Iteration 1#:[1 6 3 2 4 9 7 ]Items swapped: [ 6, 2 ]Iteration 2#:[1 2 3 6 4 9 7 ]Iteration 3#:[1 2 3 6 4 9 7 ]Items swapped: [ 6, 4 ]Iteration 4#:[1 2 3 4 6 9 7 ]Iteration 5#:[1 2 3 4 6 9 7 ]Items swapped: [ 9, 7 ]Iteration 6#:[1 2 3 4 6 7 9 ]Output Array: [1 2 3 4 6 7 9 ]==================================================
Data Structures & Algorithms155Merge sort is a sorting technique based on divide and conquer technique. With worst-casetime complexity being Ο(n log n), it is one of the most respected algorithms.Merge sort first divides the array into equal halves and then combines them in a sortedmanner.HowMergeSortWorks?To understand merge sort, we take an unsorted array as the following −We know that merge sort first divides the whole array iteratively into equal halves unlessthe atomic values are achieved. We see here that an array of 8 items is divided into twoarrays of size 4.This does not change the sequence of appearance of items in the original. Now we dividethese two arrays into halves.We further divide these arrays and we achieve atomic value which can no more be divided.Now, we combine them in exactly the same manner as they were broken down. Pleasenote the color codes given to these lists.We first compare the element for each list and then combine them into another list in asorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 andin the target list of 2 values we put 10 first, followed by 27. We change the order of 19and 35 whereas 42 and 44 are placed sequentially.24. Merge Sort Algorithm
Data Structures & Algorithms156In the next iteration of the combining phase, we compare lists of two data values, andmerge them into a list of found data values placing all in a sorted order.After the final merging, the list should look like this −Now we should learn some programming aspects of merge sorting.AlgorithmMerge sort keeps on dividing the list into equal halves until it can no more be divided. Bydefinition, if it is only one element in the list, it is sorted. Then, merge sort combines thesmaller sorted lists keeping the new list sorted too.Step 1 − if it is only one element in the list it is already sorted, return.Step 2 − divide the list recursively into two halves until it can no more bedivided.Step 3 − merge the smaller lists into new list in sorted order.PseudocodeWe shall now see the pseudocodes for merge sort functions. As our algorithms point outtwo main functions − divide & merge.Merge sort works with recursion and we shall see our implementation in the same way.procedure mergesort( var a as array )if ( n == 1 ) return avar l1 as array = a[0] ... a[n/2]var l2 as array = a[n/2+1] ... a[n]l1 = mergesort( l1 )
Data Structures & Algorithms157l2 = mergesort( l2 )return merge( l1, l2 )end procedureprocedure merge( var a as array, var b as array )var c as arraywhile ( a and b have elements )if ( a[0] > b[0] )add b[0] to the end of cremove b[0] from belseadd a[0] to the end of cremove a[0] from aend ifend whilewhile ( a has elements )add a[0] to the end of cremove a[0] from aend whilewhile ( b has elements )add b[0] to the end of cremove b[0] from bend whilereturn cend procedureTo know about merge sort implementation in C programming language, please click here.
Data Structures & Algorithms158MergeSortPrograminCMerge sort is a sorting technique based on divide and conquer technique. With the worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.Implementation in CWe shall see the implementation of merge sort in C programming language here −#include <stdio.h>#define max 10int a[10] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44 };int b[10];void merging(int low, int mid, int high) {int l1, l2, i;for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {if(a[l1] <= a[l2])b[i] = a[l1++];elseb[i] = a[l2++];}while(l1 <= mid)b[i++] = a[l1++];while(l2 <= high)b[i++] = a[l2++];for(i = low; i <= high; i++)a[i] = b[i];}void sort(int low, int high) {int mid;if(low < high) {
Data Structures & Algorithms159mid = (low + high) / 2;sort(low, mid);sort(mid+1, high);merging(low, mid, high);}else {return;}}int main() {int i;printf("List before sortingn");for(i = 0; i <= max; i++)printf("%d ", a[i]);sort(0, max);printf("nList after sortingn");for(i = 0; i <= max; i++)printf("%d ", a[i]);}If we compile and run the above program, it will produce the following result −List before sorting10 14 19 26 27 31 33 35 42 44 0List after sorting0 10 14 19 26 27 31 33 35 42 44
Data Structures & Algorithms160Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm.This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to thefar right and has to be moved to the far left.This algorithm uses insertion sort on a widely spread elements, first to sort them and thensorts the less widely spaced elements. This spacing is termed as interval. This interval iscalculated based on Knuth's formula as −h = h * 3 + 1where −h is interval with initial value 1This algorithm is quite efficient for medium-sized data sets as its average and worst casecomplexity are of O(n), where n is the number of items.HowShellSortWorks?Let us consider the following example to have an idea of how shell sort works. We takethe same array we have used in our previous examples. For our example and ease ofunderstanding, we take the interval of 4. Make a virtual sub-list of all values located atthe interval of 4 positions. Here these values are {35, 14}, {33, 19}, {42, 27} and {10,14}25. Shell Sort
Data Structures & Algorithms161We compare values in each sub-list and swap them (if necessary) in the original array.After this step, the new array should look like this −Then, we take interval of 2 and this gap generates two sub-lists - {14, 27, 35, 42}, {19,10, 33, 44}We compare and swap the values, if required, in the original array. After this step, thearray should look like this −Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sortto sort the array.
Data Structures & Algorithms162Following is the step-by-step depiction −
Data Structures & Algorithms163We see that it required only four swaps to sort the rest of the array.AlgorithmFollowing is the algorithm for shell sort.Step 1 − Initialize the value of hStep 2 − Divide the list into smaller sub-list of equal interval hStep 3 − Sort these sub-lists using insertion sortStep 3 − Repeat until complete list is sortedPseudocodeFollowing is the pseudocode for shell sort.procedure shellSort()A : array of items/* calculate interval*/while interval < A.length /3 do:interval = interval * 3 + 1end whilewhile interval > 0 do:for outer = interval; outer < A.length; outer ++ do:/* select value to be inserted */valueToInsert = A[outer]inner = outer;/*shift element towards right*/while inner > interval -1 && A[inner - interval] >= valueToInsert do:A[inner] = A[inner - interval]inner = inner - intervalend while
Data Structures & Algorithms164/* insert the number at hole position */A[inner] = valueToInsertend for/* calculate interval*/interval = (interval -1) /3;end whileend procedureTo know about shell sort implementation in C programming language, please click here.ShellSortPrograminCShell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm.This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to thefar right and has to be moved to the far left.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}printf("=n");}
Data Structures & Algorithms165void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}printf("]n");}void shellSort(){int inner, outer;int valueToInsert;int interval = 1;int elements = MAX;int i = 0;while(interval <= elements/3) {interval = interval*3 +1;}while(interval > 0) {printf("iteration %d#:",i);display();for(outer = interval; outer < elements; outer++) {valueToInsert = intArray[outer];inner = outer;while(inner > interval -1 && intArray[inner - interval]>= valueToInsert) {intArray[inner] = intArray[inner - interval];inner -=interval;printf(" item moved :%dn",intArray[inner]);}
Data Structures & Algorithms166intArray[inner] = valueToInsert;printf(" item inserted :%d, at position :%dn",valueToInsert,inner);}interval = (interval -1) /3;i++;}}int main() {printf("Input Array: ");display();printline(50);shellSort();printf("Output Array: ");display();printline(50);return 1;}If we compile and run the above program, it will produce the following result −Input Array: [4 6 3 2 1 9 7 ]==================================================iteration 0#:[4 6 3 2 1 9 7 ]item moved :4item inserted :1, at position :0item inserted :9, at position :5item inserted :7, at position :6iteration 1#:[1 6 3 2 4 9 7 ]item inserted :6, at position :1item moved :6item inserted :3, at position :1item moved :6
Data Structures & Algorithms167item moved :3item inserted :2, at position :1item moved :6item inserted :4, at position :3item inserted :9, at position :5item moved :9item inserted :7, at position :5Output Array: [1 2 3 4 6 7 9 ]==================================================
Data Structures & Algorithms168Quick sort is a highly efficient sorting algorithm and is based on partitioning of array ofdata into smaller arrays. A large array is partitioned into two arrays one of which holdsvalues smaller than the specified value, say pivot, based on which the partition is madeand another array holds values greater than the pivot value.Quick sort partitions an array and then calls itself recursively twice to sort the two resultingsubarrays. This algorithm is quite efficient for large-sized data sets as its average andworst case complexity are of O(nlogn), where n is the number of items.PartitioninQuickSortFollowing animated representation explains how to find the pivot value in an array.The pivot value divides the list into two parts. And recursively, we find the pivot for eachsub-lists until all lists contains only one element.QuickSortPivotAlgorithmBased on our understanding of partitioning in quick sort, we will now try to write analgorithm for it, which is as follows.Step 1 − Choose the highest index value has pivotStep 2 − Take two variables to point left and right of the list excluding pivotStep 3 − left points to the low indexStep 4 − right points to the highStep 5 − while value at left is less than pivot move rightStep 6 − while value at right is greater than pivot move leftStep 7 − if both step 5 and step 6 does not match swap left and rightStep 8 − if left ≥ right, the point where they met is new pivot26. Quick Sort
Data Structures & Algorithms169QuickSortPivotPseudocodeThe pseudocode for the above algorithm can be derived as −function partitionFunc(left, right, pivot)leftPointer = left -1rightPointer = rightwhile True dowhile A[++leftPointer] < pivot do//do-nothingend whilewhile rightPointer > 0 && A[--rightPointer] > pivot do//do-nothingend whileif leftPointer >= rightPointerbreakelseswap leftPointer,rightPointerend ifend whileswap leftPointer,rightreturn leftPointerend functionQuickSortAlgorithmUsing pivot algorithm recursively, we end up with smaller possible partitions. Eachpartition is then processed for quick sort. We define recursive algorithm for quicksort asfollows −Step 1 − Make the right-most index value pivotStep 2 − partition the array using pivot valueStep 3 − quicksort left partition recursivelyStep 4 − quicksort right partition recursively
Data Structures & Algorithms170QuickSortPseudocodeTo get more into it, let see the pseudocode for quick sort algorithm −procedure quickSort(left, right)if right-left <= 0returnelsepivot = A[right]partition = partitionFunc(left, right, pivot)quickSort(left,partition-1)quickSort(partition+1,right)end ifend procedureTo know about quick sort implementation in C programming language, please click here.QuickSortPrograminCQuick sort is a highly efficient sorting algorithm and is based on partitioning of array ofdata into smaller arrays. A large array is partitioned into two arrays one of which holdsvalues smaller than the specified value, say pivot, based on which the partition is madeand another array holds values greater than the pivot value.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}
Data Structures & Algorithms171printf("=n");}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}printf("]n");}void swap(int num1, int num2){int temp = intArray[num1];intArray[num1] = intArray[num2];intArray[num2] = temp;}int partition(int left, int right, int pivot){int leftPointer = left -1;int rightPointer = right;while(true){while(intArray[++leftPointer] < pivot){//do nothing}while(rightPointer > 0 && intArray[--rightPointer] > pivot){//do nothing}if(leftPointer >= rightPointer){break;}else{
Data Structures & Algorithms172printf(" item swapped :%d,%dn",intArray[leftPointer],intArray[rightPointer]);swap(leftPointer,rightPointer);}}printf(" pivot swapped :%d,%dn", intArray[leftPointer],intArray[right]);swap(leftPointer,right);printf("Updated Array: ");display();return leftPointer;}void quickSort(int left, int right){if(right-left <= 0){return;}else {int pivot = intArray[right];int partitionPoint = partition(left, right, pivot);quickSort(left,partitionPoint-1);quickSort(partitionPoint+1,right);}}main(){printf("Input Array: ");display();printline(50);quickSort(0,MAX-1);printf("Output Array: ");display();printline(50);}
Data Structures & Algorithms173If we compile and run the above program, it will produce the following result −Input Array: [4 6 3 2 1 9 7 ]==================================================pivot swapped :9,7Updated Array: [4 6 3 2 1 7 9 ]pivot swapped :4,1Updated Array: [1 6 3 2 4 7 9 ]item swapped :6,2pivot swapped :6,4Updated Array: [1 2 3 4 6 7 9 ]pivot swapped :3,3Updated Array: [1 2 3 4 6 7 9 ]Output Array: [1 2 3 4 6 7 9 ]==================================================
Data Structures & Algorithms174Graph Data Structure
Data Structures & Algorithms175A graph is a pictorial representation of a set of objects where some pairs of objects areconnected by links. The interconnected objects are represented by points termedas vertices, and the links that connect the vertices are called edges.Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set ofedges, connecting the pairs of vertices. Take a look at the following graph −In the above graph,V = {a, b, c, d, e}E = {ab, ac, bd, cd, de}GraphDataStructureMathematical graphs can be represented in data structure. We can represent a graph usingan array of vertices and a two-dimensional array of edges. Before we proceed further, let'sfamiliarize ourselves with some important terms − Vertex − Each node of the graph is represented as a vertex. In the followingexample, the labeled circle represents vertices. Thus, A to G are vertices. We canrepresent them using an array as shown in the following image. Here A can beidentified by index 0. B can be identified using index 1 and so on.27. Graphs
Data Structures & Algorithms176 Edge − Edge represents a path between two vertices or a line between twovertices. In the following example, the lines from A to B, B to C, and so onrepresents edges. We can use a two-dimensional array to represent an array asshown in the following image. Here AB can be represented as 1 at row 0, column1, BC as 1 at row 1, column 2 and so on, keeping other combinations as 0. Adjacency − Two node or vertices are adjacent if they are connected to eachother through an edge. In the following example, B is adjacent to A, C is adjacentto B, and so on. Path − Path represents a sequence of edges between the two vertices. In thefollowing example, ABCD represents a path from A to D.
Data Structures & Algorithms177BasicOperationsFollowing are the basic primary operations that can be performed on 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.To know more about Graph, please read Graph Theory Tutorial. We shall learn abouttraversing a graph in the coming chapters.
Data Structures & Algorithms178Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses astack to remember to get the next vertex to start a search, when a dead end occurs inany iteration.As in the example given above, DFS algorithm traverses from A to B to C to D first thento E, then to F and lastly to G. It employs the following rules. Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push itin a stack. Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It willpop up all the vertices from the stack, which do not have adjacent vertices.) Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.28. Depth First Traversal
Data Structures & Algorithms179Steps Traversal Description1. Initialize the stack.2.Mark S as visited and put itonto the stack. Explore anyunvisited adjacent nodefrom S. We have three nodesand we can pick any of them.For this example, we shalltake the node in analphabetical order.3.Mark A as visited and put itonto the stack. Explore anyunvisited adjacent node fromA. Both S and D are adjacentto A but we are concerned forunvisited nodes only.
Data Structures & Algorithms1804.Visit D and mark it as visitedand put onto the stack. Here,we have B and C nodes, whichare adjacent to D and bothare unvisited. However, weshall again choose in analphabetical order.5.We choose B, mark it asvisited and put onto the stack.Here B does not have anyunvisited adjacent node. So,we pop B from the stack.6.We check the stack top forreturn to the previous nodeand check if it has anyunvisited nodes. Here, wefind D to be on the top of thestack.7.Only unvisited adjacent nodeis from D is C now. So wevisit C, mark it as visited andput it onto the stack.
Data Structures & Algorithms181As C does not have any unvisited adjacent node so we keep popping the stack until wefind a node that has an unvisited adjacent node. In this case, there's none and we keeppopping until the stack is empty.To know about the implementation of this algorithm in C programming language, clickhere.DepthFirstTraversalinCWe shall not see the implementation of Depth First Traversal (or Depth First Search) in Cprogramming language. For our reference purpose, we shall follow our example and takethis as our graph model −Implementation in C#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#define MAX 5struct Vertex {char label;bool visited;};
Data Structures & Algorithms182//stack variablesint stack[MAX];int top = -1;//graph variables//array of verticesstruct Vertex* lstVertices[MAX];//adjacency matrixint adjMatrix[MAX][MAX];//vertex countint vertexCount = 0;//stack functionsvoid push(int item) {stack[++top] = item;}int pop() {return stack[top--];}int peek() {return stack[top];}bool isStackEmpty() {return top == -1;}
Data Structures & Algorithms183//graph functions//add vertex to the vertex listvoid addVertex(char label) {struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));vertex->label = label;vertex->visited = false;lstVertices[vertexCount++] = vertex;}//add edge to edge arrayvoid addEdge(int start,int end) {adjMatrix[start][end] = 1;adjMatrix[end][start] = 1;}//display the vertexvoid displayVertex(int vertexIndex) {printf("%c ",lstVertices[vertexIndex]->label);}//get the adjacent unvisited vertexint getAdjUnvisitedVertex(int vertexIndex) {int i;for(i = 0; i<vertexCount; i++) {if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false) {return i;}}return -1;}
Data Structures & Algorithms184void depthFirstSearch() {int i;//mark first node as visitedlstVertices[0]->visited = true;//display the vertexdisplayVertex(0);//push vertex index in stackpush(0);while(!isStackEmpty()) {//get the unvisited vertex of vertex which is at top of the stackint unvisitedVertex = getAdjUnvisitedVertex(peek());//no adjacent vertex foundif(unvisitedVertex == -1) {pop();}else {lstVertices[unvisitedVertex]->visited = true;displayVertex(unvisitedVertex);push(unvisitedVertex);}}//stack is empty, search is complete, reset the visited flagfor(i = 0;i < vertexCount;i++) {lstVertices[i]->visited = false;}}
Data Structures & Algorithms185int main() {int i, j;for(i = 0; i<MAX; i++) // set adjacency {for(j = 0; j<MAX; j++) // matrix to 0adjMatrix[i][j] = 0;}addVertex('S'); // 0addVertex('A'); // 1addVertex('B'); // 2addVertex('C'); // 3addVertex('D'); // 4addEdge(0, 1); // S - AaddEdge(0, 2); // S - BaddEdge(0, 3); // S - CaddEdge(1, 4); // A - DaddEdge(2, 4); // B - DaddEdge(3, 4); // C - Dprintf("Depth First Search: ");depthFirstSearch();return 0;}If we compile and run the above program, it will produce the following result −Depth First Search: S A D B C
Data Structures & Algorithms186Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and usesa queue to remember to get the next vertex to start a search, when a dead end occurs inany iteration.As in the example given above, BFS algorithm traverses from A to B to E to F first then toC and G lastly to D. It employs the following rules. Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insertit in a queue. Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue. Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.29. Breadth First Traversal
Data Structures & Algorithms187Steps Traversal Description1. Initialize the queue.2.We start from visiting S(starting node), and mark itas visited.3.We then see an unvisitedadjacent node from S. In thisexample, we have three nodesbut alphabetically wechoose A, mark it as visitedand enqueue it.4.Next, the unvisited adjacentnode from S is B. We mark itas visited and enqueue it.
Data Structures & Algorithms1885.Next, the unvisited adjacentnode from S is C. We mark itas visited and enqueue it.6.Now, S is left with nounvisited adjacent nodes. So,we dequeue and find A.7.From A we have D asunvisited adjacent node. Wemark it as visited andenqueue it.At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithmwe keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied,the program is over.The implementation of this algorithm in C programming language can be seen here.BreadthFirstTraversalinCWe shall not see the implementation of Breadth First Traversal (or Breadth First Search)in C programming language. For our reference purpose, we shall follow our example andtake this as our graph model −
Data Structures & Algorithms189Implementation in C#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#define MAX 5struct Vertex {char label;bool visited;};//queue variablesint queue[MAX];int rear = -1;int front = 0;int queueItemCount = 0;//graph variables
Data Structures & Algorithms190//array of verticesstruct Vertex* lstVertices[MAX];//adjacency matrixint adjMatrix[MAX][MAX];//vertex countint vertexCount = 0;//queue functionsvoid insert(int data) {queue[++rear] = data;queueItemCount++;}int removeData() {queueItemCount--;return queue[front++];}bool isQueueEmpty() {return queueItemCount == 0;}//graph functions//add vertex to the vertex listvoid addVertex(char label) {struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));vertex->label = label;vertex->visited = false;lstVertices[vertexCount++] = vertex;}
Data Structures & Algorithms191//add edge to edge arrayvoid addEdge(int start,int end) {adjMatrix[start][end] = 1;adjMatrix[end][start] = 1;}//display the vertexvoid displayVertex(int vertexIndex) {printf("%c ",lstVertices[vertexIndex]->label);}//get the adjacent unvisited vertexint getAdjUnvisitedVertex(int vertexIndex) {int i;for(i = 0; i<vertexCount; i++) {if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false)return i;}return -1;}void breadthFirstSearch() {int i;//mark first node as visitedlstVertices[0]->visited = true;//display the vertexdisplayVertex(0);
Data Structures & Algorithms192//insert vertex index in queueinsert(0);int unvisitedVertex;while(!isQueueEmpty()) {//get the unvisited vertex of vertex which is at front of the queueint tempVertex = removeData();//no adjacent vertex foundwhile((unvisitedVertex = getAdjUnvisitedVertex(tempVertex)) != -1) {lstVertices[unvisitedVertex]->visited = true;displayVertex(unvisitedVertex);insert(unvisitedVertex);}}//queue is empty, search is complete, reset the visited flagfor(i = 0;i<vertexCount;i++) {lstVertices[i]->visited = false;}}int main() {int i, j;for(i = 0; i<MAX; i++) // set adjacency {for(j = 0; j<MAX; j++) // matrix to 0adjMatrix[i][j] = 0;}addVertex('S'); // 0addVertex('A'); // 1addVertex('B'); // 2addVertex('C'); // 3addVertex('D'); // 4
Data Structures & Algorithms193addEdge(0, 1); // S - AaddEdge(0, 2); // S - BaddEdge(0, 3); // S - CaddEdge(1, 4); // A - DaddEdge(2, 4); // B - DaddEdge(3, 4); // C - Dprintf("nBreadth First Search: ");breadthFirstSearch();return 0;}If we compile and run the above program, it will produce the following result −Breadth First Search: S A B C D
Data Structures & Algorithms194Tree Data Structure
Data Structures & Algorithms195Tree represents the nodes connected by edges. We will discuss binary tree or binary searchtree specifically.Binary Tree is a special datastructure used for data storage purposes. A binary tree has aspecial condition that each node can have a maximum of two children. A binary tree hasthe benefits of both an ordered array and a linked list as search is as quick as in a sortedarray and insertion or deletion operation are as fast as in linked list.ImportantTermsFollowing are the important terms with respect to tree. Path − Path refers to the sequence of nodes along the edges of a tree. Root – The node at the top of the tree is called root. There is only one root pertree and one path from the root node to any node. Parent − Any node except the root node has one edge upward to a node calledparent. Child – The node below a given node connected by its edge downward is called itschild node. Leaf – The node which does not have any child node is called the leaf node. Subtree − Subtree represents the descendants of a node.30. Tree
Data Structures & Algorithms196 Visiting − Visiting refers to checking the value of a node when control is on thenode. Traversing − Traversing means passing through nodes in a specific order. Levels − Level of a node represents the generation of a node. If the root node isat level 0, then its next child node is at level 1, its grandchild is at level 2, and soon. Keys − Key represents a value of a node based on which a search operation is tobe carried out for a node.BinarySearchTreeRepresentationBinary Search tree exhibits a special behavior. A node's left child must have a value lessthan its parent's value and the node's right child must have a value greater than its parentvalue.We're going to implement tree using node object and connecting them through references.TreeNodeThe code to write a tree node would be similar to what is given below. It has a data partand references to its left and right child nodes.struct node {int data;struct node *leftChild;struct node *rightChild;};
Data Structures & Algorithms197In a tree, all nodes share common construct.BSTBasicOperationsThe basic operations that can be performed on a binary search tree data structure, arethe following − Insert − Inserts an element in a tree/create a tree. Search − Searches an element in a tree. Pre-order Traversal − Traverses a tree in a pre-order manner. In-order Traversal − Traverses a tree in an in-order manner. Post-order Traversal − Traverses a tree in a post-order manner.We shall learn creating (inserting into) a tree structure and searching a data item in a treein this chapter. We shall learn about tree traversing methods in the coming chapter.InsertOperationThe very first insertion creates the tree. Afterwards, whenever an element is to beinserted, first locate its proper location. Start searching from the root node, then if thedata is less than the key value, search for the empty location in the left subtree and insertthe data. Otherwise, search for the empty location in the right subtree and insert the data.AlgorithmIf root is NULLthen create root nodereturnIf root exists thencompare the data with node.datawhile until insertion position is locatedIf data is greater than node.datagoto right subtreeelsegoto left subtree
Data Structures & Algorithms198endwhileinsert dataend IfImplementationThe implementation of insert function should look like this −void insert(int data) {struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is empty, create root nodeif(root == NULL) {root = tempNode;}else {current = root;parent = NULL;while(1) {parent = current;//go to left of the treeif(data < parent->data) {current = current->leftChild;//insert to the leftif(current == NULL) {parent->leftChild = tempNode;return;}
Data Structures & Algorithms199}//go to right of the treeelse {current = current->rightChild;//insert to the rightif(current == NULL) {parent->rightChild = tempNode;return;}}}}}SearchOperationWhenever an element is to be searched, start searching from the root node, then if thedata is less than the key value, search for the element in the left subtree. Otherwise,search for the element in the right subtree. Follow the same algorithm for each node.AlgorithmIf root.data is equal to search.datareturn rootelsewhile data not foundIf data is greater than node.datagoto right subtreeelsegoto left subtreeIf data foundreturn nodeendwhile
Data Structures & Algorithms200return data not foundend ifThe implementation of this algorithm should look like this.struct node* search(int data) {struct node *current = root;printf("Visiting elements: ");while(current->data != data) {if(current != NULL)printf("%d ",current->data);//go to left treeif(current->data > data) {current = current->leftChild;}//else go to right treeelse {current = current->rightChild;}//not foundif(current == NULL) {return NULL;}return current;}}To know about the implementation of binary search tree data structure, please click here.TreeTraversalinCTraversal is a process to visit all the nodes of a tree and may print their values too.Because, all nodes are connected via edges (links) we always start from the root (head)
Data Structures & Algorithms201node. That is, we cannot random access a node in a tree. There are three ways which weuse to traverse a tree − In-order Traversal Pre-order Traversal Post-order TraversalWe shall now look at the implementation of tree traversal in C programming language hereusing the following binary tree −Implementation in C#include <stdio.h>#include <stdlib.h>struct node {int data;struct node *leftChild;struct node *rightChild;};struct node *root = NULL;void insert(int data) {struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;
Data Structures & Algorithms202tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is emptyif(root == NULL) {root = tempNode;}else {current = root;parent = NULL;while(1) {parent = current;//go to left of the treeif(data < parent->data) {current = current->leftChild;//insert to the leftif(current == NULL) {parent->leftChild = tempNode;return;}}//go to right of the treeelse {current = current->rightChild;//insert to the rightif(current == NULL) {parent->rightChild = tempNode;return;}}}}}struct node* search(int data) {struct node *current = root;
Data Structures & Algorithms203printf("Visiting elements: ");while(current->data != data) {if(current != NULL)printf("%d ",current->data);//go to left treeif(current->data > data) {current = current->leftChild;}//else go to right treeelse {current = current->rightChild;}//not foundif(current == NULL) {return NULL;}}return current;}void pre_order_traversal(struct node* root) {if(root != NULL) {printf("%d ",root->data);pre_order_traversal(root->leftChild);pre_order_traversal(root->rightChild);}}void inorder_traversal(struct node* root) {if(root != NULL) {inorder_traversal(root->leftChild);printf("%d ",root->data);inorder_traversal(root->rightChild);
Data Structures & Algorithms204}}void post_order_traversal(struct node* root) {if(root != NULL) {post_order_traversal(root->leftChild);post_order_traversal(root->rightChild);printf("%d ", root->data);}}int main() {int i;int array[7] = { 27, 14, 35, 10, 19, 31, 42 };for(i = 0; i < 7; i++)insert(array[i]);i = 31;struct node * temp = search(i);if(temp != NULL) {printf("[%d] Element found.", temp->data);printf("n");}else {printf("[ x ] Element not found (%d).n", i);}i = 15;temp = search(i);if(temp != NULL) {printf("[%d] Element found.", temp->data);printf("n");}else {printf("[ x ] Element not found (%d).n", i);}
Data Structures & Algorithms205printf("nPreorder traversal: ");pre_order_traversal(root);printf("nInorder traversal: ");inorder_traversal(root);printf("nPost order traversal: ");post_order_traversal(root);return 0;}If we compile and run the above program, it will produce the following result −Visiting elements: 27 35 [31] Element found.Visiting elements: 27 14 19 [ x ] Element not found (15).Preorder traversal: 27 14 10 19 35 31 42Inorder traversal: 10 14 19 27 31 35 42Post order traversal: 10 19 14 31 42 35 27
Data Structures & Algorithms206Traversal is a process to visit all the nodes of a tree and may print their values too.Because, all nodes are connected via edges (links) we always start from the root (head)node. That is, we cannot randomly access a node in a tree. There are three ways whichwe use to traverse a tree − In-order Traversal Pre-order Traversal Post-order TraversalGenerally, we traverse a tree to search or locate a given item or key in the tree or to printall the values it contains.In-orderTraversalIn this traversal method, the left subtree is visited first, then the root and later the rightsub-tree. We should always remember that every node may represent a subtree itself.If a binary tree is traversed in-order, the output will produce sorted key values in anascending order.31. Tree Traversal
Data Structures & Algorithms207We start from A, and following in-order traversal, we move to its left subtree B. B is alsotraversed in-order. The process goes on until all the nodes are visited. The output of in-order traversal of this tree will be −D → B → E → A → F → C → GAlgorithmUntil all nodes are traversed −Step 1 − Recursively traverse left subtree.Step 2 − Visit root node.Step 3 − Recursively traverse right subtree.Pre-orderTraversalIn this traversal method, the root node is visited first, then the left subtree and finally theright subtree.We start from A, and following pre-order traversal, we first visit A itself and then move toits left subtree B. B is also traversed pre-order. The process goes on until all the nodesare visited. The output of pre-order traversal of this tree will be −A → B → D → E → C → F → G
Data Structures & Algorithms208AlgorithmUntil all nodes are traversed −Step 1 − Visit root node.Step 2 − Recursively traverse left subtree.Step 3 − Recursively traverse right subtree.Post-orderTraversalIn this traversal method, the root node is visited last, hence the name. First we traversethe left subtree, then the right subtree and finally the root node.We start from A, and following pre-order traversal, we first visit the left subtree B. B isalso traversed post-order. The process goes on until all the nodes are visited. The outputof post-order traversal of this tree will be −D → E → B → F → G → C → A
Data Structures & Algorithms209AlgorithmUntil all nodes are traversed −Step 1 − Recursively traverse left subtree.Step 2 − Recursively traverse right subtree.Step 3 − Visit root node.To check the C implementation of tree traversing, please click hereTreeTraversalinCTraversal is a process to visit all the nodes of a tree and may print their values too.Because, all nodes are connected via edges (links) we always start from the root (head)node. That is, we cannot randomly access a node in a tree. There are three ways whichwe use to traverse a tree − In-order Traversal Pre-order Traversal Post-order TraversalWe shall now see the implementation of tree traversal in C programming language hereusing the following binary tree −
Data Structures & Algorithms210Implementation in C#include <stdio.h>#include <stdlib.h>struct node {int data;struct node *leftChild;struct node *rightChild;};struct node *root = NULL;void insert(int data) {struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is emptyif(root == NULL) {root = tempNode;}else {current = root;parent = NULL;while(1) {parent = current;//go to left of the treeif(data < parent->data) {current = current->leftChild;
Data Structures & Algorithms211//insert to the leftif(current == NULL) {parent->leftChild = tempNode;return;}}//go to right of the treeelse {current = current->rightChild;//insert to the rightif(current == NULL) {parent->rightChild = tempNode;return;}}}}}struct node* search(int data) {struct node *current = root;printf("Visiting elements: ");while(current->data != data) {if(current != NULL)printf("%d ",current->data);//go to left treeif(current->data > data) {current = current->leftChild;}//else go to right treeelse {current = current->rightChild;}//not found
Data Structures & Algorithms212if(current == NULL) {return NULL;}}return current;}void pre_order_traversal(struct node* root) {if(root != NULL) {printf("%d ",root->data);pre_order_traversal(root->leftChild);pre_order_traversal(root->rightChild);}}void inorder_traversal(struct node* root) {if(root != NULL) {inorder_traversal(root->leftChild);printf("%d ",root->data);inorder_traversal(root->rightChild);}}void post_order_traversal(struct node* root) {if(root != NULL) {post_order_traversal(root->leftChild);post_order_traversal(root->rightChild);printf("%d ", root->data);}}int main() {
Data Structures & Algorithms213int i;int array[7] = { 27, 14, 35, 10, 19, 31, 42 };for(i = 0; i < 7; i++)insert(array[i]);i = 31;struct node * temp = search(i);if(temp != NULL) {printf("[%d] Element found.", temp->data);printf("n");}else {printf("[ x ] Element not found (%d).n", i);}i = 15;temp = search(i);if(temp != NULL) {printf("[%d] Element found.", temp->data);printf("n");}else {printf("[ x ] Element not found (%d).n", i);}printf("nPreorder traversal: ");pre_order_traversal(root);printf("nInorder traversal: ");inorder_traversal(root);printf("nPost order traversal: ");post_order_traversal(root);return 0;}
Data Structures & Algorithms214If we compile and run the above program, it will produce the following result −Visiting elements: 27 35 [31] Element found.Visiting elements: 27 14 19 [ x ] Element not found (15).Preorder traversal: 27 14 10 19 35 31 42Inorder traversal: 10 14 19 27 31 35 42Post order traversal: 10 19 14 31 42 35 27
Data Structures & Algorithms215A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentionedproperties − The left sub-tree of a node has a key less than or equal to its parent node's key. The right sub-tree of a node has a key greater than or equal to its parent node'skey.Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and can be defined as −left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)RepresentationBST is a collection of nodes arranged in a way where they maintain BST properties. Eachnode has a key and an associated value. While searching, the desired key is compared tothe keys in BST and if found, the associated value is retrieved.Following is a pictorial representation of BST −We observe that the root node key (27) has all less-valued keys on the left sub-tree andthe higher valued keys on the right sub-tree.32. Binary Search Tree
Data Structures & Algorithms216BasicOperationsFollowing are the basic operations of a tree - Search − Searches an element in a tree. Insert − Inserts an element in a tree. Pre-order Traversal − Traverses a tree in a pre-order manner. In-order Traversal − Traverses a tree in an in-order manner. Post-order Traversal − Traverses a tree in a post-order manner.NodeDefine a node having some data, references to its left and right child nodes.struct node {int data;struct node *leftChild;struct node *rightChild;};SearchOperationWhenever an element is to be searched, start searching from the root node. Then if thedata is less than the key value, search for the element in the left subtree. Otherwise,search for the element in the right subtree. Follow the same algorithm for each node.struct node* search(int data){struct node *current = root;printf("Visiting elements: ");while(current->data != data){if(current != NULL) {printf("%d ",current->data);//go to left treeif(current->data > data){current = current->leftChild;
Data Structures & Algorithms217}//else go to right treeelse {current = current->rightChild;}//not foundif(current == NULL){return NULL;}}}return current;}InsertOperationWhenever an element is to be inserted, first locate its proper location. Start searchingfrom the root node, then if the data is less than the key value, search for the emptylocation in the left subtree and insert the data. Otherwise, search for the empty locationin the right subtree and insert the data.void insert(int data){struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is emptyif(root == NULL){root = tempNode;}else {current = root;parent = NULL;while(1){parent = current;
Data Structures & Algorithms218//go to left of the treeif(data < parent->data){current = current->leftChild;//insert to the leftif(current == NULL){parent->leftChild = tempNode;return;}}//go to right of the treeelse{current = current->rightChild;//insert to the rightif(current == NULL){parent->rightChild = tempNode;return;}}}}}
Data Structures & Algorithms219What if the input to binary search tree comes in a sorted (ascending or descending)manner? It will then look like this −It is observed that BST's worst-case performance is closest to linear search algorithms,that is Ο(n). In real-time data, we cannot predict data pattern and their frequencies. So,a need arises to balance out the existing BST.Named after their inventor Adelson, Velski & Landis, AVL trees are height balancingbinary search tree. AVL tree checks the height of the left and the right sub-trees andassures that the difference is not more than 1. This difference is called the BalanceFactor.Here we see that the first tree is balanced and the next two trees are not balanced −33. AVL Trees
Data Structures & Algorithms220In the second tree, the left subtree of C has height 2 and the right subtree has height 0,so the difference is 2. In the third tree, the right subtree of A has height 2 and the left ismissing, so it is 0, and the difference is 2 again. AVL tree permits difference (balancefactor) to be only 1.BalanceFactor = height(left-sutree) − height(right-sutree)If the difference in the height of left and right sub-trees is more than 1, the tree is balancedusing some rotation techniques.AVLRotationsTo balance itself, an AVL tree may perform the following four kinds of rotations − Left rotation Right rotation Left-Right rotation Right-Left rotationThe first two rotations are single rotations and the next two rotations are double rotations.To have an unbalanced tree, we at least need a tree of height 2. With this simple tree,let's understand them one by one.Left RotationIf a tree becomes unbalanced, when a node is inserted into the right subtree of the rightsubtree, then we perform a single left rotation −In our example, node A has become unbalanced as a node is inserted in the right subtreeof A's right subtree. We perform the left rotation by making A the left-subtree of B.
Data Structures & Algorithms221Right RotationAVL tree may become unbalanced, if a node is inserted in the left subtree of the leftsubtree. The tree then needs a right rotation.As depicted, the unbalanced node becomes the right child of its left child by performing aright rotation.Left-Right RotationDouble rotations are slightly complex version of already explained versions of rotations.To understand them better, we should take note of each action performed while rotation.Let's first check how to perform Left-Right rotation. A left-right rotation is a combinationof left rotation followed by right rotation.State ActionA node has been inserted into the right subtree of the leftsubtree. This makes C an unbalanced node. These scenarioscause AVL tree to perform left-right rotation.We first perform the left rotation on the left subtree of C.This makes A, the left subtree of B.
Data Structures & Algorithms222Node C is still unbalanced, however now, it is because of theleft-subtree of the left-subtree.We shall now right-rotate the tree, making B the new rootnode of this subtree. C now becomes the right subtree of itsown left subtree.The tree is now balanced.Right-Left RotationThe second type of double rotation is Right-Left Rotation. It is a combination of rightrotation followed by left rotation.State ActionA node has been inserted into the left subtree of the rightsubtree. This makes A, an unbalanced node with balancefactor 2.
Data Structures & Algorithms223First, we perform the right rotation along C node, making Cthe right subtree of its own left subtree B. Now, B becomesthe right subtree of A.Node A is still unbalanced because of the right subtree of itsright subtree and requires a left rotation.A left rotation is performed by making B the new root nodeof the subtree. A becomes the left subtree of its rightsubtree B.The tree is now balanced.
Data Structures & Algorithms224A spanning tree is a subset of Graph G, which has all the vertices covered with minimumpossible number of edges. Hence, a spanning tree does not have cycles and it cannot bedisconnected.By this definition, we can draw a conclusion that every connected and undirected Graph Ghas at least one spanning tree. A disconnected graph does not have any spanning tree, asit cannot be spanned to all its vertices.We found three spanning trees off one complete graph. A complete undirected graph canhave maximum nn-2number of spanning trees, where n is the number of nodes. In theabove addressed example, n is 3, hence 33−2= 3 spanning trees are possible.GeneralPropertiesofSpanningTreeWe now understand that one graph can have more than one spanning tree. Following area few properties of the spanning tree connected to graph G - A connected graph G can have more than one spanning tree. All possible spanning trees of graph G, have the same number of edges andvertices. The spanning tree does not have any cycle (loops).34. Spanning Tree
Data Structures & Algorithms225 Removing one edge from the spanning tree will make the graph disconnected, i.e.the spanning tree is minimally connected. Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanningtree is maximally acyclic.MathematicalPropertiesofSpanningTree Spanning tree has n-1 edges, where n is the number of nodes (vertices). From a complete graph, by removing maximum e-n+1 edges, we can construct aspanning tree. A complete graph can have maximum nn-2number of spanning trees.Thus, we can conclude that spanning trees are a subset of connected Graph G anddisconnected graphs do not have spanning tree.ApplicationofSpanningTreeSpanning tree is basically used to find a minimum path to connect all nodes in a graph.Common application of spanning trees are − Civil Network Planning Computer Network Routing Protocol Cluster AnalysisLet us understand this through a small example. Consider, city network as a huge graphand now plans to deploy telephone lines in such a way that in minimum lines we canconnect to all city nodes. This is where the spanning tree comes into picture.MinimumSpanningTree(MST)In a weighted graph, a minimum spanning tree is a spanning tree that has minimumweight than all other spanning trees of the same graph. In real-world situations, thisweight can be measured as distance, congestion, traffic load or any arbitrary valuedenoted to the edges.MinimumSpanning-TreeAlgorithmWe shall learn about two most important spanning tree algorithms here − Kruskal's Algorithm Prim's AlgorithmBoth are greedy algorithms.
Data Structures & Algorithms226Kruskal'sSpanningTreeAlgorithmKruskal's algorithm to find the minimum cost spanning tree uses the greedy approach.This algorithm treats the graph as a forest and every node it has as an individual tree. Atree connects to another only and only if, it has the least cost among all available optionsand does not violate MST properties.To understand Kruskal's algorithm let us consider the following example −Step 1 - Remove all loops and parallel edgesRemove all loops and parallel edges from the given graph.
Data Structures & Algorithms227In case of parallel edges, keep the one which has the least cost associated and remove allothers.Step 2 - Arrange all edges in their increasing order of weightThe next step is to create a set of edges and weight, and arrange them in an ascendingorder of weightage (cost).Step 3 - Add the edge which has the least weightageNow we start adding edges to the graph beginning from the one which has the least weight.Throughout, we shall keep checking that the spanning properties remain intact. In case,by adding one edge, the spanning tree property does not hold then we shall consider notto include the edge in the graph.
Data Structures & Algorithms228The least cost is 2 and edges involved are B,D and D,T. We add them. Adding them doesnot violate spanning tree properties, so we continue to our next edge selection.Next cost is 3, and associated edges are A,C and C,D. We add them again −Next cost in the table is 4, and we observe that adding it will create a circuit in the graph.We ignore it. In the process we shall ignore/avoid all edges that create a circuit.
Data Structures & Algorithms229We observe that edges with cost 5 and 6 also create circuits. We ignore them and moveon.Now we are left with only one node to be added. Between the two least cost edges available7 and 8, we shall add the edge with cost 7.By adding edge S,A we have included all the nodes of the graph and we now have minimumcost spanning tree.Prim'sSpanningTreeAlgorithmPrim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses thegreedy approach. Prim's algorithm shares a similarity with the shortest pathfirst algorithms.Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single treeand keeps on adding new nodes to the spanning tree from the given graph.
Data Structures & Algorithms230To contrast with Kruskal's algorithm and to understand Prim's algorithm better, we shalluse the same example −Step 1 - Remove all loops and parallel edges
Data Structures & Algorithms231Remove all loops and parallel edges from the given graph. In case of parallel edges, keepthe one which has the least cost associated and remove all others.Step 2 - Choose any arbitrary node as root nodeIn this case, we choose S node as the root node of Prim's spanning tree. This node isarbitrarily chosen, so any node can be the root node. One may wonder why any video canbe a root node. So the answer is, in the spanning tree all the nodes of a graph are includedand because it is connected then there must be at least one edge, which will join it to therest of the tree.Step 3 - Check outgoing edges and select the one with less costAfter choosing the root node S, we see that S,A and S,C are two edges with weight 7 and8, respectively. We choose the edge S,A as it is lesser than the other.
Data Structures & Algorithms232Now, the tree S-7-A is treated as one node and we check for all edges going out from it.We select the one which has the lowest cost and include it in the tree.After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will checkall the edges again. However, we will choose only the least cost edge. In this case, C-3-Dis the new edge, which is less than other edges' cost 8, 6, 4, etc.After adding node D to the spanning tree, we now have two edges going out of it havingthe same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the next step willagain yield edge 2 as the least cost. Hence, we are showing a spanning tree with bothedges included.We may find that the output spanning tree of the same graph using two differentalgorithms is same.
Data Structures & Algorithms233Heap is a special case of balanced binary tree data structure where the root-node key iscompared with its children and arranged accordingly. If α has child node β then −key(α) ≥ key(β)As the value of parent is greater than that of child, this property generates Max Heap.Based on this criteria, a heap can be of two types −For Input → 35 33 42 10 14 19 27 44 26 31Min-Heap − Where the value of the root node is less than or equal to either of its children.Max-Heap − Where the value of the root node is greater than or equal to either of itschildren.35. Heaps
Data Structures & Algorithms234Both trees are constructed using the same input and order of arrival.MaxHeapConstructionAlgorithmWe shall use the same example to demonstrate how a Max Heap is created. The procedureto create Min Heap is similar but we go for min values instead of max values.We are going to derive an algorithm for max heap by inserting one element at a time. Atany point of time, heap must maintain its property. While insertion, we also assume thatwe are inserting a node in an already heapified tree.Step 1 − Create a new node at the end of heap.Step 2 − Assign new value to the node.Step 3 − Compare the value of this child node with its parent.Step 4 − If value of parent is less than child, then swap them.Step 5 − Repeat step 3 & 4 until Heap property holds.Note − In Min Heap construction algorithm, we expect the value of the parent node to beless than that of the child node.Let's understand Max Heap construction by an animated illustration. We consider the sameinput sample that we used earlier.
Data Structures & Algorithms235MaxHeapDeletionAlgorithmLet us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap alwayshappens at the root to remove the Maximum (or minimum) value.Step 1 − Remove root node.Step 2 − Move the last element of last level to root.Step 3 − Compare the value of this child node with its parent.Step 4 − If value of parent is less than child, then swap them.Step 5 − Repeat step 3 & 4 until Heap property holds.
Data Structures & Algorithms236Recursion
Data Structures & Algorithms237Some computer programming languages allow a module or function to call itself. Thistechnique is known as recursion. In recursion, a function α either calls itself directly orcalls a function β that in turn calls the original function α. The function α is called recursivefunction.Example − a function calling itself.int function(int value) {if(value < 1)return;function(value - 1);printf("%d ",value);}Example − a function that calls another function which in turn calls it again.int function(int value) {if(value < 1)return;function(value - 1);printf("%d ",value);}PropertiesA recursive function can go infinite like a loop. To avoid infinite running of recursivefunction, there are two properties that a recursive function must have − Base criteria − There must be at least one base criteria or condition, such that,when this condition is met the function stops calling itself recursively. Progressive approach − The recursive calls should progress in such a way thateach time a recursive call is made it comes closer to the base criteria.36. Recursion ─ Basics
Data Structures & Algorithms238ImplementationMany programming languages implement recursion by means of stacks. Generally,whenever a function (caller) calls another function (callee) or itself as callee, the callerfunction transfers execution control to the callee. This transfer process may also involvesome data to be passed from the caller to the callee.This implies, the caller function has to suspend its execution temporarily and resume laterwhen the execution control returns from the callee function. Here, the caller function needsto start exactly from the point of execution where it puts itself on hold. It also needs theexact same data values it was working on. For this purpose, an activation record (or stackframe) is created for the caller function.This activation record keeps the information about local variables, formal parameters,return address and all information passed to the caller function.AnalysisofRecursionOne may argue why to use recursion, as the same task can be done with iteration. Thefirst reason is, recursion makes a program more readable and because of latest enhancedCPU systems, recursion is more efficient than iterations.TimeComplexityIn case of iterations, we take number of iterations to count the time complexity. Likewise,in case of recursion, assuming everything is constant, we try to figure out the number oftimes a recursive call is being made. A call made to a function is Ο(1), hence the (n)number of times a recursive call is made makes the recursive function Ο(n).
Data Structures & Algorithms239SpaceComplexitySpace complexity is counted as what amount of extra space is required for a module toexecute. In case of iterations, the compiler hardly requires any extra space. The compilerkeeps updating the values of variables used in the iterations. But in case of recursion, thesystem needs to store activation record each time a recursive call is made. Hence, it isconsidered that space complexity of recursive function may go higher than that of afunction with iteration.
Data Structures & Algorithms240Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and morethan one rings is as depicted −These rings are of different sizes and stacked upon in an ascending order, i.e. the smallerone sits over the larger one. There are other variations of the puzzle where the number ofdisks increase, but the tower count remains the same.RulesThe mission is to move all the disks to some another tower without violating the sequenceof arrangement. A few rules to be followed for Tower of Hanoi are − Only one disk can be moved among the towers at any given time. Only the "top" disk can be removed. No large disk can sit over a small disk.37. Tower of Hanoi
Data Structures & Algorithms241Following is an animated representation of solving a Tower of Hanoi puzzle with threedisks.
Data Structures & Algorithms242
Data Structures & Algorithms243Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. Thispresentation shows that a puzzle with 3 disks has taken 23−1 = 7 steps.
Data Structures & Algorithms244AlgorithmTo write an algorithm for Tower of Hanoi, first we need to learn how to solve this problemwith lesser amount of disks, say → 1 or 2. We mark three towers withname, source, destination and aux (only to help moving the disks). If we have only onedisk, then it can easily be moved from source to destination peg.If we have 2 disks – First, we move the smaller (top) disk to aux peg. Then, we move the larger (bottom) disk to destination peg. And finally, we move the smaller disk from aux to destination peg.
Data Structures & Algorithms245
Data Structures & Algorithms246So now, we are in a position to design an algorithm for Tower of Hanoi with more thantwo disks. We divide the stack of disks in two parts. The largest disk (nthdisk) is in onepart and all other (n-1) disks are in the second part.Our ultimate aim is to move disk n from source to destination and then put all other (n-1) disks onto it. We can imagine to apply the same in a recursive way for all given set ofdisks.The steps to follow are −Step 1 − Move n-1 disks from source to auxStep 2 − Move nth disk from source to destStep 3 − Move n-1 disks from aux to destA recursive algorithm for Tower of Hanoi can be driven as follows −STARTProcedure Hanoi(disk, source, dest, aux)IF disk == 0, THENmove disk from source to destELSEHanoi(disk - 1, source, aux, dest) // Step 1move disk from source to dest // Step 2Hanoi(disk - 1, aux, dest, source) // Step 3END IFEND ProcedureSTOPTo check the implementation in C programming, click here.
Data Structures & Algorithms247TowerofHanoiinCProgram#include <stdio.h>#include <stdbool.h>#define MAX 10int list[MAX] = {1,8,4,6,0,3,5,2,7,9};void display(){int i;printf("[");// navigate through all itemsfor(i = 0; i < MAX; i++){printf("%d ",list[i]);}printf("]n");}void bubbleSort() {int temp;int i,j;bool swapped = false;// loop through all numbersfor(i = 0; i < MAX-1; i++) {swapped = false;// loop through numbers falling aheadfor(j = 0; j < MAX-1-i; j++) {printf("Items compared: [ %d, %d ] ", list[j],list[j+1]);
Data Structures & Algorithms248// check if next number is lesser than current no// swap the numbers.// (Bubble up the highest number)if(list[j] > list[j+1]) {temp = list[j];list[j] = list[j+1];list[j+1] = temp;swapped = true;printf(" => swapped [%d, %d]n",list[j],list[j+1]);}else {printf(" => not swappedn");}}// if no number was swapped that means// array is sorted now, break the loop.if(!swapped) {break;}printf("Iteration %d#: ",(i+1));display();}}main(){printf("Input Array: ");display();printf("n");bubbleSort();printf("nOutput Array: ");display();}
Data Structures & Algorithms249If we compile and run the above program, it will produce the following result −Input Array: [1 8 4 6 0 3 5 2 7 9 ]Items compared: [ 1, 8 ] => not swappedItems compared: [ 8, 4 ] => swapped [4, 8]Items compared: [ 8, 6 ] => swapped [6, 8]Items compared: [ 8, 0 ] => swapped [0, 8]Items compared: [ 8, 3 ] => swapped [3, 8]Items compared: [ 8, 5 ] => swapped [5, 8]Items compared: [ 8, 2 ] => swapped [2, 8]Items compared: [ 8, 7 ] => swapped [7, 8]Items compared: [ 8, 9 ] => not swappedIteration 1#: [1 4 6 0 3 5 2 7 8 9 ]Items compared: [ 1, 4 ] => not swappedItems compared: [ 4, 6 ] => not swappedItems compared: [ 6, 0 ] => swapped [0, 6]Items compared: [ 6, 3 ] => swapped [3, 6]Items compared: [ 6, 5 ] => swapped [5, 6]Items compared: [ 6, 2 ] => swapped [2, 6]Items compared: [ 6, 7 ] => not swappedItems compared: [ 7, 8 ] => not swappedIteration 2#: [1 4 0 3 5 2 6 7 8 9 ]Items compared: [ 1, 4 ] => not swappedItems compared: [ 4, 0 ] => swapped [0, 4]Items compared: [ 4, 3 ] => swapped [3, 4]Items compared: [ 4, 5 ] => not swappedItems compared: [ 5, 2 ] => swapped [2, 5]Items compared: [ 5, 6 ] => not swappedItems compared: [ 6, 7 ] => not swappedIteration 3#: [1 0 3 4 2 5 6 7 8 9 ]Items compared: [ 1, 0 ] => swapped [0, 1]Items compared: [ 1, 3 ] => not swappedItems compared: [ 3, 4 ] => not swappedItems compared: [ 4, 2 ] => swapped [2, 4]Items compared: [ 4, 5 ] => not swappedItems compared: [ 5, 6 ] => not swapped
Data Structures & Algorithms250Iteration 4#: [0 1 3 2 4 5 6 7 8 9 ]Items compared: [ 0, 1 ] => not swappedItems compared: [ 1, 3 ] => not swappedItems compared: [ 3, 2 ] => swapped [2, 3]Items compared: [ 3, 4 ] => not swappedItems compared: [ 4, 5 ] => not swappedIteration 5#: [0 1 2 3 4 5 6 7 8 9 ]Items compared: [ 0, 1 ] => not swappedItems compared: [ 1, 2 ] => not swappedItems compared: [ 2, 3 ] => not swappedItems compared: [ 3, 4 ] => not swappedOutput Array: [0 1 2 3 4 5 6 7 8 9 ]
Data Structures & Algorithms251Fibonacci series generates the subsequent number by adding two previous numbers.Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can betaken as 0, 1 or 1, 1 respectively.Fibonacci series satisfies the following conditions −Fn = Fn-1 + Fn-2Hence, a Fibonacci series can look like this −F8 = 0 1 1 2 3 5 8 13or, this −F8 = 1 1 2 3 5 8 13 21For illustration purpose, Fibonacci of F8 is displayed as −38. Fibonacci Series
Data Structures & Algorithms252FibonacciIterativeAlgorithmFirst we try to draft the iterative algorithm for Fibonacci series.Procedure Fibonacci(n)declare f0, f1, fib, loopset f0 to 0set f1 to 1display f0, f1for loop ← 1 to nfib ← f0 + f1f0 ← f1f1 ← fibdisplay fibend forend procedureTo know about the implementation of the above algorithm in C programminglanguage, click here.FibonacciInteractivePrograminCFibonacci Program in CRecursionDemo.c#include <stdio.h>int factorial(int n){//base caseif(n == 0){return 1;
Data Structures & Algorithms253}else {return n * factorial(n-1);}}int fibbonacci(int n){if(n == 0){return 0;}else if(n == 1){return 1;}else {return (fibbonacci(n-1) + fibbonacci(n-2));}}main(){int n = 5;int i;printf("Factorial of %d: %dn" , n , factorial(n));printf("Fibbonacci of %d: " , n);for(i = 0;i<n;i++){printf("%d ",fibbonacci(i));}}If we compile and run the above program, it will produce the following result −Factorial of 5: 120Fibbonacci of 5: 0 1 1 2 3
Data Structures & Algorithms254FibonacciRecursiveAlgorithmLet us learn how to create a recursive algorithm Fibonacci series. The base criteria ofrecursion.STARTProcedure Fibonacci(n)declare f0, f1, fib, loopset f0 to 0set f1 to 1display f0, f1for loop ← 1 to nfib ← f0 + f1f0 ← f1f1 ← fibdisplay fibend forENDTo know about the implementation of the above algorithm in C programminglanguage, click here.FibonacciRecursivePrograminCFibonacci Program in C#include <stdio.h>int factorial(int n){//base caseif(n == 0){return 1;
Data Structures & Algorithms255}else {return n * factorial(n-1);}}int fibbonacci(int n){if(n == 0){return 0;}else if(n == 1){return 1;}else {return (fibbonacci(n-1) + fibbonacci(n-2));}}main(){int n = 5;int i;printf("Factorial of %d: %dn" , n , factorial(n));printf("Fibbonacci of %d: " , n);for(i = 0;i<n;i++){printf("%d ",fibbonacci(i));}}If we compile and run the above program, it will produce the following result −Factorial of 5: 120Fibbonacci of 5: 0 1 1 2 3

Recommended

PDF
Computer fundamentals
PDF
Assembly programming tutorial
PDF
Computer fundamentals tutorial
PDF
Analog communication tutorial
PDF
Wireless communication tutorial
PDF
Cakephp tutorial
PDF
Data structures algorithms_tutorial
PDF
Data structures algorithms_tutorial
PPTX
Data Structure and Algorithms
PDF
computer notes - Introduction to data structures
PPTX
Data Structures and Algorithm - Module 1.pptx
DOC
e computer notes - Introduction to data structures
PDF
Unit I Data structure and algorithms notes
PPTX
lecture1-2202211144eeeee24444444413.pptx
PPTX
lecture1-220221114413Algorithims and data structures.pptx
PPTX
Algorithms and Data Structures
PDF
Data Structure and its Fundamentals
PPT
Lect 1-2
PPTX
Data_structures_and_algorithm_Lec_1.pptx
PPTX
Data_structures_and_algorithm_Lec_1.pptx
PPT
Data structure
PDF
Data Structures and Algorithms (DSA) in C
PPTX
Data Structure & aaplications_Module-1.pptx
PDF
Data Structures and algorithms in C
PDF
Data structures-sample-programs
PPTX
Introduction-to-Data-Structure-and-Algorithm.pptx
PDF
Mahout tutorial
PDF
Learn c programming

More Related Content

PDF
Computer fundamentals
PDF
Assembly programming tutorial
PDF
Computer fundamentals tutorial
PDF
Analog communication tutorial
PDF
Wireless communication tutorial
PDF
Cakephp tutorial
Computer fundamentals
Assembly programming tutorial
Computer fundamentals tutorial
Analog communication tutorial
Wireless communication tutorial
Cakephp tutorial

Similar to Learn data structures algorithms tutorial

PDF
Data structures algorithms_tutorial
PDF
Data structures algorithms_tutorial
PPTX
Data Structure and Algorithms
PDF
computer notes - Introduction to data structures
PPTX
Data Structures and Algorithm - Module 1.pptx
DOC
e computer notes - Introduction to data structures
PDF
Unit I Data structure and algorithms notes
PPTX
lecture1-2202211144eeeee24444444413.pptx
PPTX
lecture1-220221114413Algorithims and data structures.pptx
PPTX
Algorithms and Data Structures
PDF
Data Structure and its Fundamentals
PPT
Lect 1-2
PPTX
Data_structures_and_algorithm_Lec_1.pptx
PPTX
Data_structures_and_algorithm_Lec_1.pptx
PPT
Data structure
PDF
Data Structures and Algorithms (DSA) in C
PPTX
Data Structure & aaplications_Module-1.pptx
PDF
Data Structures and algorithms in C
PDF
Data structures-sample-programs
PPTX
Introduction-to-Data-Structure-and-Algorithm.pptx
Data structures algorithms_tutorial
Data structures algorithms_tutorial
Data Structure and Algorithms
computer notes - Introduction to data structures
Data Structures and Algorithm - Module 1.pptx
e computer notes - Introduction to data structures
Unit I Data structure and algorithms notes
lecture1-2202211144eeeee24444444413.pptx
lecture1-220221114413Algorithims and data structures.pptx
Algorithms and Data Structures
Data Structure and its Fundamentals
Lect 1-2
Data_structures_and_algorithm_Lec_1.pptx
Data_structures_and_algorithm_Lec_1.pptx
Data structure
Data Structures and Algorithms (DSA) in C
Data Structure & aaplications_Module-1.pptx
Data Structures and algorithms in C
Data structures-sample-programs
Introduction-to-Data-Structure-and-Algorithm.pptx

More from Ashoka Vanjare

PDF
Mahout tutorial
PDF
Learn c programming
PDF
Php7 tutorial
PDF
Xml tutorial
PDF
Mongodb tutorial
PDF
Postgresql tutorial
PDF
Sqoop tutorial
PDF
Maven tutorial
PDF
Xslt tutorial
PDF
Tika tutorial
PDF
Xpath tutorial
PDF
Postgresql quick guide
PDF
Xsd tutorial
PDF
Xquery xpath
PDF
Json tutorial
PDF
Sqlite perl
PDF
Learn embedded systems tutorial
PDF
Learn c standard library
PDF
Perltut
PDF
Perl tutorial final
Mahout tutorial
Learn c programming
Php7 tutorial
Xml tutorial
Mongodb tutorial
Postgresql tutorial
Sqoop tutorial
Maven tutorial
Xslt tutorial
Tika tutorial
Xpath tutorial
Postgresql quick guide
Xsd tutorial
Xquery xpath
Json tutorial
Sqlite perl
Learn embedded systems tutorial
Learn c standard library
Perltut
Perl tutorial final

Recently uploaded

PDF
ACI 318-2205_American Concrete Institute.pdf
PPTX
TPM Metrics & Measurement: Drive Performance Excellence with TPM | MaintWiz
PPT
Software Engineering Unit-1 presentation for students
PDF
Presentation-on-Energy-Transition-in-Bangladesh-Employment-and-Skills.pdf
PPTX
Optimizing Plant Maintenance — Key Elements of a Successful Maintenance Plan ...
PPTX
Track & Monitor Preventive Maintenance — Best Practices with MaintWiz CMMS
PPTX
How to Implement Kaizen in Your Organization for Continuous Improvement Success
PPTX
Best CMMS for IoT Integration: Real-Time Asset Intelligence & Smart Maintenan...
PPTX
Emerging Trends and Research Frontiers in Chemical Engineering for Green and ...
PDF
Enhancing Distributed Authorization with Lagrange Interpolation and Attribute...
PPT
new Introduction to PACS.ppt Picture Archieving and communication and medicine
PDF
Hybrid Anomaly Detection Mechanism for IOT Networks
PDF
Advanced Intrusion Detection and Classification using Transfer Learning with ...
PPTX
علي نفط.pptx هندسة النفط هندسة النفط والغاز
PPTX
Role of In Vitro and In Vivo Testing biomedical engineering
PDF
IPEC Presentation - Partial discharge Pro .pdf
PPTX
Data Science with R Final yrUnit II.pptx
PDF
Handheld_Laser_Welding_Presentation 2.pdf
PDF
Engineering Properties of Agricultural Food Materials-.pdf and ppt
PDF
Narrows Planning Collective Transportation Capstone.pdf
 
ACI 318-2205_American Concrete Institute.pdf
TPM Metrics & Measurement: Drive Performance Excellence with TPM | MaintWiz
Software Engineering Unit-1 presentation for students
Presentation-on-Energy-Transition-in-Bangladesh-Employment-and-Skills.pdf
Optimizing Plant Maintenance — Key Elements of a Successful Maintenance Plan ...
Track & Monitor Preventive Maintenance — Best Practices with MaintWiz CMMS
How to Implement Kaizen in Your Organization for Continuous Improvement Success
Best CMMS for IoT Integration: Real-Time Asset Intelligence & Smart Maintenan...
Emerging Trends and Research Frontiers in Chemical Engineering for Green and ...
Enhancing Distributed Authorization with Lagrange Interpolation and Attribute...
new Introduction to PACS.ppt Picture Archieving and communication and medicine
Hybrid Anomaly Detection Mechanism for IOT Networks
Advanced Intrusion Detection and Classification using Transfer Learning with ...
علي نفط.pptx هندسة النفط هندسة النفط والغاز
Role of In Vitro and In Vivo Testing biomedical engineering
IPEC Presentation - Partial discharge Pro .pdf
Data Science with R Final yrUnit II.pptx
Handheld_Laser_Welding_Presentation 2.pdf
Engineering Properties of Agricultural Food Materials-.pdf and ppt
Narrows Planning Collective Transportation Capstone.pdf
 

Learn data structures algorithms tutorial

  • 2.
    Data Structures &AlgorithmsiAbouttheTutorialData Structures are the programmatic way of storing data so that data can be usedefficiently. Almost every enterprise application uses various types of data structures in oneor the other way.This tutorial will give you a great understanding on Data Structures needed to understandthe complexity of enterprise level applications and need of algorithms, and data structures.AudienceThis tutorial is designed for Computer Science graduates as well as Software Professionalswho are willing to learn data structures and algorithm programming in simple and easysteps.After completing this tutorial you will be at intermediate level of expertise from where youcan take yourself to higher level of expertise.PrerequisitesBefore proceeding with this tutorial, you should have a basic understanding of Cprogramming language, text editor, and execution of programs, etc.CopyrightandDisclaimer© Copyright 2016 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.com
  • 3.
    Data Structures &AlgorithmsiiCompile&ExecuteOnlineFor most of the examples given in this tutorial you will find Try it option, so just make useof this option to execute your programs on the spot and enjoy your learning.Try the following example using the Try it option available at the top right corner of thefollowing sample code box −#include <stdio.h>int main(){/* My first program in C */printf("Hello, World! n");return 0;}
  • 4.
    Data Structures &AlgorithmsiiiTableofContentsAbout the Tutorial ............................................................................................................................................iAudience...........................................................................................................................................................iPrerequisites.....................................................................................................................................................iCopyright and Disclaimer .................................................................................................................................iCompile & Execute Online............................................................................................................................... iiTable of Contents ........................................................................................................................................... iiiBASICS.........................................................................................................................................11. Overview ..................................................................................................................................................2Characteristics of a Data Structure..................................................................................................................2Need for Data Structure ..................................................................................................................................2Execution Time Cases ......................................................................................................................................3Basic Terminology ...........................................................................................................................................32. Environment Setup ...................................................................................................................................4Try it Option Online.........................................................................................................................................4Local Environment Setup.................................................................................................................................4Installation on UNIX/Linux...............................................................................................................................5Installation on Mac OS.....................................................................................................................................5Installation on Windows..................................................................................................................................6ALGORITHM................................................................................................................................73. Algorithms ─ Basics...................................................................................................................................8Characteristics of an Algorithm .......................................................................................................................8How to Write an Algorithm? ...........................................................................................................................9Algorithm Analysis.........................................................................................................................................10Algorithm Complexity....................................................................................................................................11Space Complexity ..........................................................................................................................................11Time Complexity............................................................................................................................................114. Asymptotic Analysis................................................................................................................................12Asymptotic Notations....................................................................................................................................12Common Asymptotic Notations ....................................................................................................................155. Greedy Algorithms..................................................................................................................................16Counting Coins...............................................................................................................................................166. Divide & Conquer....................................................................................................................................18Divide/Break..................................................................................................................................................18Conquer/Solve...............................................................................................................................................18Merge/Combine ............................................................................................................................................197. Dynamic Programming............................................................................................................................20
  • 5.
    Data Structures &AlgorithmsivDATA STRUCTURES ...................................................................................................................218. Basic Concepts ........................................................................................................................................22Data Definition ..............................................................................................................................................22Data Object....................................................................................................................................................22Data Type.......................................................................................................................................................22Basic Operations............................................................................................................................................239. Arrays .....................................................................................................................................................24Array Representation ....................................................................................................................................24Basic Operations............................................................................................................................................25Insertion Operation .......................................................................................................................................25Array Insertions .............................................................................................................................................27Insertion at the Beginning of an Array ..........................................................................................................28Insertion at the Given Index of an Array .......................................................................................................30Insertion After the Given Index of an Array ..................................................................................................32Insertion Before the Given Index of an Array................................................................................................34Deletion Operation........................................................................................................................................36Search Operation...........................................................................................................................................37Update Operation..........................................................................................................................................39LINKED LIST...............................................................................................................................4110. Linked List ─ Basics..................................................................................................................................42Linked List Representation ............................................................................................................................42Types of Linked List .......................................................................................................................................42Basic Operations............................................................................................................................................43Insertion Operation .......................................................................................................................................43Deletion Operation........................................................................................................................................44Reverse Operation.........................................................................................................................................45Linked List Program in C ................................................................................................................................4611. Doubly Linked List...................................................................................................................................55Doubly Linked List Representation................................................................................................................55Basic Operations............................................................................................................................................55Insertion Operation .......................................................................................................................................56Deletion Operation........................................................................................................................................57Insertion at the End of an Operation.............................................................................................................57Doubly Linked List Program in C....................................................................................................................5812. Circular Linked List..................................................................................................................................67Singly Linked List as Circular..........................................................................................................................67Doubly Linked List as Circular........................................................................................................................67Basic Operations............................................................................................................................................67Insertion Operation .......................................................................................................................................68Deletion Operation........................................................................................................................................68Display List Operation....................................................................................................................................69Circular Linked List Program in C...................................................................................................................69
  • 6.
    Data Structures &AlgorithmsvSTACK & QUEUE........................................................................................................................7413. Stack.......................................................................................................................................................75Stack Representation.....................................................................................................................................75Basic Operations............................................................................................................................................76peek().............................................................................................................................................................76isfull().............................................................................................................................................................77isempty()........................................................................................................................................................77Push Operation..............................................................................................................................................78Pop Operation ...............................................................................................................................................79Stack Program in C.........................................................................................................................................8114. Expression Parsing ..................................................................................................................................85Infix Notation.................................................................................................................................................85Prefix Notation ..............................................................................................................................................85Postfix Notation.............................................................................................................................................85Parsing Expressions .......................................................................................................................................86Postfix Evaluation Algorithm .........................................................................................................................87Expression Parsing Using Stack......................................................................................................................8715. Queue.....................................................................................................................................................93Queue Representation ..................................................................................................................................93Basic Operations............................................................................................................................................93peek().............................................................................................................................................................94isfull().............................................................................................................................................................94isempty()........................................................................................................................................................95Enqueue Operation .......................................................................................................................................96Dequeue Operation.......................................................................................................................................97Queue Program in C ......................................................................................................................................99SEARCHING TECHNIQUES........................................................................................................10316. Linear Search ........................................................................................................................................104Linear Search Program in C .........................................................................................................................10517. Binary Search........................................................................................................................................108How Binary Search Works? .........................................................................................................................108Binary Search Program in C .........................................................................................................................11118. Interpolation Search .............................................................................................................................115Positioning in Binary Search ........................................................................................................................115Position Probing in Interpolation Search.....................................................................................................116Interpolation Search Program in C ..............................................................................................................11819. Hash Table ............................................................................................................................................120Hashing........................................................................................................................................................120Linear Probing..............................................................................................................................................121Basic Operations..........................................................................................................................................122Data Item.....................................................................................................................................................122
  • 7.
    Data Structures &AlgorithmsviHash Method...............................................................................................................................................122Search Operation.........................................................................................................................................122Insert Operation ..........................................................................................................................................123Delete Operation.........................................................................................................................................124Hash Table Program in C .............................................................................................................................125SORTING TECHNIQUES............................................................................................................13020. Sorting Algorithm..................................................................................................................................131In-place Sorting and Not-in-place Sorting ...................................................................................................131Stable and Not Stable Sorting......................................................................................................................131Adaptive and Non-Adaptive Sorting Algorithm...........................................................................................132Important Terms..........................................................................................................................................13221. Bubble Sort Algorithm ..........................................................................................................................134How Bubble Sort Works?.............................................................................................................................134Bubble Sort Program in C ............................................................................................................................13822. Insertion Sort........................................................................................................................................142How Insertion Sort Works? .........................................................................................................................142Insertion Sort Program in C .........................................................................................................................14523. Selection Sort........................................................................................................................................149How Selection Sort Works? .........................................................................................................................149Selection Sort Program in C.........................................................................................................................15224. Merge Sort Algorithm ...........................................................................................................................155How Merge Sort Works? .............................................................................................................................155Merge Sort Program in C.............................................................................................................................15825. Shell Sort ..............................................................................................................................................160How Shell Sort Works? ................................................................................................................................160Shell Sort Program in C................................................................................................................................16426. Quick Sort .............................................................................................................................................168Partition in Quick Sort .................................................................................................................................168Quick Sort Pivot Algorithm ..........................................................................................................................168Quick Sort Pivot Pseudocode ......................................................................................................................169Quick Sort Algorithm ...................................................................................................................................169Quick Sort Pseudocode................................................................................................................................170Quick Sort Program in C ..............................................................................................................................170GRAPH DATA STRUCTURE.......................................................................................................17427. Graphs ..................................................................................................................................................175Graph Data Structure ..................................................................................................................................175Basic Operations..........................................................................................................................................177
  • 8.
    Data Structures &Algorithmsvii28. Depth First Traversal.............................................................................................................................178Depth First Traversal in C ............................................................................................................................18129. Breadth First Traversal..........................................................................................................................186Breadth First Traversal in C .........................................................................................................................188TREE DATA STRUCTURE ..........................................................................................................19430. Tree ......................................................................................................................................................195Important Terms..........................................................................................................................................195Binary Search Tree Representation.............................................................................................................196Tree Node....................................................................................................................................................196BST Basic Operations...................................................................................................................................197Insert Operation ..........................................................................................................................................197Search Operation.........................................................................................................................................199Tree Traversal in C.......................................................................................................................................20031. Tree Traversal .......................................................................................................................................206In-order Traversal........................................................................................................................................206Pre-order Traversal......................................................................................................................................207Post-order Traversal....................................................................................................................................208Tree Traversal in C.......................................................................................................................................20932. Binary Search Tree................................................................................................................................215Representation............................................................................................................................................215Basic Operations..........................................................................................................................................216Node............................................................................................................................................................216Search Operation.........................................................................................................................................216Insert Operation ..........................................................................................................................................21733. AVL Trees..............................................................................................................................................219AVL Rotations ..............................................................................................................................................22034. Spanning Tree.......................................................................................................................................224General Properties of Spanning Tree ..........................................................................................................224Mathematical Properties of Spanning Tree.................................................................................................225Application of Spanning Tree ......................................................................................................................225Minimum Spanning Tree (MST)...................................................................................................................225Minimum Spanning-Tree Algorithm............................................................................................................225Kruskal's Spanning Tree Algorithm..............................................................................................................226Prim's Spanning Tree Algorithm..................................................................................................................22935. Heaps....................................................................................................................................................233Max Heap Construction Algorithm..............................................................................................................234Max Heap Deletion Algorithm.....................................................................................................................235RECURSION.............................................................................................................................236
  • 9.
    Data Structures &Algorithmsviii36. Recursion ─ Basics.................................................................................................................................237Properties ....................................................................................................................................................237Implementation...........................................................................................................................................238Analysis of Recursion...................................................................................................................................238Time Complexity..........................................................................................................................................238Space Complexity ........................................................................................................................................23937. Tower of Hanoi .....................................................................................................................................240Rules ............................................................................................................................................................240Algorithm.....................................................................................................................................................244Tower of Hanoi in C .....................................................................................................................................24738. Fibonacci Series ....................................................................................................................................251Fibonacci Iterative Algorithm ......................................................................................................................252Fibonacci Interactive Program in C..............................................................................................................252Fibonacci Recursive Algorithm ....................................................................................................................254Fibonacci Recursive Program in C................................................................................................................254
  • 10.
    Data Structures &Algorithms1Basics
  • 11.
    Data Structures &Algorithms2Data Structure is a systematic way to organize data in order to use it efficiently. Followingterms are the foundation terms of a data structure. Interface − Each data structure has an interface. Interface represents the set ofoperations that a data structure supports. An interface only provides the list ofsupported operations, type of parameters they can accept and return type of theseoperations. Implementation − Implementation provides the internal representation of adata structure. Implementation also provides the definition of the algorithms usedin the operations of the data structure.CharacteristicsofaDataStructure Correctness − Data structure implementation should implement its interfacecorrectly. Time Complexity − Running time or the execution time of operations of datastructure must be as small as possible. Space Complexity − Memory usage of a data structure operation should be aslittle as possible.NeedforDataStructureAs applications are getting complex and data rich, there are three common problems thatapplications face now-a-days. Data Search − Consider an inventory of 1 million(106) items of a store. If theapplication is to search an item, it has to search an item in 1 million(106) itemsevery time slowing down the search. As data grows, search will become slower. Processor Speed − Processor speed although being very high, falls limited if thedata grows to billion records. Multiple Requests − As thousands of users can search data simultaneously on aweb server, even the fast server fails while searching the data.To solve the above-mentioned problems, data structures come to rescue. Data can beorganized in a data structure in such a way that all items may not be required to besearched, and the required data can be searched almost instantly.1. Overview
  • 12.
    Data Structures &Algorithms3ExecutionTimeCasesThere are three cases which are usually used to compare various data structure's executiontime in a relative manner. Worst Case − This is the scenario where a particular data structure operationtakes maximum time it can take. If an operation's worst case time is ƒ(n) thenthis operation will not take more than ƒ(n) time, where ƒ(n) represents functionof n. Average Case − This is the scenario depicting the average execution time of anoperation of a data structure. If an operation takes ƒ(n) time in execution, thenm operations will take mƒ(n) time. Best Case − This is the scenario depicting the least possible execution time of anoperation of a data structure. If an operation takes ƒ(n) time in execution, thenthe actual operation may take time as the random number which would bemaximum as ƒ(n).BasicTerminology Data − Data are values or set of values. Data Item − Data item refers to single unit of values. Group Items − Data items that are divided into sub items are called as GroupItems. Elementary Items − Data items that cannot be divided are called as ElementaryItems. Attribute and Entity − An entity is that which contains certain attributes orproperties, which may be assigned values. Entity Set − Entities of similar attributes form an entity set. Field − Field is a single elementary unit of information representing an attributeof an entity. Record − Record is a collection of field values of a given entity. File − File is a collection of records of the entities in a given entity set.
  • 13.
    Data Structures &Algorithms4TryitOptionOnlineYou really do not need to set up your own environment to start learning C programminglanguage. Reason is very simple, we already have set up C Programming environmentonline, so that you can compile and execute all the available examples online at the sametime when you are doing your theory work. This gives you confidence in what you arereading and to check the result with different options. Feel free to modify any exampleand execute it online.Try the following example using the Try it option available at the top right corner of thesample code box −#include <stdio.h>int main(){/* My first program in C */printf("Hello, World! n");return 0;}For most of the examples given in this tutorial, you will find Try it option, so just makeuse of it and enjoy your learning.LocalEnvironmentSetupIf you are still willing to set up your environment for C programming language, you needthe following two tools available on your computer, (a) Text Editor and (b) The C Compiler.Text EditorThis will be used to type your program. Examples of few editors include Windows Notepad,OS Edit command, Brief, Epsilon, EMACS, and vim or vi.The name and the version of the text editor can vary on different operating systems. Forexample, Notepad will be used on Windows, and vim or vi can be used on Windows as wellas Linux or UNIX.The files you create with your editor are called source files and contain program sourcecode. The source files for C programs are typically named with the extension ".c".Before starting your programming, make sure you have one text editor in place and youhave enough experience to write a computer program, save it in a file, compile it, andfinally execute it.2. Environment Setup
  • 14.
    Data Structures &Algorithms5The C CompilerThe source code written in the source file is the human readable source for your program.It needs to be "compiled", to turn into machine language so that your CPU can actuallyexecute the program as per the given instructions.This C programming language compiler will be used to compile your source code into afinal executable program. We assume you have the basic knowledge about a programminglanguage compiler.Most frequently used and free available compiler is GNU C/C++ compiler. Otherwise, youcan have compilers either from HP or Solaris if you have respective Operating Systems(OS).The following section guides you on how to install GNU C/C++ compiler on various OS.We are mentioning C/C++ together because GNU GCC compiler works for both C and C++programming languages.InstallationonUNIX/LinuxIf you are using Linux or UNIX, then check whether GCC is installed on your system byentering the following command from the command line −$ gcc -vIf you have GNU compiler installed on your machine, then it should print a message suchas the following −Using built-in specs.Target: i386-redhat-linuxConfigured with: ../configure --prefix=/usr .......Thread model: posixgcc version 4.1.2 20080704 (Red Hat 4.1.2-46)If GCC is not installed, then you will have to install it yourself using the detailed instructionsavailable at http://gcc.gnu.org/install/This tutorial has been written based on Linux and all the given examples have beencompiled on Cent OS flavor of Linux system.InstallationonMacOSIf you use Mac OS X, the easiest way to obtain GCC is to download the Xcode developmentenvironment from Apple's website and follow the simple installation instructions. Once youhave Xcode setup, you will be able to use GNU compiler for C/C++.Xcode is currently available at developer.apple.com/technologies/tools/
  • 15.
    Data Structures &Algorithms6InstallationonWindowsTo install GCC on Windows, you need to install MinGW. To install MinGW, go to the MinGWhomepage, www.mingw.org, and follow the link to the MinGW download page. Downloadthe latest version of the MinGW installation program, which should be named MinGW-<version>.exe.While installing MinWG, at a minimum, you must install gcc-core, gcc-g++, binutils, andthe MinGW runtime, but you may wish to install more.Add the bin subdirectory of your MinGW installation to your PATH environment variable,so that you can specify these tools on the command line by their simple names.When the installation is complete, you will be able to run gcc, g++, ar, ranlib, dlltool, andseveral other GNU tools from the Windows command line.
  • 16.
    Data Structures &Algorithms7Algorithm
  • 17.
    Data Structures &Algorithms8Algorithm is a step-by-step procedure, which defines a set of instructions to be executedin a certain order to get the desired output. Algorithms are generally created independentof underlying languages, i.e. an algorithm can be implemented in more than oneprogramming language.From the data structure point of view, following are some important categories ofalgorithms − Search − Algorithm to search an item in a data structure. Sort − Algorithm to sort items in a certain order. 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 a data structure.CharacteristicsofanAlgorithmNot all procedures can be called an algorithm. An algorithm should have the followingcharacteristics − Unambiguous − Algorithm should be clear and unambiguous. Each of its steps(or phases), and their inputs/outputs should be clear and must lead to only onemeaning. Input − An algorithm should have 0 or more well-defined inputs. Output − An algorithm should have 1 or more well-defined outputs, and shouldmatch the desired output. Finiteness − Algorithms must terminate after a finite number of steps. Feasibility − Should be feasible with the available resources. Independent − An algorithm should have step-by-step directions, which shouldbe independent of any programming code.3. Algorithms ─ Basics
  • 18.
    Data Structures &Algorithms9HowtoWriteanAlgorithm?There are no well-defined standards for writing algorithms. Rather, it is problem andresource dependent. Algorithms are never written to support a particular programmingcode.As we know that all programming languages share basic code constructs like loops(do, for, while), flow-control (if-else), etc. These common constructs can be used to writean algorithm.We write algorithms in a step-by-step manner, but it is not always the case. Algorithmwriting is a process and is executed after the problem domain is well-defined. That is, weshould know the problem domain, for which we are designing a solution.ExampleLet's try to learn algorithm-writing by using an example.Problem − Design an algorithm to add two numbers and display the result.step 1 − STARTstep 2 − declare three integers a, b & cstep 3 − define values of a & bstep 4 − add values of a & bstep 5 − store output of step 4 to cstep 6 − print cstep 7 − STOPAlgorithms tell the programmers how to code the program. Alternatively, the algorithmcan be written as −step 1 − START ADDstep 2 − get values of a & bstep 3 − c ← a + bstep 4 − display cstep 5 − STOPIn design and analysis of algorithms, usually the second method is used to describe analgorithm. It makes it easy for the analyst to analyze the algorithm ignoring all unwanteddefinitions. He can observe what operations are being used and how the process is flowing.Writing step numbers, is optional.We design an algorithm to get a solution of a given problem. A problem can be solved inmore than one ways.
  • 19.
    Data Structures &Algorithms10Hence, many solution algorithms can be derived for a given problem. The next step is toanalyze those proposed solution algorithms and implement the best suitable solution.AlgorithmAnalysisEfficiency of an algorithm can be analyzed at two different stages, before implementationand after implementation. They are the following − A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of analgorithm is measured by assuming that all other factors, for example, processorspeed, are constant and have no effect on the implementation. A Posterior Analysis − This is an empirical analysis of an algorithm. The selectedalgorithm is implemented using programming language. This is then executed ontarget computer machine. In this analysis, actual statistics like running time andspace required, are collected.We shall learn about a priori algorithm analysis. Algorithm analysis deals with theexecution or running time of various operations involved. The running time of an operationcan be defined as the number of computer instructions executed per operation.
  • 20.
    Data Structures &Algorithms11AlgorithmComplexitySuppose X is an algorithm and n is the size of input data, the time and space used by thealgorithm X are the two main factors, which decide the efficiency of X. Time Factor – Time is measured by counting the number of key operations suchas comparisons in the sorting algorithm. Space Factor − Space is measured by counting the maximum memory spacerequired by the algorithm.The complexity of an algorithm f(n) gives the running time and/or the storage spacerequired by the algorithm in terms of n as the size of input data.SpaceComplexitySpace complexity of an algorithm represents the amount of memory space required bythe algorithm in its life cycle. The space required by an algorithm is equal to the sum ofthe following two components − A fixed part that is a space required to store certain data and variables, that areindependent of the size of the problem. For example, simple variables andconstants used, program size, etc. A variable part is a space required by variables, whose size depends on the sizeof the problem. For example, dynamic memory allocation, recursion stack space,etc.Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed partand S(I) is the variable part of the algorithm, which depends on instance characteristic I.Following is a simple example that tries to explain the concept −Algorithm: SUM(A, B)Step 1 - STARTStep 2 - C ← A + B + 10Step 3 - StopHere we have three variables A, B, and C and one constant. Hence S(P) = 1+3. Now,space depends on data types of given variables and constant types and it will be multipliedaccordingly.TimeComplexityTime complexity of an algorithm represents the amount of time required by the algorithmto run to completion. Time requirements can be defined as a numerical function T(n),where T(n) can be measured as the number of steps, provided each step consumesconstant time.For example, addition of two n-bit integers takes n steps. Consequently, the totalcomputational time is T(n) = c*n, where c is the time taken for the addition of two bits.Here, we observe that T(n) grows linearly as the input size increases.
  • 21.
    Data Structures &Algorithms12Asymptotic analysis of an algorithm refers to defining the mathematicalboundation/framing of its run-time performance. Using asymptotic analysis, we can verywell conclude the best case, average case, and worst case scenario of an algorithm.Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concludedto work in a constant time. Other than the "input" all other factors are considered constant.Asymptotic analysis refers to computing the running time of any operation in mathematicalunits of computation. For example, the running time of one operation is computed as f(n)and may be for another operation it is computed as g(n2). This means the first operationrunning time will increase linearly with the increase in n and the running time of the secondoperation will increase exponentially when n increases. Similarly, the running time of bothoperations will be nearly the same if n is significantly small.Usually, the time required by an algorithm falls under three types − Best Case − Minimum time required for program execution. Average Case − Average time required for program execution. Worst Case − Maximum time required for program execution.AsymptoticNotationsFollowing are the commonly used asymptotic notations to calculate the running timecomplexity of an algorithm. Ο Notation Ω Notation θ NotationBig Oh Notation, ΟThe notation Ο(n) is the formal way to express the upper bound of an algorithm's runningtime. It measures the worst case time complexity or the longest amount of time analgorithm can possibly take to complete.4. Asymptotic Analysis
  • 22.
    Data Structures &Algorithms13For example, for a function f(n)Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n> n0. }Omega Notation, ΩThe notation Ω(n) is the formal way to express the lower bound of an algorithm's runningtime. It measures the best case time complexity or the best amount of time an algorithmcan possibly take to complete.
  • 23.
    Data Structures &Algorithms14For example, for a function f(n)Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n> n0. }Theta Notation, θThe notation θ(n) is the formal way to express both the lower bound and the upper boundof an algorithm's running time. It is represented as follows −θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n >n0. }
  • 24.
    Data Structures &Algorithms15CommonAsymptoticNotationsFollowing is a list of some common asymptotic notations:constant − Ο(1)logarithmic − Ο(log n)linear − Ο(n)n log n − Ο(n log n)quadratic − Ο(n2)cubic − Ο(n3)polynomial − nΟ(1)exponential − 2Ο(n)
  • 25.
    Data Structures &Algorithms16An algorithm is designed to achieve optimum solution for a given problem. In greedyalgorithm approach, decisions are made from the given solution domain. As being greedy,the closest solution that seems to provide an optimum solution is chosen.Greedy algorithms try to find a localized optimum solution, which may eventually lead toglobally optimized solutions. However, generally greedy algorithms do not provide globallyoptimized solutions.CountingCoinsThis problem is to count to a desired value by choosing the least possible coins and thegreedy approach forces the algorithm to pick the largest possible coin. If we are providedcoins of € 1, 2, 5 and 10 and we are asked to count € 18 then the greedy procedure willbe − 1 − Select one € 10 coin, the remaining count is 8 2 − Then select one € 5 coin, the remaining count is 3 3 − Then select one € 2 coin, the remaining count is 1 3 − And finally, the selection of one € 1 coins solves the problemThough, it seems to be working fine, for this count we need to pick only 4 coins. But if weslightly change the problem then the same approach may not be able to produce the sameoptimum result.For the currency system, where we have coins of 1, 7, 10 value, counting coins for value18 will be absolutely optimum but for count like 15, it may use more coins than necessary.For example, the greedy approach will use 10 + 1 + 1 + 1 + 1 + 1, total 6 coins. Whereasthe same problem could be solved by using only 3 coins (7 + 7 + 1)Hence, we may conclude that the greedy approach picks an immediate optimized solutionand may fail where global optimization is a major concern.5. Greedy Algorithms
  • 26.
    Data Structures &Algorithms17ExamplesMost networking algorithms use the greedy approach. Here is a list of few of them − Travelling Salesman Problem Prim's Minimal Spanning Tree Algorithm Kruskal's Minimal Spanning Tree Algorithm Dijkstra's Minimal Spanning Tree Algorithm Graph - Map Coloring Graph - Vertex Cover Knapsack Problem Job Scheduling ProblemThere are lots of similar problems that uses the greedy approach to find an optimumsolution.
  • 27.
    Data Structures &Algorithms18In divide and conquer approach, the problem in hand, is divided into smaller sub-problemsand then each problem is solved independently. When we keep on dividing the sub-problems into even smaller sub-problems, we may eventually reach a stage where nomore division is possible. Those "atomic" smallest possible sub-problem (fractions) aresolved. The solution of all sub-problems is finally merged in order to obtain the solution ofan original problem.Broadly, we can understand divide-and-conquer approach in a three-step process.Divide/BreakThis step involves breaking the problem into smaller sub-problems. Sub-problems shouldrepresent a part of the original problem. This step generally takes a recursive approach todivide the problem until no sub-problem is further divisible. At this stage, sub-problemsbecome atomic in nature but still represent some part of the actual problem.Conquer/SolveThis step receives a lot of smaller sub-problems to be solved. Generally, at this level, theproblems are considered 'solved' on their own.6. Divide &Conquer
  • 28.
    Data Structures &Algorithms19Merge/CombineWhen the smaller sub-problems are solved, this stage recursively combines them untilthey formulate a solution of the original problem. This algorithmic approach worksrecursively and conquer & merge steps works so close that they appear as one.ExamplesThe following computer algorithms are based on divide-and-conquer programmingapproach − Merge Sort Quick Sort Binary Search Strassen's Matrix Multiplication Closest Pair (points)There are various ways available to solve any computer problem, but the mentioned area good example of divide and conquer approach.
  • 29.
    Data Structures &Algorithms20Dynamic programming approach is similar to divide and conquer in breaking down theproblem into smaller and yet smaller possible sub-problems. But unlike, divide andconquer, these sub-problems are not solved independently. Rather, results of thesesmaller sub-problems are remembered and used for similar or overlapping sub-problems.Dynamic programming is used where we have problems, which can be divided into similarsub-problems, so that their results can be re-used. Mostly, these algorithms are used foroptimization. Before solving the in-hand sub-problem, dynamic algorithm will try toexamine the results of the previously solved sub-problems. The solutions of sub-problemsare combined in order to achieve the best solution.So we can say − The problem should be able to be divided into smaller overlapping sub-problem. An optimum solution can be achieved by using an optimum solution of smaller sub-problems. Dynamic algorithms use memorization.ComparisonIn contrast to greedy algorithms, where local optimization is addressed, dynamicalgorithms are motivated for an overall optimization of the problem.In contrast to divide and conquer algorithms, where solutions are combined to achieve anoverall solution, dynamic algorithms use the output of a smaller sub-problem and then tryto optimize a bigger sub-problem. Dynamic algorithms use memorization to remember theoutput of already solved sub-problems.ExampleThe following computer problems can be solved using dynamic programming approach − Fibonacci number series Knapsack problem Tower of Hanoi All pair shortest path by Floyd-Warshall Shortest path by Dijkstra Project schedulingDynamic programming can be used in both top-down and bottom-up manner. And ofcourse, most of the times, referring to the previous solution output is cheaper than re-computing in terms of CPU cycles.7. Dynamic Programming
  • 30.
    Data Structures &Algorithms21Data Structures
  • 31.
    Data Structures &Algorithms22This chapter explains the basic terms related to data structure.DataDefinitionData Definition defines a particular data with the following characteristics. Atomic − Definition should define a single concept. Traceable − Definition should be able to be mapped to some data element. Accurate − Definition should be unambiguous. Clear and Concise − Definition should be understandable.DataObjectData Object represents an object having a data.DataTypeData type is a way to classify various types of data such as integer, string, etc. whichdetermines the values that can be used with the corresponding type of data, the type ofoperations that can be performed on the corresponding type of data. There are two datatypes − Built-in Data Type Derived Data TypeBuilt-in Data TypeThose data types for which a language has built-in support are known as Built-in Datatypes. For example, most of the languages provide the following built-in data types. Integers Boolean (true, false) Floating (Decimal numbers) Character and Strings8. Basic Concepts
  • 32.
    Data Structures &Algorithms23Derived Data TypeThose data types which are implementation independent as they can be implemented inone or the other way are known as derived data types. These data types are normally builtby the combination of primary or built-in data types and associated operations on them.For example − List Array Stack QueueBasicOperationsThe data in the data structures are processed by certain operations. The particular datastructure chosen largely depends on the frequency of the operation that needs to beperformed on the data structure. Traversing Searching Insertion Deletion Sorting Merging
  • 33.
    Data Structures &Algorithms24Array is a container which can hold a fix number of items and these items should be of thesame type. Most of the data structures make use of arrays to implement their algorithms.Following are the important terms to understand the concept of Array. Element − Each item stored in an array is called an element. Index − Each location of an element in an array has a numerical index, which isused to identify the element.ArrayRepresentationArrays can be declared in various ways in different languages. For illustration, let's take Carray declaration.Arrays can be declared in various ways in different languages. For illustration, let's take Carray declaration.As per the above illustration, following are the important points to be considered. Index starts with 0. Array length is 8 which means it can store 8 elements. Each element can be accessed via its index. For example, we can fetch an elementat index 6 as 9.9. Arrays
  • 34.
    Data Structures &Algorithms25BasicOperationsFollowing are the basic operations supported by an array. Traverse − Prints all the array elements one by one. Insertion − Adds an element at the given index. Deletion − Deletes an element at the given index. Search − Searches an element using the given index or by the value. Update − Updates an element at the given index.In C, when an array is initialized with size, then it assigns defaults values to its elementsin following order.Data Type Default Valuebool falsechar 0int 0float 0.0double 0.0fvoidwchar_t 0InsertionOperationInsert operation is to insert one or more data elements into an array. Based on therequirement, a new element can be added at the beginning, end, or any given index ofarray.Here, we see a practical implementation of insertion operation, where we add data at theend of the array −AlgorithmLet Array be a linear unordered array of MAX elements.
  • 35.
    Data Structures &Algorithms26ExampleResultLet LA be a Linear Array (unordered) with N elements and K is a positive integer suchthat K<=N. Following is the algorithm where ITEM is inserted into the Kthposition of LA −1. Start2. Set J=N3. Set N = N+14. Repeat steps 5 and 6 while J >= K5. Set LA[J+1] = LA[J]6. Set J = J-17. Set LA[K] = ITEM8. StopExampleFollowing is the implementation of the above algorithm −#include <stdio.h>main() {int LA[] = {1,3,5,7,8};int item = 10, k = 3, n = 5;int i = 0, j = n;printf("The original array elements are :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}n = n + 1;while( j >= k){LA[j+1] = LA[j];j = j - 1;}
  • 36.
    Data Structures &Algorithms27LA[k] = item;printf("The array elements after insertion :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}}When we compile and execute the above program, it produces the following result −The original array elements are :LA[0]=1LA[1]=3LA[2]=5LA[3]=7LA[4]=8The array elements after insertion :LA[0]=1LA[1]=3LA[2]=5LA[3]=10LA[4]=7LA[5]=8For other variations of array insertion operation click hereArrayInsertionsIn the previous section, we have learnt how the insertion operation works. It is not alwaysnecessary that an element is inserted at the end of an array. Following can be a situationwith array insertion − Insertion at the beginning of an array Insertion at the given index of an array Insertion after the given index of an array Insertion before the given index of an array
  • 37.
    Data Structures &Algorithms28InsertionattheBeginningofanArrayWhen the insertion happens at the beginning, it causes all the existing data items to shiftone step downward. Here, we design and implement an algorithm to insert an element atthe beginning of an array.AlgorithmWe assume A is an array with N elements. The maximum numbers of elements it canstore is defined by MAX. We shall first check if an array has any empty space to store anyelement and then we proceed with the insertion process.beginIF N = MAX, returnELSEN = N + 1For All Elements in AMove to next adjacent locationA[FIRST] = New_ElementendImplementation in C#include <stdio.h>#define MAX 5void main() {int array[MAX] = {2, 3, 4, 5};int N = 4; // number of elements in arrayint i = 0; // loop variableint value = 1; // new data element to be stored in array// print array before insertionprintf("Printing array before insertion −n");
  • 38.
    Data Structures &Algorithms29for(i = 0; i < N; i++) {printf("array[%d] = %d n", i, array[i]);}// now shift rest of the elements downwardsfor(i = N; i >= 0; i--) {array[i+1] = array[i];}// add new element at first positionarray[0] = value;// increase N to reflect number of elementsN++;// print to confirmprintf("Printing array after insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %dn", i, array[i]);}}This program should yield the following output −Printing array before insertion −array[0] = 2array[1] = 3array[2] = 4array[3] = 5Printing array after insertion −array[0] = 0array[1] = 2array[2] = 3array[3] = 4array[4] = 5
  • 39.
    Data Structures &Algorithms30InsertionattheGivenIndexofanArrayIn this scenario, we are given the exact location (index) of an array where a new dataelement (value) needs to be inserted. First we shall check if the array is full, if it is not,then we shall move all data elements from that location one step downward. This will makeroom for a new data element.AlgorithmWe assume A is an array with N elements. The maximum numbers of elements it canstore is defined by MAX.beginIF N = MAX, returnELSEN = N + 1SEEK Location indexFor All Elements from A[index] to A[N]Move to next adjacent locationA[index] = New_ElementendImplementation in C#include <stdio.h>#define MAX 5void main() {int array[MAX] = {1, 2, 4, 5};int N = 4; // number of elements in arrayint i = 0; // loop variableint index = 2; // index location to insert new valueint value = 3; // new data element to be inserted// print array before insertionprintf("Printing array before insertion −n");
  • 40.
    Data Structures &Algorithms31for(i = 0; i < N; i++) {printf("array[%d] = %d n", i, array[i]);}// now shift rest of the elements downwardsfor(i = N; i >= index; i--) {array[i+1] = array[i];}// add new element at first positionarray[index] = value;// increase N to reflect number of elementsN++;// print to confirmprintf("Printing array after insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %dn", i, array[i]);}}If we compile and run the above program, it will produce the following result −Printing array before insertion −array[0] = 1array[1] = 2array[2] = 4array[3] = 5Printing array after insertion −array[0] = 1array[1] = 2array[2] = 3array[3] = 4array[4] = 5
  • 41.
    Data Structures &Algorithms32InsertionAftertheGivenIndexofanArrayIn this scenario we are given a location (index) of an array after which a new data element(value) has to be inserted. Only the seek process varies, the rest of the activities are thesame as in the previous example.AlgorithmWe assume A is an array with N elements. The maximum numbers of elements it canstore is defined by MAX.beginIF N = MAX, returnELSEN = N + 1SEEK Location indexFor All Elements from A[index + 1] to A[N]Move to next adjacent locationA[index + 1] = New_ElementendImplementation in C#include <stdio.h>#define MAX 5void main() {int array[MAX] = {1, 2, 4, 5};int N = 4; // number of elements in arrayint i = 0; // loop variableint index = 1; // index location after which value will be insertedint value = 3; // new data element to be inserted// print array before insertionprintf("Printing array before insertion −n");
  • 42.
    Data Structures &Algorithms33for(i = 0; i < N; i++) {printf("array[%d] = %d n", i, array[i]);}// now shift rest of the elements downwardsfor(i = N; i >= index + 1; i--) {array[i + 1] = array[i];}// add new element at first positionarray[index + 1] = value;// increase N to reflect number of elementsN++;// print to confirmprintf("Printing array after insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %dn", i, array[i]);}}If we compile and run the above program, it will produce the following result −Printing array before insertion −array[0] = 1array[1] = 2array[2] = 4array[3] = 5Printing array after insertion −array[0] = 1array[1] = 2array[2] = 3array[3] = 4
  • 43.
    Data Structures &Algorithms34array[4] = 5InsertionBeforetheGivenIndexofanArrayIn this scenario we are given a location (index) of an array before which a new dataelement (value) has to be inserted. This time we seek till index-1, i.e., one locationahead of the given index. Rest of the activities are the same as in the previous example.AlgorithmWe assume A is an array with N elements. The maximum numbers of elements it canstore is defined by MAX.beginIF N = MAX, returnELSEN = N + 1SEEK Location indexFor All Elements from A[index - 1] to A[N]Move to next adjacent locationA[index - 1] = New_ElementendImplementation in C#include <stdio.h>#define MAX 5
  • 44.
    Data Structures &Algorithms35void main() {int array[MAX] = {1, 2, 4, 5};int N = 4; // number of elements in arrayint i = 0; // loop variableint index = 3; // index location before which value will be insertedint value = 3; // new data element to be inserted// print array before insertionprintf("Printing array before insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %d n", i, array[i]);}// now shift rest of the elements downwardsfor(i = N; i >= index + 1; i--) {array[i + 1] = array[i];}// add new element at first positionarray[index + 1] = value;// increase N to reflect number of elementsN++;// print to confirmprintf("Printing array after insertion −n");for(i = 0; i < N; i++) {printf("array[%d] = %dn", i, array[i]);}}If we compile and run the above program, it will produce the following result −Printing array before insertion −array[0] = 1array[1] = 2
  • 45.
    Data Structures &Algorithms36array[2] = 4array[3] = 5Printing array after insertion −array[0] = 1array[1] = 2array[2] = 4array[3] = 5array[4] = 3DeletionOperationDeletion refers to removing an existing element from the array and re-organizing allelements of an array.AlgorithmConsider LA is a linear array with N elements and K is a positive integer such that K<=N.Following is the algorithm to delete an element available at the Kthposition of LA.1. Start2. Set J=K3. Repeat steps 4 and 5 while J < N4. Set LA[J-1] = LA[J]5. Set J = J+16. Set N = N-17. StopExampleFollowing is the implementation of the above algorithm −#include <stdio.h>main() {int LA[] = {1,3,5,7,8};int k = 3, n = 5;int i, j;printf("The original array elements are :n");
  • 46.
    Data Structures &Algorithms37for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}j = k;while( j < n){LA[j-1] = LA[j];j = j + 1;}n = n -1;printf("The array elements after deletion :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}}When we compile and execute the above program, it produces the following result −The original array elements are :LA[0]=1LA[1]=3LA[2]=5LA[3]=7LA[4]=8The array elements after deletion :LA[0]=1LA[1]=3LA[2]=7LA[3]=8SearchOperationYou can perform a search for an array element based on its value or its index.Algorithm
  • 47.
    Data Structures &Algorithms38Consider LA is a linear array with N elements and K is a positive integer such that K<=N.Following is the algorithm to find an element with a value of ITEM using sequential search.1. Start2. Set J=03. Repeat steps 4 and 5 while J < N4. IF LA[J] is equal ITEM THEN GOTO STEP 65. Set J = J +16. PRINT J, ITEM7. StopExampleFollowing is the implementation of the above algorithm −#include <stdio.h>main() {int LA[] = {1,3,5,7,8};int item = 5, n = 5;int i = 0, j = 0;printf("The original array elements are :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}while( j < n){if( LA[j] == item ){break;}j = j + 1;}printf("Found element %d at position %dn", item, j+1);}When we compile and execute the above program, it produces the following result −
  • 48.
    Data Structures &Algorithms39The original array elements are :LA[0]=1LA[1]=3LA[2]=5LA[3]=7LA[4]=8Found element 5 at position 3UpdateOperationUpdate operation refers to updating an existing element from the array at a given index.AlgorithmConsider LA is a linear array with N elements and K is a positive integer such that K<=N.Following is the algorithm to update an element available at the Kthposition of LA.1. Start2. Set LA[K-1] = ITEM3. StopExampleFollowing is the implementation of the above algorithm −#include <stdio.h>main() {int LA[] = {1,3,5,7,8};int k = 3, n = 5, item = 10;int i, j;printf("The original array elements are :n");for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}LA[k-1] = item;printf("The array elements after updation :n");
  • 49.
    Data Structures &Algorithms40for(i = 0; i<n; i++) {printf("LA[%d] = %d n", i, LA[i]);}}When we compile and execute the above program, it produces the following result −The original array elements are :LA[0]=1LA[1]=3LA[2]=5LA[3]=7LA[4]=8The array elements after updation :LA[0]=1LA[1]=3LA[2]=10LA[3]=7LA[4]=8
  • 50.
    Data Structures &Algorithms41Linked List
  • 51.
    Data Structures &Algorithms42A linked list is a sequence of data structures, which are connected together via links.Linked List is a sequence of links which contains items. Each link contains a connection toanother link. Linked list is the second most-used data structure after array. Following arethe important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. Linked List − A Linked List contains the connection link to the first link calledFirst.LinkedListRepresentationLinked list can be visualized as a chain of nodes, where every node points to the nextnode.As per the above illustration, following are the important points to be considered. Linked List contains a link element called first. Each link carries a data field(s) and a link field called next. Each link is linked with its next link using its next link. Last link carries a link as null to mark the end of the list.TypesofLinkedListFollowing are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next andthe first element has a link to the last element as previous.10. Linked List ─ Basics
  • 52.
    Data Structures &Algorithms43BasicOperationsFollowing are the basic operations supported by a list. Insertion − Adds an element at the beginning of the list. Deletion − Deletes an element at the beginning of the list. Display − Displays the complete list. Search − Searches an element using the given key. Delete − Deletes an element using the given key.InsertionOperationAdding a new node in linked list is a more than one step activity. We shall learn this withdiagrams here. First, create a node using the same structure and find the location whereit has to be inserted.Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C(RightNode). Then point B.next to C -NewNode.next −> RightNode;It should look like this −
  • 53.
    Data Structures &Algorithms44Now, the next node at the left should point to the new node.LeftNode.next −> NewNode;This will put the new node in the middle of the two. The new list should look like this −Similar steps should be taken if the node is being inserted at the beginning of the list.While inserting it at the end, the second last node of the list should point to the new nodeand the new node will point to NULL.DeletionOperationDeletion is also a more than one step process. We shall learn with pictorial representation.First, locate the target node to be removed, by using searching algorithms.The left (previous) node of the target node now should point to the next node of the targetnode −LeftNode.next −> TargetNode.next;
  • 54.
    Data Structures &Algorithms45This will remove the link that was pointing to the target node. Now, using the followingcode, we will remove what the target node is pointing at.TargetNode.next −> NULL;We need to use the deleted node. We can keep that in memory otherwise we can simplydeallocate memory and wipe off the target node completely.ReverseOperationThis operation is a thorough one. We need to make the last node to be pointed by thehead node and reverse the whole linked list.First, we traverse to the end of the list. It should be pointing to NULL. Now, we shall makeit point to its previous node −
  • 55.
    Data Structures &Algorithms46We have to make sure that the last node is not the lost node. So we'll have some tempnode, which looks like the head node pointing to the last node. Now, we shall make all leftside nodes point to their previous nodes one by one.Except the node (first node) pointed by the head node, all nodes should point to theirpredecessor, making them their new successor. The first node will point to NULL.We'll make the head node point to the new first node by using the temp node.The linked list is now reversed. To see linked list implementation in C programminglanguage, please click here.LinkedListPrograminCA linked list is a sequence of data structures, which are connected together via links.Linked List is a sequence of links which contains items. Each link contains a connection toanother link. Linked list is the second most-used data structure after array.
  • 56.
    Data Structures &Algorithms47Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>struct node{int data;int key;struct node *next;};struct node *head = NULL;struct node *current = NULL;//display the listvoid printList(){struct node *ptr = head;printf("n[ ");//start from the beginningwhile(ptr != NULL){printf("(%d,%d) ",ptr->key,ptr->data);ptr = ptr->next;}printf(" ]");}//insert link at the first locationvoid insertFirst(int key, int data){//create a link
  • 57.
    Data Structures &Algorithms48struct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;//point it to old first nodelink->next = head;//point first to new first nodehead = link;}//delete first itemstruct node* deleteFirst(){//save reference to first linkstruct node *tempLink = head;//mark next to first link as firsthead = head->next;//return the deleted linkreturn tempLink;}//is list emptybool isEmpty(){return head == NULL;}int length(){int length = 0;struct node *current;
  • 58.
    Data Structures &Algorithms49for(current = head; current != NULL; current = current->next){length++;}return length;}//find a link with given keystruct node* find(int key){//start from the first linkstruct node* current = head;//if list is emptyif(head == NULL){return NULL;}//navigate through listwhile(current->key != key){//if it is last nodeif(current->next == NULL){return NULL;}else {//go to next linkcurrent = current->next;}}//if data found, return the current Linkreturn current;}
  • 59.
    Data Structures &Algorithms50//delete a link with given keystruct node* delete(int key){//start from the first linkstruct node* current = head;struct node* previous = NULL;//if list is emptyif(head == NULL){return NULL;}//navigate through listwhile(current->key != key){//if it is last nodeif(current->next == NULL){return NULL;}else {//store reference to current linkprevious = current;//move to next linkcurrent = current->next;}}//found a match, update the linkif(current == head) {//change first to point to next linkhead = head->next;}else {//bypass the current linkprevious->next = current->next;}return current;}
  • 60.
    Data Structures &Algorithms51void sort(){int i, j, k, tempKey, tempData ;struct node *current;struct node *next;int size = length();k = size ;for ( i = 0 ; i < size - 1 ; i++, k-- ) {current = head ;next = head->next ;for ( j = 1 ; j < k ; j++ ) {if ( current->data > next->data ) {tempData = current->data ;current->data = next->data;next->data = tempData ;tempKey = current->key;current->key = next->key;next->key = tempKey;}current = current->next;next = next->next;}}}void reverse(struct node** head_ref) {struct node* prev = NULL;struct node* current = *head_ref;struct node* next;
  • 61.
    Data Structures &Algorithms52while (current != NULL) {next = current->next;current->next = prev;prev = current;current = next;}*head_ref = prev;}main() {insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);insertFirst(5,40);insertFirst(6,56);printf("Original List: ");//print listprintList();while(!isEmpty()){struct node *temp = deleteFirst();printf("nDeleted value:");printf("(%d,%d) ",temp->key,temp->data);}printf("nList after deleting all items: ");printList();insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);
  • 62.
    Data Structures &Algorithms53insertFirst(5,40);insertFirst(6,56);printf("nRestored List: ");printList();printf("n");struct node *foundLink = find(4);if(foundLink != NULL){printf("Element found: ");printf("(%d,%d) ",foundLink->key,foundLink->data);printf("n");}else {printf("Element not found.");}delete(4);printf("List after deleting an item: ");printList();printf("n");foundLink = find(4);if(foundLink != NULL){printf("Element found: ");printf("(%d,%d) ",foundLink->key,foundLink->data);printf("n");}else {printf("Element not found.");}printf("n");sort();printf("List after sorting the data: ");printList();reverse(&head);
  • 63.
    Data Structures &Algorithms54printf("nList after reversing the data: ");printList();}If we compile and run the above program, it will produce the following result −Original List:[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]Deleted value:(6,56)Deleted value:(5,40)Deleted value:(4,1)Deleted value:(3,30)Deleted value:(2,20)Deleted value:(1,10)List after deleting all items:[ ]Restored List:[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]Element found: (4,1)List after deleting an item:[ (6,56) (5,40) (3,30) (2,20) (1,10) ]Element not found.List after sorting the data:[ (1,10) (2,20) (3,30) (5,40) (6,56) ]List after reversing the data:[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
  • 64.
    Data Structures &Algorithms55Doubly Linked List is a variation of Linked list in which navigation is possible in both ways,either forward and backward easily as compared to Single Linked List. Following are theimportant terms to understand the concept of doubly linked list. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. Prev − Each link of a linked list contains a link to the previous link called Prev. Linked List − A Linked List contains the connection link to the first link calledFirst and to the last link called Last.DoublyLinkedListRepresentationAs per the above illustration, following are the important points to be considered. Doubly Linked List contains a link element called first and last. Each link carries a data field(s) and a link field called next. Each link is linked with its next link using its next link. Each link is linked with its previous link using its previous link. The last link carries a link as null to mark the end of the list.BasicOperationsFollowing are the basic operations supported by a list. Insertion − Adds an element at the beginning of the list. Deletion − Deletes an element at the beginning of the list. Insert Last − Adds an element at the end of the list. Delete Last − Deletes an element from the end of the list.11. Doubly Linked List
  • 65.
    Data Structures &Algorithms56 Insert After − Adds an element after an item of the list. Delete − Deletes an element from the list using the key. Display forward − Displays the complete list in a forward manner. Display backward − Displays the complete list in a backward manner.InsertionOperationFollowing code demonstrates the insertion operation at the beginning of a doubly linkedlist.//insert link at the first locationvoid insertFirst(int key, int data) {//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;if(isEmpty()) {//make it the last linklast = link;}else {//update first prev linkhead->prev = link;}//point it to old first linklink->next = head;//point first to new first linkhead = link;}
  • 66.
    Data Structures &Algorithms57DeletionOperationFollowing code demonstrates the deletion operation at the beginning of a doubly linkedlist.//delete first itemstruct node* deleteFirst() {//save reference to first linkstruct node *tempLink = head;//if only one linkif(head->next == NULL) {last = NULL;}else {head->next->prev = NULL;}head = head->next;//return the deleted linkreturn tempLink;}InsertionattheEndofanOperationFollowing code demonstrates the insertion operation at the last position of a doubly linkedlist.//insert link at the last locationvoid insertLast(int key, int data) {//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;
  • 67.
    Data Structures &Algorithms58if(isEmpty()) {//make it the last linklast = link;}else {//make link a new last linklast->next = link;//mark old last node as prev of new linklink->prev = last;}//point last to new last nodelast = link;}To see the implementation in C programming language, please click here.DoublyLinkedListPrograminCDoubly Linked List is a variation of Linked list in which navigation is possible in both ways,either forward and backward easily as compared to Single Linked List.Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>struct node {int data;int key;struct node *next;struct node *prev;};
  • 68.
    Data Structures &Algorithms59//this link always point to first Linkstruct node *head = NULL;//this link always point to last Linkstruct node *last = NULL;struct node *current = NULL;//is list emptybool isEmpty(){return head == NULL;}int length(){int length = 0;struct node *current;for(current = head; current != NULL; current = current->next){length++;}return length;}//display the list in from first to lastvoid displayForward(){//start from the beginningstruct node *ptr = head;//navigate till the end of the listprintf("n[ ");while(ptr != NULL){printf("(%d,%d) ",ptr->key,ptr->data);ptr = ptr->next;}
  • 69.
    Data Structures &Algorithms60printf(" ]");}//display the list from last to firstvoid displayBackward(){//start from the laststruct node *ptr = last;//navigate till the start of the listprintf("n[ ");while(ptr != NULL){//print dataprintf("(%d,%d) ",ptr->key,ptr->data);//move to next itemptr = ptr ->prev;printf(" ");}printf(" ]");}//insert link at the first locationvoid insertFirst(int key, int data){//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;if(isEmpty()){//make it the last linklast = link;
  • 70.
    Data Structures &Algorithms61}else {//update first prev linkhead->prev = link;}//point it to old first linklink->next = head;//point first to new first linkhead = link;}//insert link at the last locationvoid insertLast(int key, int data){//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;if(isEmpty()){//make it the last linklast = link;}else {//make link a new last linklast->next = link;//mark old last node as prev of new linklink->prev = last;}//point last to new last nodelast = link;}
  • 71.
    Data Structures &Algorithms62//delete first itemstruct node* deleteFirst(){//save reference to first linkstruct node *tempLink = head;//if only one linkif(head->next == NULL){last = NULL;}else {head->next->prev = NULL;}head = head->next;//return the deleted linkreturn tempLink;}//delete link at the last locationstruct node* deleteLast(){//save reference to last linkstruct node *tempLink = last;//if only one linkif(head->next == NULL){head = NULL;}else {last->prev->next = NULL;}last = last->prev;//return the deleted linkreturn tempLink;}
  • 72.
    Data Structures &Algorithms63//delete a link with given keystruct node* delete(int key){//start from the first linkstruct node* current = head;struct node* previous = NULL;//if list is emptyif(head == NULL){return NULL;}//navigate through listwhile(current->key != key){//if it is last nodeif(current->next == NULL){return NULL;}else {//store reference to current linkprevious = current;//move to next linkcurrent = current->next;}}//found a match, update the linkif(current == head) {//change first to point to next linkhead = head->next;}else {//bypass the current link
  • 73.
    Data Structures &Algorithms64current->prev->next = current->next;}if(current == last){//change last to point to prev linklast = current->prev;}else {current->next->prev = current->prev;}return current;}bool insertAfter(int key, int newKey, int data){//start from the first linkstruct node *current = head;//if list is emptyif(head == NULL){return false;}//navigate through listwhile(current->key != key){//if it is last nodeif(current->next == NULL){return false;}else {//move to next linkcurrent = current->next;}}//create a linkstruct node *newLink = (struct node*) malloc(sizeof(struct node));newLink->key = key;
  • 74.
    Data Structures &Algorithms65newLink->data = data;if(current == last) {newLink->next = NULL;last = newLink;}else {newLink->next = current->next;current->next->prev = newLink;}newLink->prev = current;current->next = newLink;return true;}main() {insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);insertFirst(5,40);insertFirst(6,56);printf("nList (First to Last): ");displayForward();printf("n");printf("nList (Last to first): ");displayBackward();printf("nList , after deleting first record: ");deleteFirst();displayForward();printf("nList , after deleting last record: ");
  • 75.
    Data Structures &Algorithms66deleteLast();displayForward();printf("nList , insert after key(4) : ");insertAfter(4,7, 13);displayForward();printf("nList , after delete key(4) : ");delete(4);displayForward();}If we compile and run the above program, it will produce the following result −List (First to Last):[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]List (Last to first):[ (1,10) (2,20) (3,30) (4,1) (5,40) (6,56) ]List , after deleting first record:[ (5,40) (4,1) (3,30) (2,20) (1,10) ]List , after deleting last record:[ (5,40) (4,1) (3,30) (2,20) ]List , insert after key(4) :[ (5,40) (4,1) (4,13) (3,30) (2,20) ]List , after delete key(4) :[ (5,40) (4,13) (3,30) (2,20) ]
  • 76.
    Data Structures &Algorithms67Circular Linked List is a variation of Linked list in which the first element points to the lastelement and the last element points to the first element. Both Singly Linked List andDoubly Linked List can be made into a circular linked list.SinglyLinkedListasCircularIn singly linked list, the next pointer of the last node points to the first node.DoublyLinkedListasCircularIn doubly linked list, the next pointer of the last node points to the first node and theprevious pointer of the first node points to the last node making the circular in bothdirections.As per the above illustration, following are the important points to be considered. The last link's next points to the first link of the list in both cases of singly as wellas doubly linked list. The first link's previous points to the last of the list in case of doubly linked list.BasicOperationsFollowing are the important operations supported by a circular list. insert − Inserts an element at the start of the list. delete – Deletes an element from the start of the list. display − Displays the list.12. Circular Linked List
  • 77.
    Data Structures &Algorithms68InsertionOperationFollowing code demonstrates the insertion operation in a circular linked list based on singlelinked list.//insert link at the first locationvoid insertFirst(int key, int data) {//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data= data;if (isEmpty()) {head = link;head->next = head;}else {//point it to old first nodelink->next = head;//point first to new first nodehead = link;}}DeletionOperationFollowing code demonstrates the deletion operation in a circular linked list based on singlelinked list.//delete first itemstruct node * deleteFirst() {//save reference to first linkstruct node *tempLink = head;if(head->next == head){head = NULL;return tempLink;}
  • 78.
    Data Structures &Algorithms69//mark next to first link as firsthead = head->next;//return the deleted linkreturn tempLink;}DisplayListOperationFollowing code demonstrates the display list operation in a circular linked list.//display the listvoid printList() {struct node *ptr = head;printf("n[ ");//start from the beginningif(head != NULL) {while(ptr->next != ptr) {printf("(%d,%d) ",ptr->key,ptr->data);ptr = ptr->next;}}printf(" ]");}To know about its implementation in C programming language, please click here.CircularLinkedListPrograminCCircular Linked List is a variation of Linked list in which the first element points to the lastelement and the last element points to the first element. Both Singly Linked List andDoubly Linked List can be made into a circular linked list.
  • 79.
    Data Structures &Algorithms70Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>struct node {int data;int key;struct node *next;};struct node *head = NULL;struct node *current = NULL;bool isEmpty(){return head == NULL;}int length(){int length = 0;//if list is emptyif(head == NULL){return 0;}current = head->next;while(current != head){length++;current = current->next;}
  • 80.
    Data Structures &Algorithms71return length;}//insert link at the first locationvoid insertFirst(int key, int data){//create a linkstruct node *link = (struct node*) malloc(sizeof(struct node));link->key = key;link->data = data;if (isEmpty()) {head = link;head->next = head;}else {//point it to old first nodelink->next = head;//point first to new first nodehead = link;}}//delete first itemstruct node * deleteFirst(){//save reference to first linkstruct node *tempLink = head;if(head->next == head){head = NULL;return tempLink;}
  • 81.
    Data Structures &Algorithms72//mark next to first link as firsthead = head->next;//return the deleted linkreturn tempLink;}//display the listvoid printList(){struct node *ptr = head;printf("n[ ");//start from the beginningif(head != NULL){while(ptr->next != ptr){printf("(%d,%d) ",ptr->key,ptr->data);ptr = ptr->next;}}printf(" ]");}main() {insertFirst(1,10);insertFirst(2,20);insertFirst(3,30);insertFirst(4,1);insertFirst(5,40);insertFirst(6,56);
  • 82.
    Data Structures &Algorithms73printf("Original List: ");//print listprintList();while(!isEmpty()){struct node *temp = deleteFirst();printf("nDeleted value:");printf("(%d,%d) ",temp->key,temp->data);}printf("nList after deleting all items: ");printList();}If we compile and run the above program, it will produce the following result −Original List:[ (6,56) (5,40) (4,1) (3,30) (2,20) ]Deleted value:(6,56)Deleted value:(5,40)Deleted value:(4,1)Deleted value:(3,30)Deleted value:(2,20)Deleted value:(1,10)List after deleting all items:[ ]
  • 83.
    Data Structures &Algorithms74Stack & Queue
  • 84.
    Data Structures &Algorithms75A stack is an Abstract Data Type (ADT), commonly used in most programming languages.It is named stack as it behaves like a real-world stack, for example – a deck of cards or apile of plates, etc.A real-world stack allows operations at one end only. For example, we can place or removea card or plate from the top of the stack only. Likewise, Stack ADT allows all dataoperations at one end only. At any given time, we can only access the top element of astack.This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, theelement which is placed (inserted or added) last, is accessed first. In stack terminology,insertion operation is called PUSH operation and removal operation iscalled POP operation.StackRepresentationThe following diagram depicts a stack and its operations −A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stackcan either be a fixed size one or it may have a sense of dynamic resizing. Here, we aregoing to implement stack using arrays, which makes it a fixed size stack implementation.13. Stack
  • 85.
    Data Structures &Algorithms76BasicOperationsStack operations may involve initializing the stack, using it and then de-initializing it. Apartfrom these basic stuffs, a stack is used for the following two primary operations − push() − Pushing (storing) an element on the stack. pop() − Removing (accessing) an element from the stack.When data is PUSHed onto stack.To use a stack efficiently, we need to check the status of stack as well. For the samepurpose, the following functionality is added to stacks − peek() − get the top data element of the stack, without removing it. isFull() − check if stack is full. isEmpty() − check if stack is empty.At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointeralways represents the top of the stack, hence named top. The top pointer provides topvalue of the stack without actually removing it.First we should learn about procedures to support stack functions −peek()Algorithm of peek() function −begin procedure peekreturn stack[top]end procedureImplementation of peek() function in C programming language −int peek() {return stack[top];}
  • 86.
    Data Structures &Algorithms77isfull()Algorithm of isfull() function −begin procedure isfullif top equals to MAXSIZEreturn trueelsereturn falseendifend procedureImplementation of isfull() function in C programming language −bool isfull() {if(top == MAXSIZE)return true;elsereturn false;}isempty()Algorithm of isempty() function −begin procedure isemptyif top less than 1return trueelsereturn falseendifend procedure
  • 87.
    Data Structures &Algorithms78Implementation of isempty() function in C programming language is slightly different. Weinitialize top at -1, as the index in array starts from 0. So we check if the top is below zeroor -1 to determine if the stack is empty. Here's the code −bool isempty() {if(top == -1)return true;elsereturn false;}PushOperationThe process of putting a new data element onto stack is known as a Push Operation. Pushoperation involves a series of steps − Step 1 − Checks if the stack is full. Step 2 − If the stack is full, produces an error and exit. Step 3 − If the stack is not full, increments top to point next empty space. Step 4 − Adds data element to the stack location, where top is pointing. Step 5 − Returns success.
  • 88.
    Data Structures &Algorithms79If the linked list is used to implement the stack, then in step 3, we need to allocate spacedynamically.Algorithm for PUSH OperationA simple algorithm for Push operation can be derived as follows −begin procedure push: stack, dataif stack is fullreturn nullendiftop ← top + 1stack[top] ← dataend procedureImplementation of this algorithm in C, is very easy. See the following code −void push(int data) {if(!isFull()) {top = top + 1;stack[top] = data;}else {printf("Could not insert data, Stack is full.n");}}PopOperationAccessing the content while removing it from the stack, is known as a Pop Operation. Inan array implementation of pop() operation, the data element is not actually removed,instead top is decremented to a lower position in the stack to point to the next value. Butin linked-list implementation, pop() actually removes data element and deallocatesmemory space.A Pop operation may involve the following steps − Step 1 − Checks if the stack is empty. Step 2 − If the stack is empty, produces an error and exit.
  • 89.
    Data Structures &Algorithms80 Step 3 − If the stack is not empty, accesses the data element at which top ispointing. Step 4 − Decreases the value of top by 1. Step 5 − Returns success.Algorithm for Pop OperationA simple algorithm for Pop operation can be derived as follows −begin procedure pop: stackif stack is emptyreturn nullendifdata ← stack[top]top ← top - 1return dataend procedure
  • 90.
    Data Structures &Algorithms81Implementation of this algorithm in C, is as follows −int pop(int data) {if(!isempty()) {data = stack[top];top = top - 1;return data;}else {printf("Could not retrieve data, Stack is empty.n");}}For a complete stack program in C programming language, please click here.StackPrograminCWe shall see the stack implementation in C programming language here. You can try theprogram by clicking on the Try-it button. To learn the theory aspect of stacks, click on visitprevious page.Implementation in C#include <stdio.h>int MAXSIZE = 8;int stack[8];int top = -1;int isempty() {if(top == -1)return 1;elsereturn 0;}
  • 91.
    Data Structures &Algorithms82int isfull() {if(top == MAXSIZE)return 1;elsereturn 0;}int peek() {return stack[top];}int pop() {int data;if(!isempty()) {data = stack[top];top = top - 1;return data;}else {printf("Could not retrieve data, Stack is empty.n");}}int push(int data) {if(!isfull()) {top = top + 1;stack[top] = data;}else {printf("Could not insert data, Stack is full.n");}}
  • 92.
    Data Structures &Algorithms83int main() {// push items on to the stackpush(3);push(5);push(9);push(1);push(12);push(15);printf("Element at top of the stack: %dn" ,peek());printf("Elements: n");// print stack datawhile(!isempty()) {int data = pop();printf("%dn",data);}printf("Stack full: %sn" , isfull()?"true":"false");printf("Stack empty: %sn" , isempty()?"true":"false");return 0;}If we compile and run the above program, it will produce the following result −Element at top of the stack: 15Elements:15121953Stack full: false
  • 93.
    Data Structures &Algorithms84Stack empty: true
  • 94.
    Data Structures &Algorithms85The way to write arithmetic expression is known as a notation. An arithmetic expressioncan be written in three different but equivalent notations, i.e., without changing theessence or output of an expression. These notations are − Infix Notation Prefix (Polish) Notation Postfix (Reverse-Polish) NotationThese notations are named as how they use operator in expression. We shall learn thesame here in this chapter.InfixNotationWe write expression in infix notation, e.g. a-b+c, where operators are used in-betweenoperands. It is easy for us humans to read, write, and speak in infix notation but the samedoes not go well with computing devices. An algorithm to process infix notation could bedifficult and costly in terms of time and space consumption.PrefixNotationIn this notation, operator is prefixed to operands, i.e. operator is written ahead ofoperands. For example, +ab. This is equivalent to its infix notation a+b. Prefix notationis also known as Polish Notation.PostfixNotationThis notation style is known as Reversed Polish Notation. In this notation style, theoperator is postfixed to the operands i.e., the operator is written after the operands. Forexample, ab+. This is equivalent to its infix notation a+b.The following table briefly tries to show the difference in all three notations −Sr.No.Infix Notation Prefix Notation Postfix Notation1 a + b + a b a b +2 (a + b) * c * + a b c a b + c *3 a * (b + c) * a + b c a b c + *4 a / b + c / d + / a b / c d a b / c d / +14. Expression Parsing
  • 95.
    Data Structures &Algorithms865 (a + b) * (c + d) * + a b + c d a b + c d + *6 ((a + b) * c) - d - * + a b c d a b + c * d -ParsingExpressionsAs we have discussed, it is not a very efficient way to design an algorithm or program toparse infix notations. Instead, these infix notations are first converted into either postfixor prefix notations and then computed.To parse any arithmetic expression, we need to take care of operator precedence andassociativity also.PrecedenceWhen an operand is in between two different operators, which operator will take theoperand first, is decided by the precedence of an operator over others. For example −As multiplication operation has precedence over addition, b * c will be evaluated first. Atable of operator precedence is provided later.AssociativityAssociativity describes the rule where operators with the same precedence appear in anexpression. For example, in expression a+b−c, both + and – have the same precedence,then which part of the expression will be evaluated first, is determined by associativity ofthose operators. Here, both + and − are left associative, so the expression will beevaluated as (a+b)−c.Precedence and associativity determines the order of evaluation of an expression.Following is an operator precedence and associativity table (highest to lowest) −Sr.No.Operator Precedence Associativity1 Exponentiation ^ Highest Right Associative2 Multiplication ( * ) & Division ( / ) Second Highest Left Associative3 Addition ( + ) & Subtraction ( − ) Lowest Left AssociativeThe above table shows the default behavior of operators. At any point of time in expressionevaluation, the order can be altered by using parenthesis. For example −
  • 96.
    Data Structures &Algorithms87In a+b*c, the expression part b*c will be evaluated first, with multiplication asprecedence over addition. We here use parenthesis for a+b to be evaluated first,like (a+b)*c.PostfixEvaluationAlgorithmWe shall now look at the algorithm on how to evaluate postfix notation −Step 1 − scan the expression from left to rightStep 2 − if it is an operand push it to stackStep 3 − if it is an operator pull operand from stack and perform operationStep 4 − store the output of step 3, back to stackStep 5 − scan the expression until all operands are consumedStep 6 − pop the stack and perform operationTo see the implementation in C programming language, please click hereExpressionParsingUsingStackInfix notation is easier for humans to read and understand whereas for electronic machineslike computers, postfix is the best form of expression to parse. We shall see here a programto convert and evaluate infix notation to postfix notation −#include<stdio.h>#include<string.h>//char stackchar stack[25];int top = -1;void push(char item) {stack[++top] = item;}char pop() {return stack[top--];}
  • 97.
    Data Structures &Algorithms88//returns precedence of operatorsint precedence(char symbol) {switch(symbol) {case '+':case '-':return 2;break;case '*':case '/':return 3;break;case '^':return 4;break;case '(':case ')':case '#':return 1;break;}}//check whether the symbol is operator?int isOperator(char symbol) {switch(symbol) {case '+':case '-':case '*':case '/':case '^':case '(':case ')':return 1;break;
  • 98.
    Data Structures &Algorithms89default:return 0;}}//converts infix expression to postfixvoid convert(char infix[],char postfix[]) {int i,symbol,j = 0;stack[++top] = '#';for(i = 0;i<strlen(infix);i++) {symbol = infix[i];if(isOperator(symbol) == 0) {postfix[j] = symbol;j++;} else {if(symbol == '(') {push(symbol);}else {if(symbol == ')') {while(stack[top] != '(') {postfix[j] = pop();j++;}pop();//pop out (.} else {if(precedence(symbol)>precedence(stack[top])) {push(symbol);}else {while(precedence(symbol)<=precedence(stack[top])) {postfix[j] = pop();j++;}
  • 99.
    Data Structures &Algorithms90push(symbol);}}}}}while(stack[top] != '#') {postfix[j] = pop();j++;}postfix[j]='0';//null terminate string.}//int stackint stack_int[25];int top_int = -1;void push_int(int item) {stack_int[++top_int] = item;}char pop_int() {return stack_int[top_int--];}//evaluates postfix expressionint evaluate(char *postfix){char ch;int i = 0,operand1,operand2;while( (ch = postfix[i++]) != '0') {if(isdigit(ch)) {
  • 100.
    Data Structures &Algorithms91push_int(ch-'0'); // Push the operand}else {//Operator,pop two operandsoperand2 = pop_int();operand1 = pop_int();switch(ch) {case '+':push_int(operand1+operand2);break;case '-':push_int(operand1-operand2);break;case '*':push_int(operand1*operand2);break;case '/':push_int(operand1/operand2);break;}}}return stack_int[top_int];}void main() {char infix[25] = "1*(2+3)",postfix[25];convert(infix,postfix);printf("Infix expression is: %sn" , infix);printf("Postfix expression is: %sn" , postfix);printf("Evaluated expression is: %dn" , evaluate(postfix));}
  • 101.
    Data Structures &Algorithms92If we compile and run the above program, it will produce the following result −Infix expression is: 1*(2+3)Postfix expression is: 123+*Result is: 5
  • 102.
    Data Structures &Algorithms93Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queueis open at both its ends. One end is always used to insert data (enqueue) and the other isused to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., thedata item stored first will be accessed first.A real-world example of queue can be a single-lane one-way road, where the vehicle entersfirst, exits first. More real-world examples can be seen as queues at the ticket windowsand bus-stops.QueueRepresentationAs we now understand that in queue, we access both ends for different reasons. Thefollowing diagram given below tries to explain queue representation as data structure −As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers andStructures. For the sake of simplicity, we shall implement queues using one-dimensionalarray.BasicOperationsQueue operations may involve initializing or defining the queue, utilizing it, and thencompletely erasing it from the memory. Here we shall try to understand the basicoperations associated with queues − enqueue() − add (store) an item to the queue. dequeue() − remove (access) an item from the queue.15. Queue
  • 103.
    Data Structures &Algorithms94Few more functions are required to make the above-mentioned queue operation efficient.These are − peek() − Gets the element at the front of the queue without removing it. isfull() − Checks if the queue is full. isempty() − Checks if the queue is empty.In queue, we always dequeue (or access) data, pointed by front pointer and whileenqueing (or storing) data in the queue we take help of rear pointer.Let's first learn about supportive functions of a queue −peek()This function helps to see the data at the front of the queue. The algorithm of peek()function is as follows −begin procedure peekreturn queue[front]end procedureImplementation of peek() function in C programming language −int peek() {return queue[front];}isfull()As we are using single dimension array to implement queue, we just check for the rearpointer to reach at MAXSIZE to determine that the queue is full. In case we maintain thequeue in a circular linked-list, the algorithm will differ. Algorithm of isfull() function −begin procedure isfullif rear equals to MAXSIZEreturn trueelse
  • 104.
    Data Structures &Algorithms95return falseendifend procedureImplementation of isfull() function in C programming language −bool isfull() {if(rear == MAXSIZE - 1)return true;elsereturn false;}isempty()Algorithm of isempty() function −begin procedure isemptyif front is less than MIN OR front is greater than rearreturn trueelsereturn falseendifend procedureIf the value of front is less than MIN or 0, it tells that the queue is not yet initialized,hence empty.Here's the C programming code −bool isempty() {if(front < 0 || front > rear)return true;elsereturn false;}
  • 105.
    Data Structures &Algorithms96EnqueueOperationQueues maintain two data pointers, front and rear. Therefore, its operations arecomparatively difficult to implement than that of stacks.The following steps should be taken to enqueue (insert) data into a queue − Step 1 − Check if the queue is full. Step 2 − If the queue is full, produce overflow error and exit. Step 3 − If the queue is not full, increment rear pointer to point the next emptyspace. Step 4 − Add data element to the queue location, where the rear is pointing. Step 5 − Return success.Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseensituations.
  • 106.
    Data Structures &Algorithms97Algorithm for enqueue Operationprocedure enqueue(data)if queue is fullreturn overflowendifrear ← rear + 1queue[rear] ← datareturn trueend procedureImplementation of enqueue() in C programming language −int enqueue(int data)if(isfull())return 0;rear = rear + 1;queue[rear] = data;return 1;end procedureDequeueOperationAccessing data from the queue is a process of two tasks − access the data where front ispointing and remove the data after access. The following steps are taken toperform dequeue operation − Step 1 − Check if the queue is empty. Step 2 − If the queue is empty, produce underflow error and exit. Step 3 − If the queue is not empty, access the data where front is pointing. Step 4 − Increment front pointer to point to the next available data element. Step 5 − Return success.
  • 107.
    Data Structures &Algorithms98Algorithm for dequeue Operationprocedure dequeueif queue is emptyreturn underflowend ifdata = queue[front]front ← front + 1return trueend procedureImplementation of dequeue() in C programming language −int dequeue() {if(isempty())return 0;int data = queue[front];front = front + 1;return data;}For a complete Queue program in C programming language, please click here.
  • 108.
    Data Structures &Algorithms99QueuePrograminCWe shall see the stack implementation in C programming language here. You can try theprogram by clicking on the Try-it button. To learn the theory aspect of stacks, click on visitprevious page.Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>#define MAX 6int intArray[MAX];int front = 0;int rear = -1;int itemCount = 0;int peek(){return intArray[front];}bool isEmpty(){return itemCount == 0;}bool isFull(){return itemCount == MAX;}int size(){return itemCount;}void insert(int data){if(!isFull()){
  • 109.
    Data Structures &Algorithms100if(rear == MAX-1){rear = -1;}intArray[++rear] = data;itemCount++;}}int removeData(){int data = intArray[front++];if(front == MAX){front = 0;}itemCount--;return data;}int main() {/* insert 5 items */insert(3);insert(5);insert(9);insert(1);insert(12);// front : 0// rear : 4// ------------------// index : 0 1 2 3 4// ------------------// queue : 3 5 9 1 12insert(15);// front : 0// rear : 5
  • 110.
    Data Structures &Algorithms101// ---------------------// index : 0 1 2 3 4 5// ---------------------// queue : 3 5 9 1 12 15if(isFull()){printf("Queue is full!n");}// remove one itemint num = removeData();printf("Element removed: %dn",num);// front : 1// rear : 5// -------------------// index : 1 2 3 4 5// -------------------// queue : 5 9 1 12 15// insert more itemsinsert(16);// front : 1// rear : -1// ----------------------// index : 0 1 2 3 4 5// ----------------------// queue : 16 5 9 1 12 15// As queue is full, elements will not be inserted.insert(17);insert(18);// ----------------------// index : 0 1 2 3 4 5// ----------------------
  • 111.
    Data Structures &Algorithms102// queue : 16 5 9 1 12 15printf("Element at front: %dn",peek());printf("----------------------n");printf("index : 5 4 3 2 1 0n");printf("----------------------n");printf("Queue: ");while(!isEmpty()){int n = removeData();printf("%d ",n);}}If we compile and run the above program, it will produce the following result −Queue is full!Element removed: 3Element at front: 5----------------------index : 5 4 3 2 1 0----------------------Queue: 5 9 1 12 15 16
  • 112.
    Data Structures &Algorithms103Searching Techniques
  • 113.
    Data Structures &Algorithms104Linear search is a very simple search algorithm. In this type of search, a sequential searchis made over all items one by one. Every item is checked and if a match is found then thatparticular item is returned, otherwise the search continues till the end of the datacollection.AlgorithmLinear Search ( Array A, Value x)Step 1: Set i to 1Step 2: if i > n then go to step 7Step 3: if A[i] = x then go to step 6Step 4: Set i to i + 1Step 5: Go to Step 2Step 6: Print Element x Found at index i and go to step 8Step 7: Print element not foundStep 8: ExitPseudocodeprocedure linear_search (list, value)for each item in the listif match item == valuereturn the item's location16. Linear Search
  • 114.
    Data Structures &Algorithms105end ifend forend procedureTo know about linear search implementation in C programming language, please click-here.LinearSearchPrograminCHere we present the implementation of linear search in C programming language. Theoutput of the program is given after the code.Linear Search Program#include <stdio.h>#define MAX 20// array of items on which linear search will be conducted.int intArray[MAX] = {1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}printf("=n");}// this method makes a linear search.int find(int data){int comparisons = 0;int index = -1;int i;
  • 115.
    Data Structures &Algorithms106// navigate through all itemsfor(i = 0;i<MAX;i++){// count the comparisons madecomparisons++;// if data found, break the loopif(data == intArray[i]){index = i;break;}}printf("Total comparisons made: %d", comparisons);return index;}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}printf("]n");}main(){printf("Input Array: ");display();printline(50);
  • 116.
    Data Structures &Algorithms107//find location of 1int location = find(55);// if element was foundif(location != -1)printf("nElement found at location: %d" ,(location+1));elseprintf("Element not found.");}If we compile and run the above program, it will produce the following result −Input Array: [1 2 3 4 6 7 9 11 12 14 15 16 17 19 33 34 43 45 55 66 ]==================================================Total comparisons made: 19Element found at location: 19
  • 117.
    Data Structures &Algorithms108Binary search is a fast search algorithm with run-time complexity of Ο(log n). This searchalgorithm works on the principle of divide and conquer. For this algorithm to work properly,the data collection should be in the sorted form.Binary search looks for a particular item by comparing the middle most item of thecollection. If a match occurs, then the index of item is returned. If the middle item isgreater than the item, then the item is searched in the sub-array to the right of the middleitem. Otherwise, the item is searched for in the sub-array to the left of the middle item.This process continues on the sub-array as well until the size of the subarray reduces tozero.HowBinarySearchWorks?For a binary search to work, it is mandatory for the target array to be sorted. We shalllearn the process of binary search with a pictorial example. The following is our sortedarray and let us assume that we need to search the location of value 31 using binarysearch.First, we shall determine half of the array by using this formula −mid = low + (high - low) / 2Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.Now we compare the value stored at location 4, with the value being searched, i.e. 31.We find that the value at location 4 is 27, which is not a match. As the value is greaterthan 27 and we have a sorted array, so we also know that the target value must be in theupper portion of the array.17. Binary Search
  • 118.
    Data Structures &Algorithms109We change our low to mid + 1 and find the new mid value again.low = mid + 1mid = low + (high - low) / 2Our new mid is 7 now. We compare the value stored at location 7 with our target value31.The value stored at location 7 is not a match, rather it is less than what we are lookingfor. So, the value must be in the lower part from this location.Hence, we calculate the mid again. This time it is 5.We compare the value stored at location 5 with our target value. We find that it is a match.We conclude that the target value 31 is stored at location 5.Binary search halves the searchable items and thus reduces the count of comparisons tobe made to very less numbers.
  • 119.
    Data Structures &Algorithms110PseudocodeThe pseudocode of binary search algorithms should look like this −Procedure binary_searchA ← sorted arrayn ← size of arrayx ← value ot be searchedSet lowerBound = 1Set upperBound = nwhile x not foundif upperBound < lowerBoundEXIT: x does not exists.set midPoint = lowerBound + ( upperBound - lowerBound ) / 2if A[midPoint] < xset lowerBound = midPoint + 1if A[midPoint] > xset upperBound = midPoint - 1if A[midPoint] = xEXIT: x found at location midPointend whileend procedureTo know about binary search implementation using array in C programming language,please click here.
  • 120.
    Data Structures &Algorithms111BinarySearchPrograminCBinary search is a fast search algorithm with run-time complexity of Ο(log n). This searchalgorithm works on the principle of divide and conquer. For this algorithm to work properly,the data collection should be in a sorted form.Implementation in C#include <stdio.h>#define MAX 20// array of items on which linear search will be conducted.int intArray[MAX] = {1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}printf("=n");}int find(int data){int lowerBound = 0;int upperBound = MAX -1;int midPoint = -1;int comparisons = 0;int index = -1;while(lowerBound <= upperBound){printf("Comparison %dn" , (comparisons +1) ) ;printf("lowerBound : %d, intArray[%d] = %dn",lowerBound,lowerBound,intArray[lowerBound]);printf("upperBound : %d, intArray[%d] = %dn",upperBound,upperBound,intArray[upperBound]);
  • 121.
    Data Structures &Algorithms112comparisons++;// compute the mid point// midPoint = (lowerBound + upperBound) / 2;midPoint = lowerBound + (upperBound - lowerBound) / 2;// data foundif(intArray[midPoint] == data){index = midPoint;break;}else {// if data is largerif(intArray[midPoint] < data){// data is in upper halflowerBound = midPoint + 1;}// data is smallerelse{// data is in lower halfupperBound = midPoint -1;}}}printf("Total comparisons made: %d" , comparisons);return index;}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}
  • 122.
    Data Structures &Algorithms113printf("]n");}main(){printf("Input Array: ");display();printline(50);//find location of 1int location = find(55);// if element was foundif(location != -1)printf("nElement found at location: %d" ,(location+1));elseprintf("nElement not found.");}If we compile and run the above program, it will produce the following result −Input Array: [1 2 3 4 6 7 9 11 12 14 15 16 17 19 33 34 43 45 55 66 ]==================================================Comparison 1lowerBound : 0, intArray[0] = 1upperBound : 19, intArray[19] = 66Comparison 2lowerBound : 10, intArray[10] = 15upperBound : 19, intArray[19] = 66Comparison 3lowerBound : 15, intArray[15] = 34upperBound : 19, intArray[19] = 66Comparison 4lowerBound : 18, intArray[18] = 55upperBound : 19, intArray[19] = 66Total comparisons made: 4Element found at location: 19
  • 123.
    Data Structures &Algorithms114
  • 124.
    Data Structures &Algorithms115Interpolation search is an improved variant of binary search. This search algorithm workson the probing position of the required value. For this algorithm to work properly, the datacollection should be in a sorted form and equally distributed.Binary search has a huge advantage of time complexity over linear search. Linear searchhas worst-case complexity of Ο(n) whereas binary search has Ο(log n).There are cases where the location of target data may be known in advance. For example,in case of a telephone directory, if we want to search the telephone number of Morphius.Here, linear search and even binary search will seem slow as we can directly jump tomemory space where the names start from 'M' are stored.PositioninginBinarySearchIn binary search, if the desired data is not found then the rest of the list is divided in twoparts, lower and higher. The search is carried out in either of them.Even when the data is sorted, binary search does not take advantage to probe the positionof the desired data.18. Interpolation Search
  • 125.
    Data Structures &Algorithms116PositionProbinginInterpolationSearchInterpolation search finds a particular item by computing the probe position. Initially, theprobe position is the position of the middle most item of the collection.If a match occurs, then the index of the item is returned. To split the list into two parts,we use the following method −mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo])where −A = listLo = Lowest index of the listHi = Highest index of the listA[n] = Value stored at index n in the listIf the middle item is greater than the item, then the probe position is again calculated inthe sub-array to the right of the middle item. Otherwise, the item is searched in the sub-array to the left of the middle item. This process continues on the sub-array as well untilthe size of subarray reduces to zero.Runtime complexity of interpolation search algorithm is Ο(log (log n)) as comparedto Ο(log n) of BST in favorable situations.AlgorithmAs it is an improvisation of the existing BST algorithm, we are mentioning the steps tosearch the 'target' data value index, using position probing −Step 1 − Start searching data from middle of the list.Step 2 − If it is a match, return the index of the item, and exit.Step 3 − If it is not a match, probe position.Step 4 − Divide the list using probing formula and find the new middle.Step 5 − If data is greater than middle, search in higher sub-list.Step 6 − If data is smaller than middle, search in lower sub-list.Step 7 − Repeat until match.
  • 126.
    Data Structures &Algorithms117PseudocodeA → Array listN → Size of AX → Target ValueProcedure Interpolation_Search()Set Lo → 0Set Mid → -1Set Hi → N-1While X does not matchif Lo equals to Hi OR A[Lo] equals to A[Hi]EXIT: Failure, Target not foundend ifSet Mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo])if A[Mid] = XEXIT: Success, Target found at Midelseif A[Mid] < XSet Lo to Mid+1else if A[Mid] > XSet Hi to Mid-1end ifend ifEnd WhileEnd ProcedureTo know about the implementation of interpolation search in C programminglanguage, click here.
  • 127.
    Data Structures &Algorithms118InterpolationSearchPrograminCInterpolation search is an improved variant of binary search. This search algorithm workson the probing position of the required value. For this algorithm to work properly, the datacollection should be in sorted and equally distributed form.It's runtime complexity is log2(log2 n).Implementation in C#include<stdio.h>#define MAX 10// array of items on which linear search will be conducted.int list[MAX] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44 };int find(int data) {int lo = 0;int hi = MAX - 1;int mid = -1;int comparisons = 1;int index = -1;while(lo <= hi) {printf("nComparison %d n" , comparisons ) ;printf("lo : %d, list[%d] = %dn", lo, lo, list[lo]);printf("hi : %d, list[%d] = %dn", hi, hi, list[hi]);comparisons++;// probe the mid pointmid = lo + (((double)(hi - lo) / (list[hi] - list[lo])) * (data - list[lo]));printf("mid = %dn",mid);// data foundif(list[mid] == data) {index = mid;break;}else {
  • 128.
    Data Structures &Algorithms119if(list[mid] < data) {// if data is larger, data is in upper halflo = mid + 1;}else {// if data is smaller, data is in lower halfhi = mid - 1;}}}printf("nTotal comparisons made: %d", --comparisons);return index;}int main() {//find location of 33int location = find(33);// if element was foundif(location != -1)printf("nElement found at location: %d" ,(location+1));elseprintf("Element not found.");return 0;}If we compile and run the above program, it will produce the following result −Comparison 1lo : 0, list[0] = 10hi : 9, list[9] = 44mid = 6Total comparisons made: 1Element found at location: 7You can change the search value and execute the program to test it.
  • 129.
    Data Structures &Algorithms120Hash Table is a data structure which stores data in an associative manner. In a hash table,data is stored in an array format, where each data value has its own unique index value.Access of data becomes very fast if we know the index of the desired data.Thus, it becomes a data structure in which insertion and search operations are very fastirrespective of the size of the data. Hash Table uses an array as a storage medium anduses hash technique to generate an index where an element is to be inserted or is to belocated from.HashingHashing is a technique to convert a range of key values into a range of indexes of an array.We're going to use modulo operator to get a range of key values. Consider an example ofhash table of size 20, and the following items are to be stored. Item are in the (key,value)format. (1,20) (2,70) (42,80) (4,25) (12,44) (14,32) (17,11) (13,78) (37,98)19. Hash Table
  • 130.
    Data Structures &Algorithms121Sr. No. Key Hash Array Index1 1 1 % 20 = 1 12 2 2 % 20 = 2 23 42 42 % 20 = 2 24 4 4 % 20 = 4 45 12 12 % 20 = 12 126 14 14 % 20 = 14 147 17 17 % 20 = 17 178 13 13 % 20 = 13 139 37 37 % 20 = 17 17LinearProbingAs we can see, it may happen that the hashing technique is used to create an already usedindex of the array. In such a case, we can search the next empty location in the array bylooking into the next cell until we find an empty cell. This technique is called linear probing.Sr. No. Key Hash Array IndexAfter LinearProbing,Array Index1 1 1 % 20 = 1 1 12 2 2 % 20 = 2 2 23 42 42 % 20 = 2 2 34 4 4 % 20 = 4 4 45 12 12 % 20 = 12 12 126 14 14 % 20 = 14 14 14
  • 131.
    Data Structures &Algorithms1227 17 17 % 20 = 17 17 178 13 13 % 20 = 13 13 139 37 37 % 20 = 17 17 18BasicOperationsFollowing are the basic primary operations of a hash table. Search − Searches an element in a hash table. Insert − inserts an element in a hash table. Delete − Deletes an element from a hash table.DataItemDefine a data item having some data and key, based on which the search is to beconducted in a hash table.struct DataItem {int data;int key;};HashMethodDefine a hashing method to compute the hash code of the key of the data item.int hashCode(int key){return key % SIZE;}SearchOperationWhenever an element is to be searched, compute the hash code of the key passed andlocate the element using that hash code as index in the array. Use linear probing to getthe element ahead if the element is not found at the computed hash code.
  • 132.
    Data Structures &Algorithms123struct DataItem *search(int key){//get the hashint hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] != NULL){if(hashArray[hashIndex]->key == key)return hashArray[hashIndex];//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}return NULL;}InsertOperationWhenever an element is to be inserted, compute the hash code of the key passed andlocate the index using that hash code as an index in the array. Use linear probing forempty location, if an element is found at the computed hash code.void insert(int key,int data){struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));item->data = data;item->key = key;//get the hashint hashIndex = hashCode(key);//move in array until an empty or deleted cellwhile(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1){//go to next cell++hashIndex;
  • 133.
    Data Structures &Algorithms124//wrap around the tablehashIndex %= SIZE;}hashArray[hashIndex] = item;}DeleteOperationWhenever an element is to be deleted, compute the hash code of the key passed andlocate the index using that hash code as an index in the array. Use linear probing to getthe element ahead if an element is not found at the computed hash code. When found,store a dummy item there to keep the performance of the hash table intact.struct DataItem* delete(struct DataItem* item){int key = item->key;//get the hashint hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] !=NULL){if(hashArray[hashIndex]->key == key){struct DataItem* temp = hashArray[hashIndex];//assign a dummy item at deleted positionhashArray[hashIndex] = dummyItem;return temp;}//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}return NULL;}
  • 134.
    Data Structures &Algorithms125To know about hash implementation in C programming language, please click here.HashTablePrograminCHash Table is a data structure which stores data in an associative manner. In hash table,the data is stored in an array format where each data value has its own unique indexvalue. Access of data becomes very fast, if we know the index of the desired data.Implementation in C#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdbool.h>#define SIZE 20struct DataItem {int data;int key;};struct DataItem* hashArray[SIZE];struct DataItem* dummyItem;struct DataItem* item;int hashCode(int key){return key % SIZE;}struct DataItem *search(int key){//get the hashint hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] != NULL){if(hashArray[hashIndex]->key == key)return hashArray[hashIndex];
  • 135.
    Data Structures &Algorithms126//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}return NULL;}void insert(int key,int data){struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));item->data = data;item->key = key;//get the hashint hashIndex = hashCode(key);//move in array until an empty or deleted cellwhile(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1){//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}hashArray[hashIndex] = item;}struct DataItem* delete(struct DataItem* item){int key = item->key;//get the hash
  • 136.
    Data Structures &Algorithms127int hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] != NULL){if(hashArray[hashIndex]->key == key){struct DataItem* temp = hashArray[hashIndex];//assign a dummy item at deleted positionhashArray[hashIndex] = dummyItem;return temp;}//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}return NULL;}void display(){int i = 0;for(i = 0; i<SIZE; i++) {if(hashArray[i] != NULL)printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);elseprintf(" ~~ ");}printf("n");}
  • 137.
    Data Structures &Algorithms128int main(){dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));dummyItem->data = -1;dummyItem->key = -1;insert(1, 20);insert(2, 70);insert(42, 80);insert(4, 25);insert(12, 44);insert(14, 32);insert(17, 11);insert(13, 78);insert(37, 97);display();item = search(37);if(item != NULL){printf("Element found: %dn", item->data);}else {printf("Element not foundn");}delete(item);item = search(37);if(item != NULL){printf("Element found: %dn", item->data);}else {printf("Element not foundn");}}
  • 138.
    Data Structures &Algorithms129If we compile and run the above program, it will produce the following result −~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~ (12,44)(13,78) (14,32) ~~ ~~ (17,11) (37,97) ~~Element found: 97Element not found
  • 139.
    Data Structures &Algorithms130Sorting Techniques
  • 140.
    Data Structures &Algorithms131Sorting refers to arranging data in a particular format. Sorting algorithm specifies the wayto arrange data in a particular order. Most common orders are in numerical orlexicographical order.The importance of sorting lies in the fact that data searching can be optimized to a veryhigh level, if data is stored in a sorted manner. Sorting is also used to represent data inmore readable formats. Following are some of the examples of sorting in real-lifescenarios: Telephone Directory – The telephone directory stores the telephone numbers ofpeople sorted by their names, so that the names can be searched easily. Dictionary – The dictionary stores words in an alphabetical order so thatsearching of any word becomes easy.In-placeSortingandNot-in-placeSortingSorting algorithms may require some extra space for comparison and temporary storageof few data elements. These algorithms do not require any extra space and sorting is saidto happen in-place, or for example, within the array itself. This is called in-place sorting.Bubble sort is an example of in-place sorting.However, in some sorting algorithms, the program requires space which is more than orequal to the elements being sorted. Sorting which uses equal or more space is called not-in-place sorting. Merge-sort is an example of not-in-place sorting.StableandNotStableSortingIf a sorting algorithm, after sorting the contents, does not change the sequence of similarcontent in which they appear, it is called stable sorting.20. Sorting Algorithm
  • 141.
    Data Structures &Algorithms132If a sorting algorithm, after sorting the contents, changes the sequence of similar contentin which they appear, it is called unstable sorting.Stability of an algorithm matters when we wish to maintain the sequence of originalelements, like in a tuple for example.AdaptiveandNon-AdaptiveSortingAlgorithmA sorting algorithm is said to be adaptive, if it takes advantage of already 'sorted' elementsin the list that is to be sorted. That is, while sorting if the source list has some elementalready sorted, adaptive algorithms will take this into account and will try not to re-orderthem.A non-adaptive algorithm is one which does not take into account the elements which arealready sorted. They try to force every single element to be re-ordered to confirm theirsortedness.ImportantTermsSome terms are generally coined while discussing sorting techniques, here is a briefintroduction to them −Increasing OrderA sequence of values is said to be in increasing order, if the successive element is greaterthan the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every nextelement is greater than the previous element.Decreasing OrderA sequence of values is said to be in decreasing order, if the successive element is lessthan the current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every nextelement is less than the previous element.
  • 142.
    Data Structures &Algorithms133Non-Increasing OrderA sequence of values is said to be in non-increasing order, if the successive element isless than or equal to its previous element in the sequence. This order occurs when thesequence contains duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasingorder, as every next element is less than or equal to (in case of 3) but not greater thanany previous element.Non-Decreasing OrderA sequence of values is said to be in non-decreasing order, if the successive element isgreater than or equal to its previous element in the sequence. This order occurs when thesequence contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasingorder, as every next element is greater than or equal to (in case of 3) but not less thanthe previous one.
  • 143.
    Data Structures &Algorithms134Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-basedalgorithm in which each pair of adjacent elements is compared and the elements areswapped if they are not in order. This algorithm is not suitable for large data sets as itsaverage and worst case complexity are of O(n2) where n is the number of items.HowBubbleSortWorks?We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keepingit short and precise.Bubble sort starts with very first two elements, comparing them to check which one isgreater.In this case, value 33 is greater than 14, so it is already in sorted locations. Next, wecompare 33 with 27.We find that 27 is smaller than 33 and these two values must be swapped.21. Bubble Sort Algorithm
  • 144.
    Data Structures &Algorithms135The new array should look like this −Next we compare 33 and 35. We find that both are in already sorted positions.Then we move to the next two values, 35 and 10.We know then that 10 is smaller 35. Hence they are not sorted.We swap these values. We find that we have reached the end of the array. After oneiteration, the array should look like this −
  • 145.
    Data Structures &Algorithms136To be precise, we are now showing how an array should look like after each iteration. Afterthe second iteration, it should look like this −Notice that after each iteration, at least one value moves at the end.And when there's no swap required, bubble sorts learns that an array is completely sorted.Now we should look into some practical aspects of bubble sort.
  • 146.
    Data Structures &Algorithms137AlgorithmWe assume list is an array of n elements. We further assume that swap function swapsthe values of the given array elements.begin BubbleSort(list)for all elements of listif list[i] > list[i+1]swap(list[i], list[i+1])end ifend forreturn listend BubbleSortPseudocodeWe observe in algorithm that Bubble Sort compares each pair of array element unless thewhole array is completely sorted in an ascending order. This may cause a few complexityissues like what if the array needs no more swapping as all the elements are alreadyascending.To ease-out the issue, we use one flag variable swapped which will help us see if anyswap has happened or not. If no swap has occurred, i.e. the array requires no moreprocessing to be sorted, it will come out of the loop.Pseudocode of BubbleSort algorithm can be written as follows −procedure bubbleSort( list : array of items )loop = list.count;for i = 0 to loop-1 do:swapped = falsefor j = 0 to loop-1 do:/* compare the adjacent elements */if list[j] > list[j+1] then/* swap them */swap( list[j], list[j+1] )
  • 147.
    Data Structures &Algorithms138swapped = trueend ifend for/*if no number was swapped that meansarray is sorted now, break the loop.*/if(not swapped) thenbreakend ifend forend procedure return listImplementationOne more issue we did not address in our original algorithm and its improvisedpseudocode, is that, after every iteration the highest values settles down at the end of thearray. Hence, the next iteration need not include already sorted elements. For thispurpose, in our implementation, we restrict the inner loop to avoid already sorted values.To know about bubble sort implementation in C programming language, please click here.BubbleSortPrograminCWe shall see the implementation of bubble sort in C programming language here.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 10int list[MAX] = {1,8,4,6,0,3,5,2,7,9};void display(){int i;printf("[");
  • 148.
    Data Structures &Algorithms139// navigate through all itemsfor(i = 0; i < MAX; i++){printf("%d ",list[i]);}printf("]n");}void bubbleSort() {int temp;int i,j;bool swapped = false;// loop through all numbersfor(i = 0; i < MAX-1; i++) {swapped = false;// loop through numbers falling aheadfor(j = 0; j < MAX-1-i; j++) {printf(" Items compared: [ %d, %d ] ", list[j],list[j+1]);// check if next number is lesser than current no// swap the numbers.// (Bubble up the highest number)if(list[j] > list[j+1]) {temp = list[j];list[j] = list[j+1];list[j+1] = temp;swapped = true;printf(" => swapped [%d, %d]n",list[j],list[j+1]);}else {printf(" => not swappedn");}
  • 149.
    Data Structures &Algorithms140}// if no number was swapped that means// array is sorted now, break the loop.if(!swapped) {break;}printf("Iteration %d#: ",(i+1));display();}}main(){printf("Input Array: ");display();printf("n");bubbleSort();printf("nOutput Array: ");display();}If we compile and run the above program, it will produce the following result −Input Array: [1 8 4 6 0 3 5 2 7 9 ]Items compared: [ 1, 8 ] => not swappedItems compared: [ 8, 4 ] => swapped [4, 8]Items compared: [ 8, 6 ] => swapped [6, 8]Items compared: [ 8, 0 ] => swapped [0, 8]Items compared: [ 8, 3 ] => swapped [3, 8]Items compared: [ 8, 5 ] => swapped [5, 8]Items compared: [ 8, 2 ] => swapped [2, 8]Items compared: [ 8, 7 ] => swapped [7, 8]Items compared: [ 8, 9 ] => not swapped
  • 150.
    Data Structures &Algorithms141Iteration 1#: [1 4 6 0 3 5 2 7 8 9 ]Items compared: [ 1, 4 ] => not swappedItems compared: [ 4, 6 ] => not swappedItems compared: [ 6, 0 ] => swapped [0, 6]Items compared: [ 6, 3 ] => swapped [3, 6]Items compared: [ 6, 5 ] => swapped [5, 6]Items compared: [ 6, 2 ] => swapped [2, 6]Items compared: [ 6, 7 ] => not swappedItems compared: [ 7, 8 ] => not swappedIteration 2#: [1 4 0 3 5 2 6 7 8 9 ]Items compared: [ 1, 4 ] => not swappedItems compared: [ 4, 0 ] => swapped [0, 4]Items compared: [ 4, 3 ] => swapped [3, 4]Items compared: [ 4, 5 ] => not swappedItems compared: [ 5, 2 ] => swapped [2, 5]Items compared: [ 5, 6 ] => not swappedItems compared: [ 6, 7 ] => not swappedIteration 3#: [1 0 3 4 2 5 6 7 8 9 ]Items compared: [ 1, 0 ] => swapped [0, 1]Items compared: [ 1, 3 ] => not swappedItems compared: [ 3, 4 ] => not swappedItems compared: [ 4, 2 ] => swapped [2, 4]Items compared: [ 4, 5 ] => not swappedItems compared: [ 5, 6 ] => not swappedIteration 4#: [0 1 3 2 4 5 6 7 8 9 ]Items compared: [ 0, 1 ] => not swappedItems compared: [ 1, 3 ] => not swappedItems compared: [ 3, 2 ] => swapped [2, 3]Items compared: [ 3, 4 ] => not swappedItems compared: [ 4, 5 ] => not swappedIteration 5#: [0 1 2 3 4 5 6 7 8 9 ]Items compared: [ 0, 1 ] => not swappedItems compared: [ 1, 2 ] => not swappedItems compared: [ 2, 3 ] => not swappedItems compared: [ 3, 4 ] => not swappedOutput Array: [0 1 2 3 4 5 6 7 8 9 ]
  • 151.
    Data Structures &Algorithms142This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintainedwhich is always sorted. For example, the lower part of an array is maintained to be sorted.An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate placeand then it has to be inserted there. Hence the name, insertion sort.The array is searched sequentially and unsorted items are moved and inserted into thesorted sub-list (in the same array). This algorithm is not suitable for large data sets as itsaverage and worst case complexity are of Ο(n2), where n is the number of items.HowInsertionSortWorks?We take an unsorted array for our example.Insertion sort compares the first two elements.It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.Insertion sort moves ahead and compares 33 with 27.And finds that 33 is not in the correct position.22. Insertion Sort
  • 152.
    Data Structures &Algorithms143It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we seethat the sorted sub-list has only one element 14, and 27 is greater than 14. Hence, thesorted sub-list remains sorted after swapping.By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.These values are not in a sorted order.So we swap them.However, swapping makes 27 and 10 unsorted.Hence, we swap them too.Again we find 14 and 10 in an unsorted order.
  • 153.
    Data Structures &Algorithms144We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.This process goes on until all the unsorted values are covered in a sorted sub-list. Now weshall see some programming aspects of insertion sort.AlgorithmNow we have a bigger picture of how this sorting technique works, so we can derive simplesteps by which we can achieve insertion sort.Step 1 − If it is the first element, it is already sorted. return 1;Step 2 − Pick next elementStep 3 − Compare with all elements in the sorted sub-listStep 4 − Shift all the elements in the sorted sub-list that is greater than thevalue to be sortedStep 5 − Insert the valueStep 6 − Repeat until list is sortedPseudocodeprocedure insertionSort( A : array of items )int holePositionint valueToInsertfor i = 1 to length(A) inclusive do:/* select value to be inserted */valueToInsert = A[i]holePosition = i
  • 154.
    Data Structures &Algorithms145/*locate hole position for the element to be inserted */while holePosition > 0 and A[holePosition-1] > valueToInsert do:A[holePosition] = A[holePosition-1]holePosition = holePosition -1end while/* insert the number at hole position */A[holePosition] = valueToInsertend forend procedureTo know about insertion sort implementation in C programming language, please clickhere.InsertionSortPrograminCThis is an in-place comparison-based sorting algorithm. Here, a sub-list is maintainedwhich is always sorted. For example, the lower part of an array is maintained to be sorted.An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate placeand then it is to be inserted there. Hence the name insertion sort.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}
  • 155.
    Data Structures &Algorithms146printf("=n");}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}printf("]n");}void insertionSort(){int valueToInsert;int holePosition;int i;// loop through all numbersfor(i = 1; i < MAX; i++){// select a value to be inserted.valueToInsert = intArray[i];// select the hole position where number is to be insertedholePosition = i;// check if previous no. is larger than value to be insertedwhile (holePosition > 0 && intArray[holePosition-1] > valueToInsert){intArray[holePosition] = intArray[holePosition-1];holePosition--;printf(" item moved : %dn" , intArray[holePosition]);}
  • 156.
    Data Structures &Algorithms147if(holePosition != i){printf(" item inserted : %d, at position : %dn" ,valueToInsert,holePosition);// insert the number at hole positionintArray[holePosition] = valueToInsert;}printf("Iteration %d#:",i);display();}}main(){printf("Input Array: ");display();printline(50);insertionSort();printf("Output Array: ");display();printline(50);}If we compile and run the above program, it will produce the following result −Input Array: [4 6 3 2 1 9 7 ]==================================================Iteration 1#:[4 6 3 2 1 9 7 ]item moved : 6item moved : 4item inserted : 3, at position : 0Iteration 2#:[3 4 6 2 1 9 7 ]item moved : 6item moved : 4item moved : 3item inserted : 2, at position : 0Iteration 3#:[2 3 4 6 1 9 7 ]item moved : 6
  • 157.
    Data Structures &Algorithms148item moved : 4item moved : 3item moved : 2item inserted : 1, at position : 0Iteration 4#:[1 2 3 4 6 9 7 ]Iteration 5#:[1 2 3 4 6 9 7 ]item moved : 9item inserted : 7, at position : 5Iteration 6#:[1 2 3 4 6 7 9 ]Output Array: [1 2 3 4 6 7 9 ]==================================================
  • 158.
    Data Structures &Algorithms149Selection sort is a simple sorting algorithm. This sorting algorithm is an in-placecomparison-based algorithm in which the list is divided into two parts, the sorted part atthe left end and the unsorted part at the right end. Initially, the sorted part is empty andthe unsorted part is the entire list.The smallest element is selected from the unsorted array and swapped with the leftmostelement, and that element becomes a part of the sorted array. This process continuesmoving unsorted array boundary by one element to the right.This algorithm is not suitable for large data sets as its average and worst case complexitiesare of O(n2), where n is the number of items.HowSelectionSortWorks?Consider the following depicted array as an example.For the first position in the sorted list, the whole list is scanned sequentially. The firstposition where 14 is stored presently, we search the whole list and find that 10 is thelowest value.So we replace 14 with 10. After one iteration 10, which happens to be the minimum valuein the list, appears in the first position of the sorted list.For the second position, where 33 is residing, we start scanning the rest of the list in alinear manner.We find that 14 is the second lowest value in the list and it should appear at the secondplace. We swap these values.23. Selection Sort
  • 159.
    Data Structures &Algorithms150After two iterations, two least values are positioned at the beginning in a sorted manner.The same process is applied to the rest of the items in the array.Following is a pictorial depiction of the entire sorting process −
  • 160.
    Data Structures &Algorithms151Now, let us learn some programming aspects of selection sort.AlgorithmStep 1 − Set MIN to location 0Step 2 − Search the minimum element in the listStep 3 − Swap with value at location MINStep 4 − Increment MIN to point to next elementStep 5 − Repeat until list is sortedPseudocodeprocedure selection sortlist : array of itemsn : size of listfor i = 1 to n - 1/* set current element as minimum*/min = i/* check the element to be minimum */for j = i+1 to nif list[j] < list[min] thenmin = j;end ifend for/* swap the minimum element with the current element*/if indexMin != i thenswap list[min] and list[i]end ifend forend procedureTo know about selection sort implementation in C programming language, please clickhere.
  • 161.
    Data Structures &Algorithms152SelectionSortPrograminCSelection sort is a simple sorting algorithm. This sorting algorithm is an in-placecomparison-based algorithm in which the list is divided into two parts, the sorted part atthe left end and the unsorted part at the right end. Initially, the sorted part is empty andthe unsorted part is the entire list.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}printf("=n");}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ", intArray[i]);}
  • 162.
    Data Structures &Algorithms153printf("]n");}void selectionSort(){int indexMin,i,j;// loop through all numbersfor(i = 0; i < MAX-1; i++){// set current element as minimumindexMin = i;// check the element to be minimumfor(j = i+1;j<MAX;j++){if(intArray[j] < intArray[indexMin]){indexMin = j;}}if(indexMin != i){printf("Items swapped: [ %d, %d ]n" , intArray[i],intArray[indexMin]);// swap the numbersint temp = intArray[indexMin];intArray[indexMin] = intArray[i];intArray[i] = temp;}printf("Iteration %d#:",(i+1));display();}}
  • 163.
    Data Structures &Algorithms154main(){printf("Input Array: ");display();printline(50);selectionSort();printf("Output Array: ");display();printline(50);}If we compile and run the above program, it will produce the following result −Input Array: [4 6 3 2 1 9 7 ]==================================================Items swapped: [ 4, 1 ]Iteration 1#:[1 6 3 2 4 9 7 ]Items swapped: [ 6, 2 ]Iteration 2#:[1 2 3 6 4 9 7 ]Iteration 3#:[1 2 3 6 4 9 7 ]Items swapped: [ 6, 4 ]Iteration 4#:[1 2 3 4 6 9 7 ]Iteration 5#:[1 2 3 4 6 9 7 ]Items swapped: [ 9, 7 ]Iteration 6#:[1 2 3 4 6 7 9 ]Output Array: [1 2 3 4 6 7 9 ]==================================================
  • 164.
    Data Structures &Algorithms155Merge sort is a sorting technique based on divide and conquer technique. With worst-casetime complexity being Ο(n log n), it is one of the most respected algorithms.Merge sort first divides the array into equal halves and then combines them in a sortedmanner.HowMergeSortWorks?To understand merge sort, we take an unsorted array as the following −We know that merge sort first divides the whole array iteratively into equal halves unlessthe atomic values are achieved. We see here that an array of 8 items is divided into twoarrays of size 4.This does not change the sequence of appearance of items in the original. Now we dividethese two arrays into halves.We further divide these arrays and we achieve atomic value which can no more be divided.Now, we combine them in exactly the same manner as they were broken down. Pleasenote the color codes given to these lists.We first compare the element for each list and then combine them into another list in asorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 andin the target list of 2 values we put 10 first, followed by 27. We change the order of 19and 35 whereas 42 and 44 are placed sequentially.24. Merge Sort Algorithm
  • 165.
    Data Structures &Algorithms156In the next iteration of the combining phase, we compare lists of two data values, andmerge them into a list of found data values placing all in a sorted order.After the final merging, the list should look like this −Now we should learn some programming aspects of merge sorting.AlgorithmMerge sort keeps on dividing the list into equal halves until it can no more be divided. Bydefinition, if it is only one element in the list, it is sorted. Then, merge sort combines thesmaller sorted lists keeping the new list sorted too.Step 1 − if it is only one element in the list it is already sorted, return.Step 2 − divide the list recursively into two halves until it can no more bedivided.Step 3 − merge the smaller lists into new list in sorted order.PseudocodeWe shall now see the pseudocodes for merge sort functions. As our algorithms point outtwo main functions − divide & merge.Merge sort works with recursion and we shall see our implementation in the same way.procedure mergesort( var a as array )if ( n == 1 ) return avar l1 as array = a[0] ... a[n/2]var l2 as array = a[n/2+1] ... a[n]l1 = mergesort( l1 )
  • 166.
    Data Structures &Algorithms157l2 = mergesort( l2 )return merge( l1, l2 )end procedureprocedure merge( var a as array, var b as array )var c as arraywhile ( a and b have elements )if ( a[0] > b[0] )add b[0] to the end of cremove b[0] from belseadd a[0] to the end of cremove a[0] from aend ifend whilewhile ( a has elements )add a[0] to the end of cremove a[0] from aend whilewhile ( b has elements )add b[0] to the end of cremove b[0] from bend whilereturn cend procedureTo know about merge sort implementation in C programming language, please click here.
  • 167.
    Data Structures &Algorithms158MergeSortPrograminCMerge sort is a sorting technique based on divide and conquer technique. With the worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.Implementation in CWe shall see the implementation of merge sort in C programming language here −#include <stdio.h>#define max 10int a[10] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44 };int b[10];void merging(int low, int mid, int high) {int l1, l2, i;for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {if(a[l1] <= a[l2])b[i] = a[l1++];elseb[i] = a[l2++];}while(l1 <= mid)b[i++] = a[l1++];while(l2 <= high)b[i++] = a[l2++];for(i = low; i <= high; i++)a[i] = b[i];}void sort(int low, int high) {int mid;if(low < high) {
  • 168.
    Data Structures &Algorithms159mid = (low + high) / 2;sort(low, mid);sort(mid+1, high);merging(low, mid, high);}else {return;}}int main() {int i;printf("List before sortingn");for(i = 0; i <= max; i++)printf("%d ", a[i]);sort(0, max);printf("nList after sortingn");for(i = 0; i <= max; i++)printf("%d ", a[i]);}If we compile and run the above program, it will produce the following result −List before sorting10 14 19 26 27 31 33 35 42 44 0List after sorting0 10 14 19 26 27 31 33 35 42 44
  • 169.
    Data Structures &Algorithms160Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm.This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to thefar right and has to be moved to the far left.This algorithm uses insertion sort on a widely spread elements, first to sort them and thensorts the less widely spaced elements. This spacing is termed as interval. This interval iscalculated based on Knuth's formula as −h = h * 3 + 1where −h is interval with initial value 1This algorithm is quite efficient for medium-sized data sets as its average and worst casecomplexity are of O(n), where n is the number of items.HowShellSortWorks?Let us consider the following example to have an idea of how shell sort works. We takethe same array we have used in our previous examples. For our example and ease ofunderstanding, we take the interval of 4. Make a virtual sub-list of all values located atthe interval of 4 positions. Here these values are {35, 14}, {33, 19}, {42, 27} and {10,14}25. Shell Sort
  • 170.
    Data Structures &Algorithms161We compare values in each sub-list and swap them (if necessary) in the original array.After this step, the new array should look like this −Then, we take interval of 2 and this gap generates two sub-lists - {14, 27, 35, 42}, {19,10, 33, 44}We compare and swap the values, if required, in the original array. After this step, thearray should look like this −Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sortto sort the array.
  • 171.
    Data Structures &Algorithms162Following is the step-by-step depiction −
  • 172.
    Data Structures &Algorithms163We see that it required only four swaps to sort the rest of the array.AlgorithmFollowing is the algorithm for shell sort.Step 1 − Initialize the value of hStep 2 − Divide the list into smaller sub-list of equal interval hStep 3 − Sort these sub-lists using insertion sortStep 3 − Repeat until complete list is sortedPseudocodeFollowing is the pseudocode for shell sort.procedure shellSort()A : array of items/* calculate interval*/while interval < A.length /3 do:interval = interval * 3 + 1end whilewhile interval > 0 do:for outer = interval; outer < A.length; outer ++ do:/* select value to be inserted */valueToInsert = A[outer]inner = outer;/*shift element towards right*/while inner > interval -1 && A[inner - interval] >= valueToInsert do:A[inner] = A[inner - interval]inner = inner - intervalend while
  • 173.
    Data Structures &Algorithms164/* insert the number at hole position */A[inner] = valueToInsertend for/* calculate interval*/interval = (interval -1) /3;end whileend procedureTo know about shell sort implementation in C programming language, please click here.ShellSortPrograminCShell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm.This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to thefar right and has to be moved to the far left.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}printf("=n");}
  • 174.
    Data Structures &Algorithms165void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}printf("]n");}void shellSort(){int inner, outer;int valueToInsert;int interval = 1;int elements = MAX;int i = 0;while(interval <= elements/3) {interval = interval*3 +1;}while(interval > 0) {printf("iteration %d#:",i);display();for(outer = interval; outer < elements; outer++) {valueToInsert = intArray[outer];inner = outer;while(inner > interval -1 && intArray[inner - interval]>= valueToInsert) {intArray[inner] = intArray[inner - interval];inner -=interval;printf(" item moved :%dn",intArray[inner]);}
  • 175.
    Data Structures &Algorithms166intArray[inner] = valueToInsert;printf(" item inserted :%d, at position :%dn",valueToInsert,inner);}interval = (interval -1) /3;i++;}}int main() {printf("Input Array: ");display();printline(50);shellSort();printf("Output Array: ");display();printline(50);return 1;}If we compile and run the above program, it will produce the following result −Input Array: [4 6 3 2 1 9 7 ]==================================================iteration 0#:[4 6 3 2 1 9 7 ]item moved :4item inserted :1, at position :0item inserted :9, at position :5item inserted :7, at position :6iteration 1#:[1 6 3 2 4 9 7 ]item inserted :6, at position :1item moved :6item inserted :3, at position :1item moved :6
  • 176.
    Data Structures &Algorithms167item moved :3item inserted :2, at position :1item moved :6item inserted :4, at position :3item inserted :9, at position :5item moved :9item inserted :7, at position :5Output Array: [1 2 3 4 6 7 9 ]==================================================
  • 177.
    Data Structures &Algorithms168Quick sort is a highly efficient sorting algorithm and is based on partitioning of array ofdata into smaller arrays. A large array is partitioned into two arrays one of which holdsvalues smaller than the specified value, say pivot, based on which the partition is madeand another array holds values greater than the pivot value.Quick sort partitions an array and then calls itself recursively twice to sort the two resultingsubarrays. This algorithm is quite efficient for large-sized data sets as its average andworst case complexity are of O(nlogn), where n is the number of items.PartitioninQuickSortFollowing animated representation explains how to find the pivot value in an array.The pivot value divides the list into two parts. And recursively, we find the pivot for eachsub-lists until all lists contains only one element.QuickSortPivotAlgorithmBased on our understanding of partitioning in quick sort, we will now try to write analgorithm for it, which is as follows.Step 1 − Choose the highest index value has pivotStep 2 − Take two variables to point left and right of the list excluding pivotStep 3 − left points to the low indexStep 4 − right points to the highStep 5 − while value at left is less than pivot move rightStep 6 − while value at right is greater than pivot move leftStep 7 − if both step 5 and step 6 does not match swap left and rightStep 8 − if left ≥ right, the point where they met is new pivot26. Quick Sort
  • 178.
    Data Structures &Algorithms169QuickSortPivotPseudocodeThe pseudocode for the above algorithm can be derived as −function partitionFunc(left, right, pivot)leftPointer = left -1rightPointer = rightwhile True dowhile A[++leftPointer] < pivot do//do-nothingend whilewhile rightPointer > 0 && A[--rightPointer] > pivot do//do-nothingend whileif leftPointer >= rightPointerbreakelseswap leftPointer,rightPointerend ifend whileswap leftPointer,rightreturn leftPointerend functionQuickSortAlgorithmUsing pivot algorithm recursively, we end up with smaller possible partitions. Eachpartition is then processed for quick sort. We define recursive algorithm for quicksort asfollows −Step 1 − Make the right-most index value pivotStep 2 − partition the array using pivot valueStep 3 − quicksort left partition recursivelyStep 4 − quicksort right partition recursively
  • 179.
    Data Structures &Algorithms170QuickSortPseudocodeTo get more into it, let see the pseudocode for quick sort algorithm −procedure quickSort(left, right)if right-left <= 0returnelsepivot = A[right]partition = partitionFunc(left, right, pivot)quickSort(left,partition-1)quickSort(partition+1,right)end ifend procedureTo know about quick sort implementation in C programming language, please click here.QuickSortPrograminCQuick sort is a highly efficient sorting algorithm and is based on partitioning of array ofdata into smaller arrays. A large array is partitioned into two arrays one of which holdsvalues smaller than the specified value, say pivot, based on which the partition is madeand another array holds values greater than the pivot value.Implementation in C#include <stdio.h>#include <stdbool.h>#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count){int i;for(i = 0;i <count-1;i++){printf("=");}
  • 180.
    Data Structures &Algorithms171printf("=n");}void display(){int i;printf("[");// navigate through all itemsfor(i = 0;i<MAX;i++){printf("%d ",intArray[i]);}printf("]n");}void swap(int num1, int num2){int temp = intArray[num1];intArray[num1] = intArray[num2];intArray[num2] = temp;}int partition(int left, int right, int pivot){int leftPointer = left -1;int rightPointer = right;while(true){while(intArray[++leftPointer] < pivot){//do nothing}while(rightPointer > 0 && intArray[--rightPointer] > pivot){//do nothing}if(leftPointer >= rightPointer){break;}else{
  • 181.
    Data Structures &Algorithms172printf(" item swapped :%d,%dn",intArray[leftPointer],intArray[rightPointer]);swap(leftPointer,rightPointer);}}printf(" pivot swapped :%d,%dn", intArray[leftPointer],intArray[right]);swap(leftPointer,right);printf("Updated Array: ");display();return leftPointer;}void quickSort(int left, int right){if(right-left <= 0){return;}else {int pivot = intArray[right];int partitionPoint = partition(left, right, pivot);quickSort(left,partitionPoint-1);quickSort(partitionPoint+1,right);}}main(){printf("Input Array: ");display();printline(50);quickSort(0,MAX-1);printf("Output Array: ");display();printline(50);}
  • 182.
    Data Structures &Algorithms173If we compile and run the above program, it will produce the following result −Input Array: [4 6 3 2 1 9 7 ]==================================================pivot swapped :9,7Updated Array: [4 6 3 2 1 7 9 ]pivot swapped :4,1Updated Array: [1 6 3 2 4 7 9 ]item swapped :6,2pivot swapped :6,4Updated Array: [1 2 3 4 6 7 9 ]pivot swapped :3,3Updated Array: [1 2 3 4 6 7 9 ]Output Array: [1 2 3 4 6 7 9 ]==================================================
  • 183.
    Data Structures &Algorithms174Graph Data Structure
  • 184.
    Data Structures &Algorithms175A graph is a pictorial representation of a set of objects where some pairs of objects areconnected by links. The interconnected objects are represented by points termedas vertices, and the links that connect the vertices are called edges.Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set ofedges, connecting the pairs of vertices. Take a look at the following graph −In the above graph,V = {a, b, c, d, e}E = {ab, ac, bd, cd, de}GraphDataStructureMathematical graphs can be represented in data structure. We can represent a graph usingan array of vertices and a two-dimensional array of edges. Before we proceed further, let'sfamiliarize ourselves with some important terms − Vertex − Each node of the graph is represented as a vertex. In the followingexample, the labeled circle represents vertices. Thus, A to G are vertices. We canrepresent them using an array as shown in the following image. Here A can beidentified by index 0. B can be identified using index 1 and so on.27. Graphs
  • 185.
    Data Structures &Algorithms176 Edge − Edge represents a path between two vertices or a line between twovertices. In the following example, the lines from A to B, B to C, and so onrepresents edges. We can use a two-dimensional array to represent an array asshown in the following image. Here AB can be represented as 1 at row 0, column1, BC as 1 at row 1, column 2 and so on, keeping other combinations as 0. Adjacency − Two node or vertices are adjacent if they are connected to eachother through an edge. In the following example, B is adjacent to A, C is adjacentto B, and so on. Path − Path represents a sequence of edges between the two vertices. In thefollowing example, ABCD represents a path from A to D.
  • 186.
    Data Structures &Algorithms177BasicOperationsFollowing are the basic primary operations that can be performed on 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.To know more about Graph, please read Graph Theory Tutorial. We shall learn abouttraversing a graph in the coming chapters.
  • 187.
    Data Structures &Algorithms178Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses astack to remember to get the next vertex to start a search, when a dead end occurs inany iteration.As in the example given above, DFS algorithm traverses from A to B to C to D first thento E, then to F and lastly to G. It employs the following rules. Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push itin a stack. Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It willpop up all the vertices from the stack, which do not have adjacent vertices.) Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.28. Depth First Traversal
  • 188.
    Data Structures &Algorithms179Steps Traversal Description1. Initialize the stack.2.Mark S as visited and put itonto the stack. Explore anyunvisited adjacent nodefrom S. We have three nodesand we can pick any of them.For this example, we shalltake the node in analphabetical order.3.Mark A as visited and put itonto the stack. Explore anyunvisited adjacent node fromA. Both S and D are adjacentto A but we are concerned forunvisited nodes only.
  • 189.
    Data Structures &Algorithms1804.Visit D and mark it as visitedand put onto the stack. Here,we have B and C nodes, whichare adjacent to D and bothare unvisited. However, weshall again choose in analphabetical order.5.We choose B, mark it asvisited and put onto the stack.Here B does not have anyunvisited adjacent node. So,we pop B from the stack.6.We check the stack top forreturn to the previous nodeand check if it has anyunvisited nodes. Here, wefind D to be on the top of thestack.7.Only unvisited adjacent nodeis from D is C now. So wevisit C, mark it as visited andput it onto the stack.
  • 190.
    Data Structures &Algorithms181As C does not have any unvisited adjacent node so we keep popping the stack until wefind a node that has an unvisited adjacent node. In this case, there's none and we keeppopping until the stack is empty.To know about the implementation of this algorithm in C programming language, clickhere.DepthFirstTraversalinCWe shall not see the implementation of Depth First Traversal (or Depth First Search) in Cprogramming language. For our reference purpose, we shall follow our example and takethis as our graph model −Implementation in C#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#define MAX 5struct Vertex {char label;bool visited;};
  • 191.
    Data Structures &Algorithms182//stack variablesint stack[MAX];int top = -1;//graph variables//array of verticesstruct Vertex* lstVertices[MAX];//adjacency matrixint adjMatrix[MAX][MAX];//vertex countint vertexCount = 0;//stack functionsvoid push(int item) {stack[++top] = item;}int pop() {return stack[top--];}int peek() {return stack[top];}bool isStackEmpty() {return top == -1;}
  • 192.
    Data Structures &Algorithms183//graph functions//add vertex to the vertex listvoid addVertex(char label) {struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));vertex->label = label;vertex->visited = false;lstVertices[vertexCount++] = vertex;}//add edge to edge arrayvoid addEdge(int start,int end) {adjMatrix[start][end] = 1;adjMatrix[end][start] = 1;}//display the vertexvoid displayVertex(int vertexIndex) {printf("%c ",lstVertices[vertexIndex]->label);}//get the adjacent unvisited vertexint getAdjUnvisitedVertex(int vertexIndex) {int i;for(i = 0; i<vertexCount; i++) {if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false) {return i;}}return -1;}
  • 193.
    Data Structures &Algorithms184void depthFirstSearch() {int i;//mark first node as visitedlstVertices[0]->visited = true;//display the vertexdisplayVertex(0);//push vertex index in stackpush(0);while(!isStackEmpty()) {//get the unvisited vertex of vertex which is at top of the stackint unvisitedVertex = getAdjUnvisitedVertex(peek());//no adjacent vertex foundif(unvisitedVertex == -1) {pop();}else {lstVertices[unvisitedVertex]->visited = true;displayVertex(unvisitedVertex);push(unvisitedVertex);}}//stack is empty, search is complete, reset the visited flagfor(i = 0;i < vertexCount;i++) {lstVertices[i]->visited = false;}}
  • 194.
    Data Structures &Algorithms185int main() {int i, j;for(i = 0; i<MAX; i++) // set adjacency {for(j = 0; j<MAX; j++) // matrix to 0adjMatrix[i][j] = 0;}addVertex('S'); // 0addVertex('A'); // 1addVertex('B'); // 2addVertex('C'); // 3addVertex('D'); // 4addEdge(0, 1); // S - AaddEdge(0, 2); // S - BaddEdge(0, 3); // S - CaddEdge(1, 4); // A - DaddEdge(2, 4); // B - DaddEdge(3, 4); // C - Dprintf("Depth First Search: ");depthFirstSearch();return 0;}If we compile and run the above program, it will produce the following result −Depth First Search: S A D B C
  • 195.
    Data Structures &Algorithms186Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and usesa queue to remember to get the next vertex to start a search, when a dead end occurs inany iteration.As in the example given above, BFS algorithm traverses from A to B to E to F first then toC and G lastly to D. It employs the following rules. Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insertit in a queue. Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue. Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.29. Breadth First Traversal
  • 196.
    Data Structures &Algorithms187Steps Traversal Description1. Initialize the queue.2.We start from visiting S(starting node), and mark itas visited.3.We then see an unvisitedadjacent node from S. In thisexample, we have three nodesbut alphabetically wechoose A, mark it as visitedand enqueue it.4.Next, the unvisited adjacentnode from S is B. We mark itas visited and enqueue it.
  • 197.
    Data Structures &Algorithms1885.Next, the unvisited adjacentnode from S is C. We mark itas visited and enqueue it.6.Now, S is left with nounvisited adjacent nodes. So,we dequeue and find A.7.From A we have D asunvisited adjacent node. Wemark it as visited andenqueue it.At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithmwe keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied,the program is over.The implementation of this algorithm in C programming language can be seen here.BreadthFirstTraversalinCWe shall not see the implementation of Breadth First Traversal (or Breadth First Search)in C programming language. For our reference purpose, we shall follow our example andtake this as our graph model −
  • 198.
    Data Structures &Algorithms189Implementation in C#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#define MAX 5struct Vertex {char label;bool visited;};//queue variablesint queue[MAX];int rear = -1;int front = 0;int queueItemCount = 0;//graph variables
  • 199.
    Data Structures &Algorithms190//array of verticesstruct Vertex* lstVertices[MAX];//adjacency matrixint adjMatrix[MAX][MAX];//vertex countint vertexCount = 0;//queue functionsvoid insert(int data) {queue[++rear] = data;queueItemCount++;}int removeData() {queueItemCount--;return queue[front++];}bool isQueueEmpty() {return queueItemCount == 0;}//graph functions//add vertex to the vertex listvoid addVertex(char label) {struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));vertex->label = label;vertex->visited = false;lstVertices[vertexCount++] = vertex;}
  • 200.
    Data Structures &Algorithms191//add edge to edge arrayvoid addEdge(int start,int end) {adjMatrix[start][end] = 1;adjMatrix[end][start] = 1;}//display the vertexvoid displayVertex(int vertexIndex) {printf("%c ",lstVertices[vertexIndex]->label);}//get the adjacent unvisited vertexint getAdjUnvisitedVertex(int vertexIndex) {int i;for(i = 0; i<vertexCount; i++) {if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false)return i;}return -1;}void breadthFirstSearch() {int i;//mark first node as visitedlstVertices[0]->visited = true;//display the vertexdisplayVertex(0);
  • 201.
    Data Structures &Algorithms192//insert vertex index in queueinsert(0);int unvisitedVertex;while(!isQueueEmpty()) {//get the unvisited vertex of vertex which is at front of the queueint tempVertex = removeData();//no adjacent vertex foundwhile((unvisitedVertex = getAdjUnvisitedVertex(tempVertex)) != -1) {lstVertices[unvisitedVertex]->visited = true;displayVertex(unvisitedVertex);insert(unvisitedVertex);}}//queue is empty, search is complete, reset the visited flagfor(i = 0;i<vertexCount;i++) {lstVertices[i]->visited = false;}}int main() {int i, j;for(i = 0; i<MAX; i++) // set adjacency {for(j = 0; j<MAX; j++) // matrix to 0adjMatrix[i][j] = 0;}addVertex('S'); // 0addVertex('A'); // 1addVertex('B'); // 2addVertex('C'); // 3addVertex('D'); // 4
  • 202.
    Data Structures &Algorithms193addEdge(0, 1); // S - AaddEdge(0, 2); // S - BaddEdge(0, 3); // S - CaddEdge(1, 4); // A - DaddEdge(2, 4); // B - DaddEdge(3, 4); // C - Dprintf("nBreadth First Search: ");breadthFirstSearch();return 0;}If we compile and run the above program, it will produce the following result −Breadth First Search: S A B C D
  • 203.
    Data Structures &Algorithms194Tree Data Structure
  • 204.
    Data Structures &Algorithms195Tree represents the nodes connected by edges. We will discuss binary tree or binary searchtree specifically.Binary Tree is a special datastructure used for data storage purposes. A binary tree has aspecial condition that each node can have a maximum of two children. A binary tree hasthe benefits of both an ordered array and a linked list as search is as quick as in a sortedarray and insertion or deletion operation are as fast as in linked list.ImportantTermsFollowing are the important terms with respect to tree. Path − Path refers to the sequence of nodes along the edges of a tree. Root – The node at the top of the tree is called root. There is only one root pertree and one path from the root node to any node. Parent − Any node except the root node has one edge upward to a node calledparent. Child – The node below a given node connected by its edge downward is called itschild node. Leaf – The node which does not have any child node is called the leaf node. Subtree − Subtree represents the descendants of a node.30. Tree
  • 205.
    Data Structures &Algorithms196 Visiting − Visiting refers to checking the value of a node when control is on thenode. Traversing − Traversing means passing through nodes in a specific order. Levels − Level of a node represents the generation of a node. If the root node isat level 0, then its next child node is at level 1, its grandchild is at level 2, and soon. Keys − Key represents a value of a node based on which a search operation is tobe carried out for a node.BinarySearchTreeRepresentationBinary Search tree exhibits a special behavior. A node's left child must have a value lessthan its parent's value and the node's right child must have a value greater than its parentvalue.We're going to implement tree using node object and connecting them through references.TreeNodeThe code to write a tree node would be similar to what is given below. It has a data partand references to its left and right child nodes.struct node {int data;struct node *leftChild;struct node *rightChild;};
  • 206.
    Data Structures &Algorithms197In a tree, all nodes share common construct.BSTBasicOperationsThe basic operations that can be performed on a binary search tree data structure, arethe following − Insert − Inserts an element in a tree/create a tree. Search − Searches an element in a tree. Pre-order Traversal − Traverses a tree in a pre-order manner. In-order Traversal − Traverses a tree in an in-order manner. Post-order Traversal − Traverses a tree in a post-order manner.We shall learn creating (inserting into) a tree structure and searching a data item in a treein this chapter. We shall learn about tree traversing methods in the coming chapter.InsertOperationThe very first insertion creates the tree. Afterwards, whenever an element is to beinserted, first locate its proper location. Start searching from the root node, then if thedata is less than the key value, search for the empty location in the left subtree and insertthe data. Otherwise, search for the empty location in the right subtree and insert the data.AlgorithmIf root is NULLthen create root nodereturnIf root exists thencompare the data with node.datawhile until insertion position is locatedIf data is greater than node.datagoto right subtreeelsegoto left subtree
  • 207.
    Data Structures &Algorithms198endwhileinsert dataend IfImplementationThe implementation of insert function should look like this −void insert(int data) {struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is empty, create root nodeif(root == NULL) {root = tempNode;}else {current = root;parent = NULL;while(1) {parent = current;//go to left of the treeif(data < parent->data) {current = current->leftChild;//insert to the leftif(current == NULL) {parent->leftChild = tempNode;return;}
  • 208.
    Data Structures &Algorithms199}//go to right of the treeelse {current = current->rightChild;//insert to the rightif(current == NULL) {parent->rightChild = tempNode;return;}}}}}SearchOperationWhenever an element is to be searched, start searching from the root node, then if thedata is less than the key value, search for the element in the left subtree. Otherwise,search for the element in the right subtree. Follow the same algorithm for each node.AlgorithmIf root.data is equal to search.datareturn rootelsewhile data not foundIf data is greater than node.datagoto right subtreeelsegoto left subtreeIf data foundreturn nodeendwhile
  • 209.
    Data Structures &Algorithms200return data not foundend ifThe implementation of this algorithm should look like this.struct node* search(int data) {struct node *current = root;printf("Visiting elements: ");while(current->data != data) {if(current != NULL)printf("%d ",current->data);//go to left treeif(current->data > data) {current = current->leftChild;}//else go to right treeelse {current = current->rightChild;}//not foundif(current == NULL) {return NULL;}return current;}}To know about the implementation of binary search tree data structure, please click here.TreeTraversalinCTraversal is a process to visit all the nodes of a tree and may print their values too.Because, all nodes are connected via edges (links) we always start from the root (head)
  • 210.
    Data Structures &Algorithms201node. That is, we cannot random access a node in a tree. There are three ways which weuse to traverse a tree − In-order Traversal Pre-order Traversal Post-order TraversalWe shall now look at the implementation of tree traversal in C programming language hereusing the following binary tree −Implementation in C#include <stdio.h>#include <stdlib.h>struct node {int data;struct node *leftChild;struct node *rightChild;};struct node *root = NULL;void insert(int data) {struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;
  • 211.
    Data Structures &Algorithms202tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is emptyif(root == NULL) {root = tempNode;}else {current = root;parent = NULL;while(1) {parent = current;//go to left of the treeif(data < parent->data) {current = current->leftChild;//insert to the leftif(current == NULL) {parent->leftChild = tempNode;return;}}//go to right of the treeelse {current = current->rightChild;//insert to the rightif(current == NULL) {parent->rightChild = tempNode;return;}}}}}struct node* search(int data) {struct node *current = root;
  • 212.
    Data Structures &Algorithms203printf("Visiting elements: ");while(current->data != data) {if(current != NULL)printf("%d ",current->data);//go to left treeif(current->data > data) {current = current->leftChild;}//else go to right treeelse {current = current->rightChild;}//not foundif(current == NULL) {return NULL;}}return current;}void pre_order_traversal(struct node* root) {if(root != NULL) {printf("%d ",root->data);pre_order_traversal(root->leftChild);pre_order_traversal(root->rightChild);}}void inorder_traversal(struct node* root) {if(root != NULL) {inorder_traversal(root->leftChild);printf("%d ",root->data);inorder_traversal(root->rightChild);
  • 213.
    Data Structures &Algorithms204}}void post_order_traversal(struct node* root) {if(root != NULL) {post_order_traversal(root->leftChild);post_order_traversal(root->rightChild);printf("%d ", root->data);}}int main() {int i;int array[7] = { 27, 14, 35, 10, 19, 31, 42 };for(i = 0; i < 7; i++)insert(array[i]);i = 31;struct node * temp = search(i);if(temp != NULL) {printf("[%d] Element found.", temp->data);printf("n");}else {printf("[ x ] Element not found (%d).n", i);}i = 15;temp = search(i);if(temp != NULL) {printf("[%d] Element found.", temp->data);printf("n");}else {printf("[ x ] Element not found (%d).n", i);}
  • 214.
    Data Structures &Algorithms205printf("nPreorder traversal: ");pre_order_traversal(root);printf("nInorder traversal: ");inorder_traversal(root);printf("nPost order traversal: ");post_order_traversal(root);return 0;}If we compile and run the above program, it will produce the following result −Visiting elements: 27 35 [31] Element found.Visiting elements: 27 14 19 [ x ] Element not found (15).Preorder traversal: 27 14 10 19 35 31 42Inorder traversal: 10 14 19 27 31 35 42Post order traversal: 10 19 14 31 42 35 27
  • 215.
    Data Structures &Algorithms206Traversal is a process to visit all the nodes of a tree and may print their values too.Because, all nodes are connected via edges (links) we always start from the root (head)node. That is, we cannot randomly access a node in a tree. There are three ways whichwe use to traverse a tree − In-order Traversal Pre-order Traversal Post-order TraversalGenerally, we traverse a tree to search or locate a given item or key in the tree or to printall the values it contains.In-orderTraversalIn this traversal method, the left subtree is visited first, then the root and later the rightsub-tree. We should always remember that every node may represent a subtree itself.If a binary tree is traversed in-order, the output will produce sorted key values in anascending order.31. Tree Traversal
  • 216.
    Data Structures &Algorithms207We start from A, and following in-order traversal, we move to its left subtree B. B is alsotraversed in-order. The process goes on until all the nodes are visited. The output of in-order traversal of this tree will be −D → B → E → A → F → C → GAlgorithmUntil all nodes are traversed −Step 1 − Recursively traverse left subtree.Step 2 − Visit root node.Step 3 − Recursively traverse right subtree.Pre-orderTraversalIn this traversal method, the root node is visited first, then the left subtree and finally theright subtree.We start from A, and following pre-order traversal, we first visit A itself and then move toits left subtree B. B is also traversed pre-order. The process goes on until all the nodesare visited. The output of pre-order traversal of this tree will be −A → B → D → E → C → F → G
  • 217.
    Data Structures &Algorithms208AlgorithmUntil all nodes are traversed −Step 1 − Visit root node.Step 2 − Recursively traverse left subtree.Step 3 − Recursively traverse right subtree.Post-orderTraversalIn this traversal method, the root node is visited last, hence the name. First we traversethe left subtree, then the right subtree and finally the root node.We start from A, and following pre-order traversal, we first visit the left subtree B. B isalso traversed post-order. The process goes on until all the nodes are visited. The outputof post-order traversal of this tree will be −D → E → B → F → G → C → A
  • 218.
    Data Structures &Algorithms209AlgorithmUntil all nodes are traversed −Step 1 − Recursively traverse left subtree.Step 2 − Recursively traverse right subtree.Step 3 − Visit root node.To check the C implementation of tree traversing, please click hereTreeTraversalinCTraversal is a process to visit all the nodes of a tree and may print their values too.Because, all nodes are connected via edges (links) we always start from the root (head)node. That is, we cannot randomly access a node in a tree. There are three ways whichwe use to traverse a tree − In-order Traversal Pre-order Traversal Post-order TraversalWe shall now see the implementation of tree traversal in C programming language hereusing the following binary tree −
  • 219.
    Data Structures &Algorithms210Implementation in C#include <stdio.h>#include <stdlib.h>struct node {int data;struct node *leftChild;struct node *rightChild;};struct node *root = NULL;void insert(int data) {struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is emptyif(root == NULL) {root = tempNode;}else {current = root;parent = NULL;while(1) {parent = current;//go to left of the treeif(data < parent->data) {current = current->leftChild;
  • 220.
    Data Structures &Algorithms211//insert to the leftif(current == NULL) {parent->leftChild = tempNode;return;}}//go to right of the treeelse {current = current->rightChild;//insert to the rightif(current == NULL) {parent->rightChild = tempNode;return;}}}}}struct node* search(int data) {struct node *current = root;printf("Visiting elements: ");while(current->data != data) {if(current != NULL)printf("%d ",current->data);//go to left treeif(current->data > data) {current = current->leftChild;}//else go to right treeelse {current = current->rightChild;}//not found
  • 221.
    Data Structures &Algorithms212if(current == NULL) {return NULL;}}return current;}void pre_order_traversal(struct node* root) {if(root != NULL) {printf("%d ",root->data);pre_order_traversal(root->leftChild);pre_order_traversal(root->rightChild);}}void inorder_traversal(struct node* root) {if(root != NULL) {inorder_traversal(root->leftChild);printf("%d ",root->data);inorder_traversal(root->rightChild);}}void post_order_traversal(struct node* root) {if(root != NULL) {post_order_traversal(root->leftChild);post_order_traversal(root->rightChild);printf("%d ", root->data);}}int main() {
  • 222.
    Data Structures &Algorithms213int i;int array[7] = { 27, 14, 35, 10, 19, 31, 42 };for(i = 0; i < 7; i++)insert(array[i]);i = 31;struct node * temp = search(i);if(temp != NULL) {printf("[%d] Element found.", temp->data);printf("n");}else {printf("[ x ] Element not found (%d).n", i);}i = 15;temp = search(i);if(temp != NULL) {printf("[%d] Element found.", temp->data);printf("n");}else {printf("[ x ] Element not found (%d).n", i);}printf("nPreorder traversal: ");pre_order_traversal(root);printf("nInorder traversal: ");inorder_traversal(root);printf("nPost order traversal: ");post_order_traversal(root);return 0;}
  • 223.
    Data Structures &Algorithms214If we compile and run the above program, it will produce the following result −Visiting elements: 27 35 [31] Element found.Visiting elements: 27 14 19 [ x ] Element not found (15).Preorder traversal: 27 14 10 19 35 31 42Inorder traversal: 10 14 19 27 31 35 42Post order traversal: 10 19 14 31 42 35 27
  • 224.
    Data Structures &Algorithms215A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentionedproperties − The left sub-tree of a node has a key less than or equal to its parent node's key. The right sub-tree of a node has a key greater than or equal to its parent node'skey.Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and can be defined as −left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)RepresentationBST is a collection of nodes arranged in a way where they maintain BST properties. Eachnode has a key and an associated value. While searching, the desired key is compared tothe keys in BST and if found, the associated value is retrieved.Following is a pictorial representation of BST −We observe that the root node key (27) has all less-valued keys on the left sub-tree andthe higher valued keys on the right sub-tree.32. Binary Search Tree
  • 225.
    Data Structures &Algorithms216BasicOperationsFollowing are the basic operations of a tree - Search − Searches an element in a tree. Insert − Inserts an element in a tree. Pre-order Traversal − Traverses a tree in a pre-order manner. In-order Traversal − Traverses a tree in an in-order manner. Post-order Traversal − Traverses a tree in a post-order manner.NodeDefine a node having some data, references to its left and right child nodes.struct node {int data;struct node *leftChild;struct node *rightChild;};SearchOperationWhenever an element is to be searched, start searching from the root node. Then if thedata is less than the key value, search for the element in the left subtree. Otherwise,search for the element in the right subtree. Follow the same algorithm for each node.struct node* search(int data){struct node *current = root;printf("Visiting elements: ");while(current->data != data){if(current != NULL) {printf("%d ",current->data);//go to left treeif(current->data > data){current = current->leftChild;
  • 226.
    Data Structures &Algorithms217}//else go to right treeelse {current = current->rightChild;}//not foundif(current == NULL){return NULL;}}}return current;}InsertOperationWhenever an element is to be inserted, first locate its proper location. Start searchingfrom the root node, then if the data is less than the key value, search for the emptylocation in the left subtree and insert the data. Otherwise, search for the empty locationin the right subtree and insert the data.void insert(int data){struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is emptyif(root == NULL){root = tempNode;}else {current = root;parent = NULL;while(1){parent = current;
  • 227.
    Data Structures &Algorithms218//go to left of the treeif(data < parent->data){current = current->leftChild;//insert to the leftif(current == NULL){parent->leftChild = tempNode;return;}}//go to right of the treeelse{current = current->rightChild;//insert to the rightif(current == NULL){parent->rightChild = tempNode;return;}}}}}
  • 228.
    Data Structures &Algorithms219What if the input to binary search tree comes in a sorted (ascending or descending)manner? It will then look like this −It is observed that BST's worst-case performance is closest to linear search algorithms,that is Ο(n). In real-time data, we cannot predict data pattern and their frequencies. So,a need arises to balance out the existing BST.Named after their inventor Adelson, Velski & Landis, AVL trees are height balancingbinary search tree. AVL tree checks the height of the left and the right sub-trees andassures that the difference is not more than 1. This difference is called the BalanceFactor.Here we see that the first tree is balanced and the next two trees are not balanced −33. AVL Trees
  • 229.
    Data Structures &Algorithms220In the second tree, the left subtree of C has height 2 and the right subtree has height 0,so the difference is 2. In the third tree, the right subtree of A has height 2 and the left ismissing, so it is 0, and the difference is 2 again. AVL tree permits difference (balancefactor) to be only 1.BalanceFactor = height(left-sutree) − height(right-sutree)If the difference in the height of left and right sub-trees is more than 1, the tree is balancedusing some rotation techniques.AVLRotationsTo balance itself, an AVL tree may perform the following four kinds of rotations − Left rotation Right rotation Left-Right rotation Right-Left rotationThe first two rotations are single rotations and the next two rotations are double rotations.To have an unbalanced tree, we at least need a tree of height 2. With this simple tree,let's understand them one by one.Left RotationIf a tree becomes unbalanced, when a node is inserted into the right subtree of the rightsubtree, then we perform a single left rotation −In our example, node A has become unbalanced as a node is inserted in the right subtreeof A's right subtree. We perform the left rotation by making A the left-subtree of B.
  • 230.
    Data Structures &Algorithms221Right RotationAVL tree may become unbalanced, if a node is inserted in the left subtree of the leftsubtree. The tree then needs a right rotation.As depicted, the unbalanced node becomes the right child of its left child by performing aright rotation.Left-Right RotationDouble rotations are slightly complex version of already explained versions of rotations.To understand them better, we should take note of each action performed while rotation.Let's first check how to perform Left-Right rotation. A left-right rotation is a combinationof left rotation followed by right rotation.State ActionA node has been inserted into the right subtree of the leftsubtree. This makes C an unbalanced node. These scenarioscause AVL tree to perform left-right rotation.We first perform the left rotation on the left subtree of C.This makes A, the left subtree of B.
  • 231.
    Data Structures &Algorithms222Node C is still unbalanced, however now, it is because of theleft-subtree of the left-subtree.We shall now right-rotate the tree, making B the new rootnode of this subtree. C now becomes the right subtree of itsown left subtree.The tree is now balanced.Right-Left RotationThe second type of double rotation is Right-Left Rotation. It is a combination of rightrotation followed by left rotation.State ActionA node has been inserted into the left subtree of the rightsubtree. This makes A, an unbalanced node with balancefactor 2.
  • 232.
    Data Structures &Algorithms223First, we perform the right rotation along C node, making Cthe right subtree of its own left subtree B. Now, B becomesthe right subtree of A.Node A is still unbalanced because of the right subtree of itsright subtree and requires a left rotation.A left rotation is performed by making B the new root nodeof the subtree. A becomes the left subtree of its rightsubtree B.The tree is now balanced.
  • 233.
    Data Structures &Algorithms224A spanning tree is a subset of Graph G, which has all the vertices covered with minimumpossible number of edges. Hence, a spanning tree does not have cycles and it cannot bedisconnected.By this definition, we can draw a conclusion that every connected and undirected Graph Ghas at least one spanning tree. A disconnected graph does not have any spanning tree, asit cannot be spanned to all its vertices.We found three spanning trees off one complete graph. A complete undirected graph canhave maximum nn-2number of spanning trees, where n is the number of nodes. In theabove addressed example, n is 3, hence 33−2= 3 spanning trees are possible.GeneralPropertiesofSpanningTreeWe now understand that one graph can have more than one spanning tree. Following area few properties of the spanning tree connected to graph G - A connected graph G can have more than one spanning tree. All possible spanning trees of graph G, have the same number of edges andvertices. The spanning tree does not have any cycle (loops).34. Spanning Tree
  • 234.
    Data Structures &Algorithms225 Removing one edge from the spanning tree will make the graph disconnected, i.e.the spanning tree is minimally connected. Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanningtree is maximally acyclic.MathematicalPropertiesofSpanningTree Spanning tree has n-1 edges, where n is the number of nodes (vertices). From a complete graph, by removing maximum e-n+1 edges, we can construct aspanning tree. A complete graph can have maximum nn-2number of spanning trees.Thus, we can conclude that spanning trees are a subset of connected Graph G anddisconnected graphs do not have spanning tree.ApplicationofSpanningTreeSpanning tree is basically used to find a minimum path to connect all nodes in a graph.Common application of spanning trees are − Civil Network Planning Computer Network Routing Protocol Cluster AnalysisLet us understand this through a small example. Consider, city network as a huge graphand now plans to deploy telephone lines in such a way that in minimum lines we canconnect to all city nodes. This is where the spanning tree comes into picture.MinimumSpanningTree(MST)In a weighted graph, a minimum spanning tree is a spanning tree that has minimumweight than all other spanning trees of the same graph. In real-world situations, thisweight can be measured as distance, congestion, traffic load or any arbitrary valuedenoted to the edges.MinimumSpanning-TreeAlgorithmWe shall learn about two most important spanning tree algorithms here − Kruskal's Algorithm Prim's AlgorithmBoth are greedy algorithms.
  • 235.
    Data Structures &Algorithms226Kruskal'sSpanningTreeAlgorithmKruskal's algorithm to find the minimum cost spanning tree uses the greedy approach.This algorithm treats the graph as a forest and every node it has as an individual tree. Atree connects to another only and only if, it has the least cost among all available optionsand does not violate MST properties.To understand Kruskal's algorithm let us consider the following example −Step 1 - Remove all loops and parallel edgesRemove all loops and parallel edges from the given graph.
  • 236.
    Data Structures &Algorithms227In case of parallel edges, keep the one which has the least cost associated and remove allothers.Step 2 - Arrange all edges in their increasing order of weightThe next step is to create a set of edges and weight, and arrange them in an ascendingorder of weightage (cost).Step 3 - Add the edge which has the least weightageNow we start adding edges to the graph beginning from the one which has the least weight.Throughout, we shall keep checking that the spanning properties remain intact. In case,by adding one edge, the spanning tree property does not hold then we shall consider notto include the edge in the graph.
  • 237.
    Data Structures &Algorithms228The least cost is 2 and edges involved are B,D and D,T. We add them. Adding them doesnot violate spanning tree properties, so we continue to our next edge selection.Next cost is 3, and associated edges are A,C and C,D. We add them again −Next cost in the table is 4, and we observe that adding it will create a circuit in the graph.We ignore it. In the process we shall ignore/avoid all edges that create a circuit.
  • 238.
    Data Structures &Algorithms229We observe that edges with cost 5 and 6 also create circuits. We ignore them and moveon.Now we are left with only one node to be added. Between the two least cost edges available7 and 8, we shall add the edge with cost 7.By adding edge S,A we have included all the nodes of the graph and we now have minimumcost spanning tree.Prim'sSpanningTreeAlgorithmPrim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses thegreedy approach. Prim's algorithm shares a similarity with the shortest pathfirst algorithms.Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single treeand keeps on adding new nodes to the spanning tree from the given graph.
  • 239.
    Data Structures &Algorithms230To contrast with Kruskal's algorithm and to understand Prim's algorithm better, we shalluse the same example −Step 1 - Remove all loops and parallel edges
  • 240.
    Data Structures &Algorithms231Remove all loops and parallel edges from the given graph. In case of parallel edges, keepthe one which has the least cost associated and remove all others.Step 2 - Choose any arbitrary node as root nodeIn this case, we choose S node as the root node of Prim's spanning tree. This node isarbitrarily chosen, so any node can be the root node. One may wonder why any video canbe a root node. So the answer is, in the spanning tree all the nodes of a graph are includedand because it is connected then there must be at least one edge, which will join it to therest of the tree.Step 3 - Check outgoing edges and select the one with less costAfter choosing the root node S, we see that S,A and S,C are two edges with weight 7 and8, respectively. We choose the edge S,A as it is lesser than the other.
  • 241.
    Data Structures &Algorithms232Now, the tree S-7-A is treated as one node and we check for all edges going out from it.We select the one which has the lowest cost and include it in the tree.After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will checkall the edges again. However, we will choose only the least cost edge. In this case, C-3-Dis the new edge, which is less than other edges' cost 8, 6, 4, etc.After adding node D to the spanning tree, we now have two edges going out of it havingthe same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the next step willagain yield edge 2 as the least cost. Hence, we are showing a spanning tree with bothedges included.We may find that the output spanning tree of the same graph using two differentalgorithms is same.
  • 242.
    Data Structures &Algorithms233Heap is a special case of balanced binary tree data structure where the root-node key iscompared with its children and arranged accordingly. If α has child node β then −key(α) ≥ key(β)As the value of parent is greater than that of child, this property generates Max Heap.Based on this criteria, a heap can be of two types −For Input → 35 33 42 10 14 19 27 44 26 31Min-Heap − Where the value of the root node is less than or equal to either of its children.Max-Heap − Where the value of the root node is greater than or equal to either of itschildren.35. Heaps
  • 243.
    Data Structures &Algorithms234Both trees are constructed using the same input and order of arrival.MaxHeapConstructionAlgorithmWe shall use the same example to demonstrate how a Max Heap is created. The procedureto create Min Heap is similar but we go for min values instead of max values.We are going to derive an algorithm for max heap by inserting one element at a time. Atany point of time, heap must maintain its property. While insertion, we also assume thatwe are inserting a node in an already heapified tree.Step 1 − Create a new node at the end of heap.Step 2 − Assign new value to the node.Step 3 − Compare the value of this child node with its parent.Step 4 − If value of parent is less than child, then swap them.Step 5 − Repeat step 3 & 4 until Heap property holds.Note − In Min Heap construction algorithm, we expect the value of the parent node to beless than that of the child node.Let's understand Max Heap construction by an animated illustration. We consider the sameinput sample that we used earlier.
  • 244.
    Data Structures &Algorithms235MaxHeapDeletionAlgorithmLet us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap alwayshappens at the root to remove the Maximum (or minimum) value.Step 1 − Remove root node.Step 2 − Move the last element of last level to root.Step 3 − Compare the value of this child node with its parent.Step 4 − If value of parent is less than child, then swap them.Step 5 − Repeat step 3 & 4 until Heap property holds.
  • 245.
    Data Structures &Algorithms236Recursion
  • 246.
    Data Structures &Algorithms237Some computer programming languages allow a module or function to call itself. Thistechnique is known as recursion. In recursion, a function α either calls itself directly orcalls a function β that in turn calls the original function α. The function α is called recursivefunction.Example − a function calling itself.int function(int value) {if(value < 1)return;function(value - 1);printf("%d ",value);}Example − a function that calls another function which in turn calls it again.int function(int value) {if(value < 1)return;function(value - 1);printf("%d ",value);}PropertiesA recursive function can go infinite like a loop. To avoid infinite running of recursivefunction, there are two properties that a recursive function must have − Base criteria − There must be at least one base criteria or condition, such that,when this condition is met the function stops calling itself recursively. Progressive approach − The recursive calls should progress in such a way thateach time a recursive call is made it comes closer to the base criteria.36. Recursion ─ Basics
  • 247.
    Data Structures &Algorithms238ImplementationMany programming languages implement recursion by means of stacks. Generally,whenever a function (caller) calls another function (callee) or itself as callee, the callerfunction transfers execution control to the callee. This transfer process may also involvesome data to be passed from the caller to the callee.This implies, the caller function has to suspend its execution temporarily and resume laterwhen the execution control returns from the callee function. Here, the caller function needsto start exactly from the point of execution where it puts itself on hold. It also needs theexact same data values it was working on. For this purpose, an activation record (or stackframe) is created for the caller function.This activation record keeps the information about local variables, formal parameters,return address and all information passed to the caller function.AnalysisofRecursionOne may argue why to use recursion, as the same task can be done with iteration. Thefirst reason is, recursion makes a program more readable and because of latest enhancedCPU systems, recursion is more efficient than iterations.TimeComplexityIn case of iterations, we take number of iterations to count the time complexity. Likewise,in case of recursion, assuming everything is constant, we try to figure out the number oftimes a recursive call is being made. A call made to a function is Ο(1), hence the (n)number of times a recursive call is made makes the recursive function Ο(n).
  • 248.
    Data Structures &Algorithms239SpaceComplexitySpace complexity is counted as what amount of extra space is required for a module toexecute. In case of iterations, the compiler hardly requires any extra space. The compilerkeeps updating the values of variables used in the iterations. But in case of recursion, thesystem needs to store activation record each time a recursive call is made. Hence, it isconsidered that space complexity of recursive function may go higher than that of afunction with iteration.
  • 249.
    Data Structures &Algorithms240Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and morethan one rings is as depicted −These rings are of different sizes and stacked upon in an ascending order, i.e. the smallerone sits over the larger one. There are other variations of the puzzle where the number ofdisks increase, but the tower count remains the same.RulesThe mission is to move all the disks to some another tower without violating the sequenceof arrangement. A few rules to be followed for Tower of Hanoi are − Only one disk can be moved among the towers at any given time. Only the "top" disk can be removed. No large disk can sit over a small disk.37. Tower of Hanoi
  • 250.
    Data Structures &Algorithms241Following is an animated representation of solving a Tower of Hanoi puzzle with threedisks.
  • 251.
    Data Structures &Algorithms242
  • 252.
    Data Structures &Algorithms243Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. Thispresentation shows that a puzzle with 3 disks has taken 23−1 = 7 steps.
  • 253.
    Data Structures &Algorithms244AlgorithmTo write an algorithm for Tower of Hanoi, first we need to learn how to solve this problemwith lesser amount of disks, say → 1 or 2. We mark three towers withname, source, destination and aux (only to help moving the disks). If we have only onedisk, then it can easily be moved from source to destination peg.If we have 2 disks – First, we move the smaller (top) disk to aux peg. Then, we move the larger (bottom) disk to destination peg. And finally, we move the smaller disk from aux to destination peg.
  • 254.
    Data Structures &Algorithms245
  • 255.
    Data Structures &Algorithms246So now, we are in a position to design an algorithm for Tower of Hanoi with more thantwo disks. We divide the stack of disks in two parts. The largest disk (nthdisk) is in onepart and all other (n-1) disks are in the second part.Our ultimate aim is to move disk n from source to destination and then put all other (n-1) disks onto it. We can imagine to apply the same in a recursive way for all given set ofdisks.The steps to follow are −Step 1 − Move n-1 disks from source to auxStep 2 − Move nth disk from source to destStep 3 − Move n-1 disks from aux to destA recursive algorithm for Tower of Hanoi can be driven as follows −STARTProcedure Hanoi(disk, source, dest, aux)IF disk == 0, THENmove disk from source to destELSEHanoi(disk - 1, source, aux, dest) // Step 1move disk from source to dest // Step 2Hanoi(disk - 1, aux, dest, source) // Step 3END IFEND ProcedureSTOPTo check the implementation in C programming, click here.
  • 256.
    Data Structures &Algorithms247TowerofHanoiinCProgram#include <stdio.h>#include <stdbool.h>#define MAX 10int list[MAX] = {1,8,4,6,0,3,5,2,7,9};void display(){int i;printf("[");// navigate through all itemsfor(i = 0; i < MAX; i++){printf("%d ",list[i]);}printf("]n");}void bubbleSort() {int temp;int i,j;bool swapped = false;// loop through all numbersfor(i = 0; i < MAX-1; i++) {swapped = false;// loop through numbers falling aheadfor(j = 0; j < MAX-1-i; j++) {printf("Items compared: [ %d, %d ] ", list[j],list[j+1]);
  • 257.
    Data Structures &Algorithms248// check if next number is lesser than current no// swap the numbers.// (Bubble up the highest number)if(list[j] > list[j+1]) {temp = list[j];list[j] = list[j+1];list[j+1] = temp;swapped = true;printf(" => swapped [%d, %d]n",list[j],list[j+1]);}else {printf(" => not swappedn");}}// if no number was swapped that means// array is sorted now, break the loop.if(!swapped) {break;}printf("Iteration %d#: ",(i+1));display();}}main(){printf("Input Array: ");display();printf("n");bubbleSort();printf("nOutput Array: ");display();}
  • 258.
    Data Structures &Algorithms249If we compile and run the above program, it will produce the following result −Input Array: [1 8 4 6 0 3 5 2 7 9 ]Items compared: [ 1, 8 ] => not swappedItems compared: [ 8, 4 ] => swapped [4, 8]Items compared: [ 8, 6 ] => swapped [6, 8]Items compared: [ 8, 0 ] => swapped [0, 8]Items compared: [ 8, 3 ] => swapped [3, 8]Items compared: [ 8, 5 ] => swapped [5, 8]Items compared: [ 8, 2 ] => swapped [2, 8]Items compared: [ 8, 7 ] => swapped [7, 8]Items compared: [ 8, 9 ] => not swappedIteration 1#: [1 4 6 0 3 5 2 7 8 9 ]Items compared: [ 1, 4 ] => not swappedItems compared: [ 4, 6 ] => not swappedItems compared: [ 6, 0 ] => swapped [0, 6]Items compared: [ 6, 3 ] => swapped [3, 6]Items compared: [ 6, 5 ] => swapped [5, 6]Items compared: [ 6, 2 ] => swapped [2, 6]Items compared: [ 6, 7 ] => not swappedItems compared: [ 7, 8 ] => not swappedIteration 2#: [1 4 0 3 5 2 6 7 8 9 ]Items compared: [ 1, 4 ] => not swappedItems compared: [ 4, 0 ] => swapped [0, 4]Items compared: [ 4, 3 ] => swapped [3, 4]Items compared: [ 4, 5 ] => not swappedItems compared: [ 5, 2 ] => swapped [2, 5]Items compared: [ 5, 6 ] => not swappedItems compared: [ 6, 7 ] => not swappedIteration 3#: [1 0 3 4 2 5 6 7 8 9 ]Items compared: [ 1, 0 ] => swapped [0, 1]Items compared: [ 1, 3 ] => not swappedItems compared: [ 3, 4 ] => not swappedItems compared: [ 4, 2 ] => swapped [2, 4]Items compared: [ 4, 5 ] => not swappedItems compared: [ 5, 6 ] => not swapped
  • 259.
    Data Structures &Algorithms250Iteration 4#: [0 1 3 2 4 5 6 7 8 9 ]Items compared: [ 0, 1 ] => not swappedItems compared: [ 1, 3 ] => not swappedItems compared: [ 3, 2 ] => swapped [2, 3]Items compared: [ 3, 4 ] => not swappedItems compared: [ 4, 5 ] => not swappedIteration 5#: [0 1 2 3 4 5 6 7 8 9 ]Items compared: [ 0, 1 ] => not swappedItems compared: [ 1, 2 ] => not swappedItems compared: [ 2, 3 ] => not swappedItems compared: [ 3, 4 ] => not swappedOutput Array: [0 1 2 3 4 5 6 7 8 9 ]
  • 260.
    Data Structures &Algorithms251Fibonacci series generates the subsequent number by adding two previous numbers.Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can betaken as 0, 1 or 1, 1 respectively.Fibonacci series satisfies the following conditions −Fn = Fn-1 + Fn-2Hence, a Fibonacci series can look like this −F8 = 0 1 1 2 3 5 8 13or, this −F8 = 1 1 2 3 5 8 13 21For illustration purpose, Fibonacci of F8 is displayed as −38. Fibonacci Series
  • 261.
    Data Structures &Algorithms252FibonacciIterativeAlgorithmFirst we try to draft the iterative algorithm for Fibonacci series.Procedure Fibonacci(n)declare f0, f1, fib, loopset f0 to 0set f1 to 1display f0, f1for loop ← 1 to nfib ← f0 + f1f0 ← f1f1 ← fibdisplay fibend forend procedureTo know about the implementation of the above algorithm in C programminglanguage, click here.FibonacciInteractivePrograminCFibonacci Program in CRecursionDemo.c#include <stdio.h>int factorial(int n){//base caseif(n == 0){return 1;
  • 262.
    Data Structures &Algorithms253}else {return n * factorial(n-1);}}int fibbonacci(int n){if(n == 0){return 0;}else if(n == 1){return 1;}else {return (fibbonacci(n-1) + fibbonacci(n-2));}}main(){int n = 5;int i;printf("Factorial of %d: %dn" , n , factorial(n));printf("Fibbonacci of %d: " , n);for(i = 0;i<n;i++){printf("%d ",fibbonacci(i));}}If we compile and run the above program, it will produce the following result −Factorial of 5: 120Fibbonacci of 5: 0 1 1 2 3
  • 263.
    Data Structures &Algorithms254FibonacciRecursiveAlgorithmLet us learn how to create a recursive algorithm Fibonacci series. The base criteria ofrecursion.STARTProcedure Fibonacci(n)declare f0, f1, fib, loopset f0 to 0set f1 to 1display f0, f1for loop ← 1 to nfib ← f0 + f1f0 ← f1f1 ← fibdisplay fibend forENDTo know about the implementation of the above algorithm in C programminglanguage, click here.FibonacciRecursivePrograminCFibonacci Program in C#include <stdio.h>int factorial(int n){//base caseif(n == 0){return 1;
  • 264.
    Data Structures &Algorithms255}else {return n * factorial(n-1);}}int fibbonacci(int n){if(n == 0){return 0;}else if(n == 1){return 1;}else {return (fibbonacci(n-1) + fibbonacci(n-2));}}main(){int n = 5;int i;printf("Factorial of %d: %dn" , n , factorial(n));printf("Fibbonacci of %d: " , n);for(i = 0;i<n;i++){printf("%d ",fibbonacci(i));}}If we compile and run the above program, it will produce the following result −Factorial of 5: 120Fibbonacci of 5: 0 1 1 2 3

[8]ページ先頭

©2009-2025 Movatter.jp