Docsity

Data Structure using Python, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structure using Python and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data Structure

Fundamentals

Data Structures

  • (^) Data Structures are the programmatic way of storing data so that data can be used efficiently.
  • Almost every enterprise application uses various types of data structures in one or the other way.
  • (^) Data Structure is a way of collecting and organizing data in such a way that we can perform operations on these data in an effective way.
  • (^) Data Structures is about rendering data elements in terms of some relationship, for better organization and storage.
  • (^) For example, we have some data which has, player's name "Virat" and age 26. Here "Virat" is of String data type and 26 is of integer data type.
  • (^) We can organize this data as a record like Player record, which will have both player's name and age in it.
  • (^) Now we can collect and store player's records in a file or database as a data structure. For example : "Dhoni" 30, "Gambhir" 31, "Sehwag" 33.
  • In simple language, Data Structures are structures programmed to store ordered data, so that various operations can be performed on it easily.

Data Definition

  • (^) Data Definition defines a particular data with 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.

Data Object

  • (^) Data Object represents an object having a data.

Data Structure

Operation

  • (^) The data in the data structures are processed by certain operations.
  • (^) The particular data structure chosen largely depends on the frequency of the operation that needs to be performed on the data structure.
  • (^) The basic operations that are performed on data structures are as follows:
  • (^) Insertion: Insertion means addition of a new data element in a data structure.
  • (^) Deletion: Deletion means removal of a data element from a data structure if it is found.
  • (^) Searching: Searching involves searching for the specified data element in a data structure.
  • (^) Traversal : Traversal of a data structure means processing all the data elements present in it.
  • (^) Sorting: Arranging data elements of a data structure in a specified order is called sorting.
  • (^) Merging: Combining elements of two similar data structures to form a new data structure of the same type, is called merging.

Built-in Data Type(Primitive) Derived Data Type(Non-Primitive)

  • Those data types for which a language has built-in support are known as Built-in Data types.
  • For example, most of the languages provides following built-in data types.
  • For example โˆ’ Integers Boolean (true, false) Floating (Decimal numbers) Character and Strings
    • Those data types which are implementation independent as they can be implemented in one or other way are known as derived data types.
    • These data types are normally built by combination of primary or built-in data types and associated operations on them.
    • For example โˆ’ List, Array, Stack, Queue

Non-primitive data type

  • (^) Non-primitive data types are not defined by the programming language, but are instead created by the programmer.
  • (^) They are sometimes called โ€œreference variables,โ€ or โ€œobject references,โ€ since they reference a memory location, which stores the data.
  • (^) A non-primitive data type is something else such as an array structure or class is known as the non-primitive data type.
  • (^) The data type that is derived from primary data types is known as a non- primitive data type.
  • (^) The non-primitive data types are used to store the group of values.
  • (^) Examples of the non-primitive data types are Array, structure, union, link list, stacks, queue etc.

Linear and non linear data

structure.

  • (^) Linear data structure: A linear data structure traverses the data

elements sequentially, in which only one data element can directly be

reached.

  • (^) Ex: Arrays, Linked Lists

Non-Linear data structure: Every data item is attached to several other

data items in a way that is specific for reflecting relationships.

  • (^) The data items are not arranged in a sequential structure.
  • (^) Ex: Trees, Graphs
  • (^) The linear data structure is a single level data structure while non-

linear data structures are the multilevel data structure.

What is an Algorithm?

  • An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain predefined task.
  • Algorithm is not the complete code or program, it is just the core logic(solution) of a problem, which can be expressed either as an informal high level description as pseudocode or using a flowchart.
  • Every Algorithm must satisfy the following properties:
  • Input-^ There should be 0 or more inputs supplied externally to the algorithm.
  • Output- There should be at least 1 output obtained.
  • Definiteness-^ Every step of the algorithm should be clear and well defined.
  • Finiteness-^ The algorithm should have finite number of steps.
  • Correctness-^ Every step of the algorithm must generate a correct output.
  • An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less memory space.
  • The performance of an algorithm is measured on the basis of following properties :
  • Time Complexity
  • Space Complexity

Space Complexity

  • Its the amount of memory space required by the algorithm, during the course of its execution.
  • Space complexity must be taken seriously for multi-user systems and in situations where limited memory is available.
  • An algorithm generally requires space for following components :
  • Instruction Space:^ Its the space required to store the executable version of the program. This space is fixed, but varies depending upon the number of lines of code in the program.
  • Data Space: Its the space required to store all the constants and variables(including temporary variables) value.
  • Environment Space: Its the space required to store the environment information needed to resume the suspended function.
  • To learn about Space Complexity in detail, jump to the Space Complexity tutorial.

Non Primitive data structure-

Array

  • (^) Array is a container which can hold a fix number of items and these items should be of the same 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 is used to identify the element.

Array

  • (^) An array is a collection of items stored at contiguous memory locations.
  • (^) The idea is to store multiple items of the same type together.
  • (^) This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).