0% found this document useful (0 votes)
8 views

Common Data Structures Explained

Uploaded by

Nirmal Ramessur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Common Data Structures Explained

Uploaded by

Nirmal Ramessur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Common Data Structures Explained:

1. Arrays

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple
items of the same type together.

 Example: An array of integers: [5, 8, 12, 20]

 Operations: Accessing an element (O(1)), inserting at the end (O(1)), inserting/deleting in


the middle (O(n)).

2. Linked List

A linked list is a collection of nodes where each node contains the data and a reference (or pointer)
to the next node.

 Example:

css

Copy code

[10] -> [20] -> [30] -> [40]

 Operations: Insertion and deletion are more efficient than in arrays (O(1) if
inserting/deleting at the beginning).

3. Stack

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. Elements are
added and removed from only one end (the top).

 Example: Browser history, where the most recently visited page is at the top.

 Operations: Push (insert), Pop (remove), Peek (view top element).

4. Queue

A queue follows the First In, First Out (FIFO) principle. Elements are added at the rear and removed
from the front.

 Example: A line of people at a ticket counter.

 Operations: Enqueue (add), Dequeue (remove), Front (view front element).

5. Hash Table

A hash table stores key-value pairs and uses a hash function to compute an index into an array of
buckets or slots.

 Example: Dictionary lookup, where the word is the key, and the definition is the value.

 Operations: Insert (O(1)), Delete (O(1)), Search (O(1)).

2. Algorithms: What Are They?


An algorithm is a step-by-step procedure or formula for solving a problem. In computer science, it is
used to manipulate data in data structures.

Common Algorithm Types:

1. Sorting Algorithms: Organize data in a specific order (e.g., Bubble Sort, Quick Sort, Merge
Sort).

2. Searching Algorithms: Find specific data in a structure (e.g., Binary Search, Linear Search).

3. Graph Algorithms: Solve problems related to graph data structures (e.g., Dijkstra’s Algorithm,
Breadth-First Search).

4. Dynamic Programming: Breaks problems into sub-problems (e.g., Fibonacci sequence,


Knapsack problem).

Example Problem: Sorting an Array Using Bubble Sort

Bubble Sort is a simple algorithm that repeatedly steps through the list, compares adjacent elements,
and swaps them if they are in the wrong order. This process continues until the list is sorted.

Unsorted Array: [5, 1, 4, 2, 8]

Pass 1:

 Compare 5 and 1: Swap → [1, 5, 4, 2, 8]

 Compare 5 and 4: Swap → [1, 4, 5, 2, 8]

 Compare 5 and 2: Swap → [1, 4, 2, 5, 8]

 Compare 5 and 8: No swap → [1, 4, 2, 5, 8]

Pass 2:

 Compare 1 and 4: No swap → [1, 4, 2, 5, 8]

 Compare 4 and 2: Swap → [1, 2, 4, 5, 8]

 Compare 4 and 5: No swap → [1, 2, 4, 5, 8]

 Compare 5 and 8: No swap → [1, 2, 4, 5, 8]

The array is now sorted: [1, 2, 4, 5, 8]

 Time Complexity: O(n^2) for worst and average cases.

 Space Complexity: O(1) (in-place sorting).

Example Problem: Searching an Element Using Binary Search

Binary Search works on sorted arrays. It divides the array into two halves, compares the target value
with the middle element, and eliminates half of the array in each step.

Array: [2, 4, 6, 8, 10, 12, 14] Target: 10


Steps:

1. Set left = 0, right = 6.

2. Find middle = (0 + 6) / 2 = 3, value at index 3 is 8.

3. Since 10 > 8, search the right half (left = 4).

4. Find middle = (4 + 6) / 2 = 5, value at index 5 is 12.

5. Since 10 < 12, search the left half (right = 4).

6. Value at index 4 is 10. Target found.

 Time Complexity: O(log n) for worst case.

 Space Complexity: O(1).

Why Learn Data Structures and Algorithms?

1. Efficiency: Well-chosen data structures and algorithms reduce time and memory usage.

2. Problem Solving: Essential for solving complex computational problems and optimizing
performance.

3. Competitive Programming: Key for coding competitions and technical interviews.

You might also like