ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Design and Analysis of Algorithms
    COMP3138
    Progress0 / 53 topics
    Topics
    1. Introduction to Algorithm Design2. Data Structures3. Efficiency in Algorithms4. Analysis of Algorithms5. Mathematical Review6. Mathematical Analysis of Algorithms7. Types of Functions8. Order of Growth9. Asymptotic Notations10. Sorting Algorithms11. Selection Sort Algorithm12. Example and Analysis of Selection Sort13. Insertion Sort Algorithm14. Divide and Conquer Algorithms15. Merge Sort Algorithm16. Quick Sort Algorithm17. Bucket Sort Algorithm18. Radix Sort Algorithm19. Counting Sort Algorithm20. Heap Sort Basics21. Heap Algorithms22. Heap Properties and Examples23. Heap Operations24. Heap Sort Algorithm Analysis25. Heap Insertion and Deletion26. Tree Based Algorithms and Hashing27. Red Black Tree Basics28. Binary Search Tree Basics29. Tree Searching Algorithms30. Analysis of Tree Searching Algorithms31. Analysis of Insertion and Deletion in BST32. Hashing Basics33. Examples of Hash Functions34. Analysis of Collision Resolution Techniques35. Dynamic Programming36. 0-1 Knapsack Problem37. Fractional Knapsack Problem38. Longest Common Subsequence39. Shortest Path Finding40. Matrix Chain Multiplication41. Assembly Line Chain Problem42. Greedy Algorithms43. Prim's Algorithm44. Kruskal's Algorithm45. Dijkstra's Algorithm46. Huffman Coding47. NP-Completeness48. Polynomial Time Verification49. Reducibility50. NP-Completeness Proofs51. Randomized Algorithms52. Particle Swarm Optimization53. Genetic Algorithms
    COMP3138›Data Structures
    Design and Analysis of AlgorithmsTopic 2 of 53

    Data Structures

    7 minread
    1,129words
    Intermediatelevel

    Data Structures

    What are Data Structures?

    A data structure is a way of organizing and storing data so that we can perform operations on it efficiently. It defines a particular way of storing data that allows access to the data, manipulation of the data, and operations like searching, inserting, deleting, and updating.

    In simpler terms, think of a data structure as a container or an organization method for the data that you work with in your program. Just like in a physical warehouse, where you need to decide how to store boxes, shelves, and items to make retrieval easy, in computer science, data structures help in organizing data to make tasks like searching, insertion, and deletion easier and more efficient.


    Types of Data Structures

    Data structures can be broadly classified into two categories:

    1. Primitive Data Structures
    2. Non-Primitive Data Structures

    1. Primitive Data Structures

    Primitive data structures are the basic types of data that are directly supported by programming languages. These include:

    • Integers: Whole numbers, both positive and negative.
    • Floats: Numbers with decimal points.
    • Characters: Single letters or symbols (e.g., 'a', 'B', '1').
    • Booleans: True or false values.

    These are the fundamental building blocks that can be used to construct more complex data structures.


    2. Non-Primitive Data Structures

    Non-primitive data structures are more complex and can store collections of data. They are further classified into:

    • Linear Data Structures
    • Non-linear Data Structures
    Linear Data Structures

    In linear data structures, elements are stored sequentially, and each element is connected to its previous and next elements (except the first and last element). You can traverse the structure from one element to the next in a linear manner.

    • Arrays
    • Linked Lists
    • Stacks
    • Queues

    Let's explore each of these in detail.


    Linear Data Structures

    1. Arrays

    An array is a collection of elements of the same type stored at contiguous memory locations. Arrays are indexed, meaning each element in the array has a unique index or position.

    • Example: int arr[] = {1, 2, 3, 4, 5};

    • Operations:

      • Accessing an element: Direct access using index (arr[2] gives 3).
      • Insertion and Deletion: Inserting or deleting elements may require shifting elements to maintain contiguous memory, which can be inefficient.
    • Time Complexity:

      • Access: O(1) (constant time)
      • Insertion/Deletion: O(n) (linear time) in the worst case, because elements might need to be shifted.

    2. Linked Lists

    A linked list is a collection of nodes, where each node contains two parts: the data and a reference (or pointer) to the next node in the list. Linked lists are dynamic, meaning they don’t require contiguous memory like arrays.

    • Example: Node 1 -> Node 2 -> Node 3 -> NULL

    • Operations:

      • Insertion/Deletion: Easy to insert or delete an element at the beginning or middle of the list.
      • Searching: Linear time complexity for searching as you need to traverse each node.
    • Types of Linked Lists:

      • Singly Linked List: Each node points to the next node.
      • Doubly Linked List: Each node points both to the next and the previous node.
      • Circular Linked List: The last node points back to the first node.
    • Time Complexity:

      • Insertion/Deletion: O(1) (if at the beginning of the list)
      • Searching: O(n) (since you may have to traverse the entire list)

    3. Stacks

    A stack is a collection that follows the LIFO (Last In, First Out) principle. The last element added to the stack is the first one to be removed. It can be imagined like a stack of plates where the last plate added is the first to be taken off.

    • Operations:

      • Push: Add an element to the top.
      • Pop: Remove the top element.
      • Peek: View the top element without removing it.
    • Example: Imagine a stack of plates, where the plate added last is the first plate you take off.

    • Applications:

      • Undo operations in text editors
      • Expression evaluation (like converting infix to postfix notation)
    • Time Complexity:

      • Push/Pop/Peek: O(1) (constant time)

    4. Queues

    A queue is a collection that follows the FIFO (First In, First Out) principle. The first element added to the queue is the first one to be removed. Think of it like a line at a grocery store: the first person in line is the first to be served.

    • Operations:

      • Enqueue: Add an element to the back.
      • Dequeue: Remove an element from the front.
      • Peek: View the front element without removing it.
    • Example: In a queue, the first item added is the first to be removed.

    • Applications:

      • Managing tasks in a printer queue
      • Breadth-first search (BFS) in graph algorithms
    • Time Complexity:

      • Enqueue/Dequeue/Peek: O(1) (constant time)

    Non-Linear Data Structures

    Non-linear data structures don't store data in a linear sequence. They are more complex and allow for hierarchical relationships between elements.

    1. Trees

    A tree is a hierarchical data structure with a root node and child nodes. Each node has a value and a list of references to its child nodes. Trees are used to represent hierarchical relationships, like organizational structures or file systems.

    • Example: A binary tree, where each node has at most two children (left and right).

    • Operations:

      • Insertion/Deletion: Based on the tree's structure (e.g., binary search trees or AVL trees).
      • Searching: Often faster than linear data structures due to hierarchical organization.
    • Time Complexity:

      • For balanced trees: O(log n) for searching, insertion, and deletion.

    2. Graphs

    A graph is a collection of nodes (also called vertices) and edges that connect pairs of nodes. Graphs are used to represent relationships between objects, such as social networks or roadmaps.

    • Types of Graphs:

      • Directed Graph (Digraph): Edges have a direction.
      • Undirected Graph: Edges do not have direction.
      • Weighted Graph: Each edge has a weight or cost associated with it.
    • Operations:

      • Searching: Depth-First Search (DFS) and Breadth-First Search (BFS).
      • Shortest path: Dijkstra’s or Bellman-Ford algorithm.
    • Time Complexity:

      • For adjacency matrix representation: O(V^2) for some operations.
      • For adjacency list representation: O(V + E) for traversal.

    Conclusion

    Data structures are fundamental building blocks in computer science. Understanding how different data structures work helps in choosing the right one for your problem, ensuring that operations like searching, insertion, and deletion can be performed efficiently. Each data structure has its strengths and weaknesses, and choosing the appropriate one depends on the type of problem you're solving and the operations you'll perform most often.

    Here's a quick summary of the major data structures:

    Data Structure Type Key Operations Time Complexity (Average)
    Array Linear Access, Insert, Delete Access: O(1), Insert/Delete: O(n)
    Linked List Linear Insert, Delete, Traverse Insert/Delete: O(1), Search: O(n)
    Stack Linear Push, Pop, Peek Push/Pop/Peek: O(1)
    Queue Linear Enqueue, Dequeue, Peek Enqueue/Dequeue/Peek: O(1)
    Tree Non-linear Insert, Search, Delete Balanced Tree: O(log n)
    Graph Non-linear Search, Add/Remove Edge Adjacency List: O(V+E)
    Previous topic 1
    Introduction to Algorithm Design
    Next topic 3
    Efficiency in Algorithms

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,129
      Code examples0
      DifficultyIntermediate