Introduction to Data Structures and Algorithms - Day 73 : Harshil Chovatiya

Introduction to Data Structures and Algorithms - Day 73 : Harshil Chovatiya

Day 73: Introduction to Data Structures and Algorithms

Brief:

On these day, we initiated our exploration into the world of data structures and algorithms, recognizing their paramount importance in computer science. We delved into fundamental concepts, including the significance of these structures and the pivotal role algorithms play in problem-solving. Through practical examples, we illustrated how efficient data organization and algorithmic strategies are essential for optimizing computational tasks.

Introduction to Data Structures and Algorithms - Day 73 : Harshil Chovatiya

Topics Covered:

1. Significance of Data Structures and Algorithms:

Example: In a library management system, employing efficient data structures (like hash tables for cataloging books) and algorithms (such as sorting algorithms for arranging books on shelves) streamlines operations, facilitating quicker access to information and smoother workflow.

2. Common Data Structures:

  • Arrays: Storing exam scores of students (`scores = [85, 92, 78, 95, 88]`).
  • Linked Lists: Visualizing compartments of a train linked together, analogous to nodes in a linked list.
  • Stacks: The analogy of a cafeteria's stack of plates, demonstrating the Last-In-First-Out (LIFO) principle.
  • Queues: The scenario of people waiting in line at a ticket counter, adhering to the First-In-First-Out (FIFO) principle.

3. Introduction to Algorithms:

Analysis of Algorithms: Comparing bubble sort and merge sort to grasp their efficiency in sorting a list of numbers.

  • Bubble Sort:
                    
    // Bubble Sort
    let arr = [5, 2, 7, 1, 9];
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr.length - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
            }
        }
    }
    console.log("Bubble Sorted array:", arr); // Sorted array: [1, 2, 5, 7, 9]
                    
                
    Sorted array: `[1, 2, 5, 7, 9]`.
  • Merge Sort:
                    
    // Merge Sort
    function mergeSort(arr) {
        if (arr.length <= 1) {
            return arr;
        }
        const mid = Math.floor(arr.length / 2);
        const left = arr.slice(0, mid);
        const right = arr.slice(mid);
        return merge(mergeSort(left), mergeSort(right));
    }
    
    function merge(left, right) {
        let result = [];
        while (left.length && right.length) {
            if (left[0] < right[0]) {
                result.push(left.shift());
            } else {
                result.push(right.shift());
            }
        }
        return result.concat(left, right);
    }
    
    let arrToSort = [5, 2, 7, 1, 9];
    console.log("Merge Sorted array:", mergeSort(arrToSort)); // Sorted array: [1, 2, 5, 7, 9]
                    
                
    Sorted array: `[1, 2, 5, 7, 9]`.

Conclusion:

As we conclude Day 73, we've laid the groundwork for our journey through data structures and algorithms. Understanding their significance, common implementations, and introductory algorithms provides a solid foundation for further exploration. Moving forward, remember to analyze problems critically, select appropriate structures and algorithms, and strive for efficient solutions. Stay curious and keep honing your problem-solving skills!

Social Media

Comments