## Data Structures And Algorithms

The data structures and algorithmsÂ  is the one of the Â important topic for computer science students. In computer programming , the data structureÂ  is a fundamental building block Â Â forÂ  developing an efficient software application .Â

And therefore , the programmer must have a good knowledge and understanding ofÂ  the data structures.

The Data Structure defines the Â way in which various Â program data elements are organized and stored into the memory Â so that the data can be used efficiently.

This Data Structures And Â Algorithms tutorial extensively covers Â all the important Â topics Â such as types Of Data structures , Linear And Non-Liner Data structures , Array, Pointer, Structure, Linked List, Stack, Queue, Graph .Â

The data operations such asÂ  Traversing , Searching , Sorting And other topics related to the algorithms.

## What Is Data Structure ?

The data structureÂ  is the most fundamental building blockÂ  inÂ  computer science . AndÂ  therefore ,Â  aÂ  good knowledge ofÂ  data structureÂ  isÂ  importantÂ  forÂ  aÂ  softwareÂ  applicationÂ  design andÂ  development .Â

TheÂ  data structureÂ  mainlyÂ  deals withÂ  the organizationÂ  ofÂ  the dataÂ  in a programÂ  andÂ  the operationsÂ  allowedÂ  on that data . Â

In computer science , the data structure is a specific way to organize and store Â the data in a computer’s memory. The programmer can organize the data and also define the permitted operations on the data with the help of data structure so that the data can beÂ  usedÂ  moreÂ  efficiently .

In most simple terms , anything Â that usedÂ  to store and organize the program data , while writing a program code is a Â data structure .

SomeÂ  data structuresÂ  and the operations allowed areÂ  predefined in the programmingÂ  language itself .Whereas the programmer can alsoÂ  defineÂ  aÂ  data structureÂ  to meet any specificÂ  requirement . Such DS are referred asÂ  user definedÂ  data structures.

## What Is the Use Of Data Structure ?

TheÂ  computer programsÂ  areÂ  designedÂ  to handle different kinds Â ofÂ  dataÂ  such as text , numbers , imagesÂ  , audio , videoÂ  andÂ  other types .

The Â softwareÂ  programsÂ  areÂ  designedÂ  and developed for different types of applications.Â  AndÂ  therefore , the computer programmersÂ  haveÂ  to deal with all kinds of dataÂ Â  suchÂ  asÂ  text Â andÂ  numericalÂ  data , images , audioÂ  andÂ  video Â .

Â FromÂ  the software application performance point of viewÂ  , the efficiency and the performance of the software program depends upon Â howÂ  the data is stored , organizedÂ  andÂ  groupedÂ  together, during theÂ  program execution . And therefore computer scientist , software engineers , software developers must have a good knowledge ofÂ  data structures .Â  Â  Â Â

## Types Of Data Structures

The data structuresÂ  ( DS ) canÂ  beÂ  broadlyÂ  groupedÂ  in to two basicÂ  types and that Â includes Primitive Data StructuresÂ  Â Â and Non-PrimitiveÂ  Data StructuresÂ  . TheseÂ  DSÂ  areÂ  groupedÂ  on the basisÂ  ofÂ  eitherÂ  pre-definedÂ  in the programming languageÂ  orÂ  defined by the userÂ Â  .Â

SomeÂ  data structuresÂ  areÂ  builtÂ  in to theÂ  programmingÂ  languageÂ  itselfÂ  which are readilyÂ  availableÂ  to theÂ  programmer forÂ  itsÂ  useÂ  and these DSÂ  are referredÂ  asÂ  primitiveÂ  data structuresÂ  .Â

ForÂ  exampleÂ  , the mostÂ  commonÂ  primitiveÂ  data structuresÂ  whichÂ  areÂ  generally supported byÂ  almostÂ  allÂ  programming languagesÂ  includeÂ  IntegerÂ  ,Â  FloatÂ  , CharacterÂ  and BooleanÂ  .

TheÂ  Non-PrimitiveÂ  Data StructuresÂ  can be further classifiedÂ  asÂ  LinearÂ  DSÂ  AndÂ  Non Linear DS . TheÂ  LinearÂ  DS can be furtherÂ  ClassifiedÂ  inÂ  two further subcategoriesÂ  such asÂ  Static andÂ  DynamicÂ  depending upon the ability of the DS to expandÂ  or shrinkÂ  as per the program needs .

The Static LinearÂ  DS includes Arrays , whereasÂ  theÂ  DynamicÂ  linearÂ  DSÂ  includes Linked ListÂ  ,Â  StackÂ  and Queue.Â  TheÂ  Non Linear DSÂ  includesÂ  TreesÂ  andÂ  Graph . Let usÂ  now discussÂ  eachÂ  of these DSÂ  one by one in detail .

## Primitive Data Structures

The primitive data structures areÂ  the programming language defined data structures which are built into the programming language itself , which programmers can readily use in the program . AlmostÂ  all programmingÂ  languagesÂ  support the primitiveÂ  data structures Â .

