Skip to main content

CT Week 6

Sorting Algorithms and Lists

📚 Sorting Algorithms and Lists

List of Lists

A list of lists is a data structure where each element in the main list is itself a list. This allows for the creation of complex, multi-dimensional arrays. For example, a list of lists can be used to represent a matrix or a table where each sublist is a row.

Insertion Sort

Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much like sorting playing cards in your hands. The algorithm iterates through the list, growing the sorted portion behind it. At each iteration, it removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.

Ordered List

An ordered list is a list in which the items are numbered and the order matters. In HTML, it is created using the

    tag. Each item in the list is defined with the
  1. tag. Ordered lists are useful for displaying items in a specific sequence, such as steps in a process or rankings.

    Insertion Sort Code

    
    function insertionSort(arr) {
        for (let i = 1; i < arr.length; i++) {
            let key = arr[i];
            let j = i - 1;
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
    }
    
        

    Explanation: The insertion sort algorithm sorts an array by iteratively inserting each element into its correct position in a sorted portion of the array.

    Ordered List Code

    
    function addToOrderedList(list, item) {
        if (list.length === 0) {
            list.push(item);
        } else {
            for (let i = 0; i < list.length; i++) {
                if (item < list[i]) {
                    list.splice(i, 0, item);
                    return;
                }
            }
            list.push(item);
        }
    }
    
        

    Explanation: The ordered list function inserts an item into its correct position in a sorted list, maintaining the order of the list.

    GA:

Comments

Popular post

IITM Notes

Course Overview “These handwritten notes encompass topics in data science and civil services. The beauty of knowledge is that you don’t need to belong to any specific group; simply maintain your curiosity, and knowledge will find its way to you. I hope these notes are helpful. If they are, please consider leaving a comment below and follow my blog for updates.” Mathematics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Revision Statistics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11

Maths 1 week 1 Summary

Number System and Set Theory 📚 Number System and Set Theory This week, our teacher covered the basics of the number system. We were instructed to consider 0 as part of the natural numbers, as it will be treated as such in future subjects like Python. However, in exams, it will be explicitly stated whether 0 should be considered a natural number. The key topics from this week include set theory and the relationship between two sets. In set theory, we focused on three Venn diagram problems. In the context of relations, we discussed the concepts of reflexive, symmetric, transitive, and equivalence relations. Detailed Explanation 1.Union of Two Sets The union of two sets A and B is the set of elements that are in either A , B , or both. It is denoted as A ∪ B . 2.Intersection of Two Sets The intersection of two sets A and B is the set of elements that are in both A and B . It is denoted as A ∩ B . 3.Subt

Community page

Welcome To our IITM BS Students Community This community is a student commune where IIT Madras Bachelor of Science students are studying. Our community is managed by 15 community admins who oversee our WhatsApp community, Discord, and Telegram profiles. With more than 1000+ active members, we study together, share memes, watch movies, play games, and have fun. Our goal is to bring all online IITM students together to excel in exams while having fun. Community Admins Agampreet LinkedIn Ansh Ashwin Ambatwar Arti Dattu Dolly Elango Koushik Shrijanani Saksham Shivamani Shivam Instagram LinkedIn Join Our Community Subscribe to our YouTube page Join our meme team on