The examples of primitive data types include integers , floating point numbers and one single individual characters in text.

The primitive data types are readily available to the programmer and Â doesnâ€™t require a large amount of data for representation.

Each Character correspond to a single specific reference value Â in an ASCII table. The Integers numbers can represent a small to a very large values without the decimal point.

Whereas the Boolean values needs only two possible binary numbers that is either 0 ( zero ) or 1 ( one ).

## Non-Primitive Data Structures

The non-primitive data structures areÂ  user ( programmer ) defined data structures which areÂ  alsoÂ  referredÂ  as userÂ  definedÂ  data structures .

The Â non-primitive data structures areÂ  derivedÂ  fromÂ  primitiveÂ  data typesÂ  by combiningÂ  two Â orÂ  moreÂ  primitiveÂ  dataÂ  structures. These DS can be furtherÂ  sub dividedÂ  asÂ  linearÂ  andÂ  non- linearÂ  data structures .

The Non-primitive data types are not pre-defined in the programming language . And therefore , the programmer has to define as per the program requirements.

## Characteristics Of Non-Primitive Data Structures

For example , In Â Java programming language, the non-primitive data types are referred as Â “objects” because these objects are Â created run-time by defining the Classes . An object may contain any primitive type of data as per the program requirements.

### Linear Data Structures

In computer scienceÂ  , anÂ  arrayÂ  is a linear data structure Â ( also commonly referred as an arrayÂ  )Â  isÂ  collectionÂ  ofÂ  elementsÂ  ofÂ  sameÂ  data type . AnÂ  arrayÂ  isÂ  linear dataÂ  structureÂ  becauseÂ  the arrayÂ  elementsÂ  (Â  ValuesÂ  )Â  are traversedÂ  in sequentialÂ  manner .

The simplest type of linear data structure is a one-dimensionalÂ  array . An array Â Â is organizedÂ  in a singleÂ  rowÂ  consistingÂ  ofÂ  number of pre-defined number of data elements . These data elements are stored in a contiguous memory locations allocated by the operating system.

### Where To Use Arrays ?

While writing a programming code , sometimes theÂ  programmerÂ  is requiredÂ  to storeÂ  large numberÂ  values of theÂ  same data typeÂ  such asÂ  student marks , employee contact numbers , customer order numbers and so on .

In such situations , one solution is to declare large number of variables using different variable names which is not a practical solution .Â Â

Instead of creating large number of variables , the programmer canÂ  use anÂ  arrayÂ  DS , whichÂ  allows the programmer ,Â  to store largeÂ  number of valuesÂ  of theÂ  same data typeÂ  Â Â usingÂ Â  sameÂ  variable name .

Let us consider one simple example of designing a software for a school . If a programmer wants to store the names ofÂ  100 students , then programmer has two options .Â

In the first option ( Not recommended ) , a programmer can crate 100 variables of string type and assign student name to each of these variable . This program will work but it is the most inefficient program .

In the second option ( Recommended ) , the programmer can simply create an array of students name ( String data type ) with capacity to store 100 names . This is a much better way of organizing program data .

### Types Of Arrays

Depending upon the programming needs , the programmer can choose different types of arrays which include one , two and multi-dimensional arrays.

### Linear Dynamic Data Structures

In computer science , the linked list is a linear dynamic data structure. It is a collection of data elements called nodes . Each node consist of two fields . The first field of the node contains the actual value whereas the send field of the node contains a pointer ( a memory address ) whichÂ  pointsÂ  to the next node .

Â In simple terms , the linked listÂ  can be visualized as a chain of nodes Â , where every node pointsÂ  toÂ  theÂ  next node. And therefore it offers advantage of flexibility of size.

### Linked List Example

The linked list is a dynamic linear data structure.Â  ThatÂ  meansÂ  ,Â  theÂ  programmer can dynamicallyÂ  modifyÂ  theÂ  sizeÂ  of theÂ  linked listÂ  during the program run-timeÂ  as per the needs .Â

ThisÂ  was notÂ  possibleÂ  in caseÂ  ofÂ Â  arrayÂ Â  whichÂ  isÂ  staticÂ  dataÂ  structure Â and programmer has to define the Â .Â

The linked list Â does not have indexingÂ  systemÂ  to accessÂ  specific data elementÂ  and makes use of traversingÂ  to perform desired operation .Â

### Trees And Graphs

A non-linear data structure is a data structure in which a data item is connected to several other data items and the data items are not organized sequentially. And therefore it Â is called as non-linear data structure.

In case of non-liner data structure it is possible for a data item to be connected with one or more data items. The examples of non-linear data-structures includes Â Graphs and Trees.

### Abstract Data Type ( ADT )

Abstract Data Type ( ADT ) is an important topic in computer science . In order to understand the concept of ADTÂ  , we need to familiar with some fundamental programming conceptsÂ  such asÂ  programÂ  variables , differentÂ  data typesÂ  ,Â  OOPÂ  conceptsÂ  suchÂ  abstractionÂ  , encapsulationÂ  and the concept of class .Â Â Â

While writing a program code , theÂ  programmerÂ  needs Â toÂ  declareÂ  the variablesÂ  to store the data . This program data Â will be operated upon as perÂ  the program instructionsÂ  . Â TheÂ  variables are namedÂ  memory locationsÂ  where differentÂ  types of data is storedÂ  that can be referred in programÂ  codeÂ  with different variable names .

TheÂ  program data can be of different typesÂ  such as integer , float , string , Boolean . The data type for a variableÂ  signifiesÂ  theÂ  type of the data that will be storedÂ  andÂ  the associatedÂ  operations .

AnÂ  ADT – abstract data type represents a mathematical modelÂ  which provides an abstracted view of the data structure in terms data propertiesÂ  and theÂ  operations allowed on the data .

Each programming language provides some predefined data typesÂ  in built in to the language which are referred as primitive data types . In some situations the primitive data typesÂ  may not be sufficientÂ  andÂ  in such cases the programmer can defineÂ  a data typeÂ  along with its associatedÂ  operations .

### Abstract Data Type ( ADT ) Examples

InÂ  Object Oriented Programming ( OOP )Â  the concept ofÂ  ClassÂ  representsÂ  anÂ Â  ADTÂ  .Â  We can defineÂ  a ClassÂ Â  asÂ  aÂ  blueprint forÂ  creatingÂ Â  theÂ  objectsÂ  .

In OOPÂ  , the Class consist of data members andÂ  the associated operations which operates on the data members Â . The ClassÂ  is an example ofÂ  an ADT Â which binds togetherÂ  theÂ  dataÂ  andÂ  theÂ  associated operations .

LetÂ  usÂ  consider one example ofÂ  anÂ  integer data type is one of the most commonly used primitive data type .

The mathematical concept of an integer, along with operations that manipulate integers, form a ADT. The int variable type is a physical representation of the abstract integer.Â

The int variable type is an ADT because it has some wellÂ  definedÂ  data propertiesÂ  andÂ  the operations that are associatedÂ  with integer such as addition , subtractionÂ Â  and otherÂ  operations .

## Data Structures And Algorithms

### Introduction To Algorithms

TheÂ  computer programÂ  isÂ  a setÂ  of program instructionsÂ  whichÂ  provides the direction to the computerâ€™sÂ  CPUÂ  to execute the desiredÂ  operationsÂ  as per the programÂ  instructions.

Before writing a computer program code , the programmer has to first decide the logic of the program and then translate this logic in to the sequence of logical stepsÂ  which implements that logicÂ  to solveÂ  the problemÂ  in a particular mannerÂ  and this logic Â is referred as an program algorithm.

### What Is Algorithms ?

There can be many possible solution to a problem .Â  And therefore , the programmers must logically thinkÂ  and explore differentÂ  algorithms on its merits.

The programmer must explore the various alternate approaches to findÂ  the best possible algorithm that provides the solutionÂ  which is most efficient .

Each computer program implements a specific algorithm . TheÂ  EfficiencyÂ  ofÂ  theÂ  computer systemÂ  to solve a particular problem depends uponÂ  the Â efficiencyÂ  of the program codeÂ  whichÂ  directs the computer to solve that problemÂ  in a particular manner .Â

NowÂ  we are going to discussÂ Â  variousÂ  aspects ofÂ  algorithm in detail .

### What Is Algorithms ?

An Algorithm can be defined as a step-by-step procedure by defining a set of instructions which implements a specific logic andÂ  executed in a specificÂ  order to get the desired output .

Each Algorithm only provides one specificÂ  approach which leads to the solution. An algorithm only indicates an approach that leads to one solution .

And therefore, algorithms are programming language independent and can beÂ  implemented usingÂ  any Â programming language.

TheÂ  computer programmers create algorithms during the software design stageÂ  beforeÂ  writingÂ  aÂ  program code Â . EachÂ  algorithm provides different approach to solve the problem in specific way .

The programmersÂ  then analyze each of these algorithms against some Â standard evaluation criterion. These algorithm are evaluation on two criterion that includes Â Time Complexity Â and Space Complexity to evaluate the algorithm performance .Â

After careful evaluation , the programmers thenÂ Â  finallyÂ  selectÂ  the algorithm for its implementation in the program code .Â

The algorithm which provides the bestÂ  approachÂ  to solve a particular problem is implemented in the final code in the software project .

## Data Structures And Algorithms

### Classification Of Algorithms

There are many types ofÂ  Algorithms that can be classified depending upon many parameters . However some of the fundamental types of algorithms include :

Join The Best Seller

## Learn Computer Science And Programming Fundamentals

This is the most comprehensiveÂ  and uniqueÂ  Computer ScienceÂ  And Programming Fundamentals course Online which will give you in depth understanding of most important fundamental concepts in computer science And Programming .

Don`t copy text!