dsa lab
https://dsalab.org/
Recent content on dsa labHugo -- gohugo.ioen-usdsaninja@dsalab.org (dsaninja)dsaninja@dsalab.org (dsaninja)Thu, 27 Oct 2022 06:04:42 +0530Tree Sort Algorithm
https://dsalab.org/tree-sort/
Thu, 27 Oct 2022 06:04:42 +0530dsaninja@dsalab.org (dsaninja)https://dsalab.org/tree-sort/The Tree Sort algorithm leverages the BST (binary search tree) structure that has elements arranged in an order, such that the in-order traversal of the elements results in a sorted output.
In other words, we can describe the Tree Sort algorithm as:
Create a BST from the input array. Perform an in-order traversal (left, root, right). Before we discuss the implementation, let’s quickly go through the BST properties:
Each node can have a maximum of 2 children.Counting Sort algorithm
https://dsalab.org/counting-sort/
Fri, 07 Oct 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/counting-sort/Counting Sort is a non-comparison-based sorting algorithm that identifies the correct slot of an element in the input by identifying the number of elements that are less than the said element. It does so by counting the number of times each element is present in the input and then using this information to sort the original input.
Consider the following example: [1, 5, 3, 1, 2, 4]
Let’s create a frequency array for this input where each value denotes the number of times the corresponding “index” is present in the input array: [0, 2, 1, 1, 1, 1].Heap Sort Algorithm
https://dsalab.org/heap-sort/
Thu, 06 Oct 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/heap-sort/Heap Sort is similar to selection sort as this algorithm is also based on the concept of finding the maximum (or minimum) value in the input and then placing it at its appropriate location.
But unlike selection sort, heap sort does not linearly iterate over the input to identify the relevant element. Instead, it uses binary heap data structure (a max heap) which always ensures that the largest element is always at the root of the heap.Merge Sort Algorithm
https://dsalab.org/merge-sort/
Wed, 05 Oct 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/merge-sort/Like the quick sort algorithm, Merge Sort is also a divide and conquer based algorithm. But in contrast, it performs the maximum work while combining the individual partitions instead of during the partitioning itself (as in quick sort).
The main idea behind merge sort is to recursively divide the input into halves until no more partitions can be created (up to single or no element partition). Once this stage is reached, the individual partitions are combined in a sorted manner, resulting in the sorted input.Quick Sort Algorithm
https://dsalab.org/quick-sort/
Tue, 04 Oct 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/quick-sort/Quick Sort is a divide-and-conquer, in-memory sorting algorithm that partitions an input around a pivot element and then sorts those subsets recursively.
The idea around the quick sort algorithm is to place all the smaller in-value elements to the left of the pivot element and all the larger elements to its right. Thus after every iteration, the pivot element will be at its correct position. The algorithm is then repeated for the two left and right subsets.Insertion Sort Algorithm
https://dsalab.org/insertion-sort/
Mon, 03 Oct 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/insertion-sort/Insertion Sort works on the same principle we intuitively use to sort the cards in hand while playing any card game.
We select one item from the input and move it to its correct position in the sorted subset (generally to the left). This way, we move all the elements from the pending unsorted subset to their correct position in the sorted subset.
While working with data structures like an array, this movement to the correct position will require us to move the other elements to their right to make a slot for the elements being moved.Selection Sort Algorithm
https://dsalab.org/selection-sort/
Sun, 02 Oct 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/selection-sort/Selection Sort is a comparison-based sorting algorithm that selects and moves the smallest element from the input set to its correct position.
At the end of each iteration, the smallest element from the remaining elements is moved to its correct position, thus dividing the input into two partitions - one sorted and the set of remaining elements.
Consider the following input: [3,5,1,9,8,7] and let’s try to trace the selection sort steps on this input:Bubble Sort Algorithm
https://dsalab.org/bubble-sort/
Sat, 01 Oct 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/bubble-sort/Bubble Sort algorithm derives its name from the fact that it results in the largest element to pop out in every iteration. It does so by repeatedly swapping adjacent elements if those are out of order until no more swaps are required.
Consider the following input: [3,5,1,9,8,7] and let’s try to trace the bubble sort steps on this input:
First Pass [3,5,1,9,8,7] : no change as 3 and 5 are in correct order.Using Binary Search to find insertion index
https://dsalab.org/insertion-index/
Fri, 30 Sep 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/insertion-index/Consider a sorted input of unique elements[1,3,4,5,9,10] that we need to analyze to find the insertion location for a key, say 7. As clear from the input, the key is not present in the input and, if added, should be added at index 4 (0 based) to maintain the sorted nature of the input.
Also, as the input contains unique elements, we will return the existing index if the key is already present in the input array.Ternary Search
https://dsalab.org/ternary-search/
Thu, 29 Sep 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/ternary-search/While binary search provides an efficient mechanism to search in a sorted input, there is always an urge to do better on the time complexity.
As we know that binary search works by dividing the input into two partitions and thus yielding in \(O(log_2n)\) complexity, what if we divide the input into three partitions? Can we do better?
Ternary Search, as the name suggests, is based on the same idea. However, it takes the binary search algorithm one step ahead (not necessarily an improvement) and works by dividing the input into three halves.Unbounded Binary Search - exponential search
https://dsalab.org/exponential-search/
Wed, 28 Sep 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/exponential-search/The binary search algorithm that we discussed in one of the previous articles makes use of the input bounds (start, end) to calculate the middle element. The middle element is then further used to decide the next search space ignoring the other half.
But what if the upper bound of the sorted input is unknown? How can we still efficiently search for an element in such a scenario?
For example, consider the function \( f(x) = 3x - 100\).Binary Search for duplicate elements
https://dsalab.org/binary-search-for-duplicate-elements/
Tue, 27 Sep 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/binary-search-for-duplicate-elements/While the binary search algorithm performs efficiently for a sorted input, there may be scenarios where we are interested in additional information about the index of the key element.
For instance, if the key is present more than once, we might be interested in knowing its first or last index. While the original algorithm will return the first index it will encounter for the key, there are some minor updates we can make to the algorithm to serve the above-mentioned use cases.Depth First Search
https://dsalab.org/depth-first-search/
Mon, 26 Sep 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/depth-first-search/Depth First Search (DFS) is similar to Breadth-First Search (BFS) with the difference in terms of how intermediate elements are held before processing.
While BFS will always process nodes in the order those are found (FIFO), DFS, on the other hand, will process nodes in the reverse order of their discovery (LIFO), i.e., DFS processes the record that is most recently discovered before processing records that were found before.
Consider the same illustration that we used for BFS:Breadth First Search
https://dsalab.org/breadth-first-search/
Sun, 25 Sep 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/breadth-first-search/Breadth-First Search algorithm for graphs (nonlinear data structures) is based on the idea of searching for an element across all the adjacent nodes of the current node before traversing their respective neighboring nodes discovered along the way.
Consider the following illustration:
Breadth First Search Assuming the start node to be the one with value 1, the nodes that the algorithm will next traverse will be 2 and 4 (in any order) before their respective adjacent nodes, like 3 or 5, can be traversed.Binary Search Algorithm
https://dsalab.org/binary-search/
Sat, 24 Sep 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/binary-search/While working with Linear Search, we observed that we are not using any information about the data in the input.
For example, consider the input is already sorted. Then, we can use this information to calculate an approximate index that might have the key value.
Binary Search is one of the most commonly used search algorithms that take inspiration from our day-to-day lives. For example, consider a scenario where you need to search for a word in a dictionary.Linear Search
https://dsalab.org/linear-search/
Fri, 23 Sep 2022 00:00:00 +0000dsaninja@dsalab.org (dsaninja)https://dsalab.org/linear-search/If there is one algorithm that we all use almost on daily basis in our lives, it has to be Linear Search. It is one of the most intutive algorithms, that does not even require any special setup before we even start to perform the search operation.
Introduction Linear Search is one of the most commonly used and intuitive search algorithms. It works similarly to how we learn to explore new things - browse through the set until you find what you are looking for.About
https://dsalab.org/about/
Fri, 19 Apr 2019 21:37:58 +0530dsaninja@dsalab.org (dsaninja)https://dsalab.org/about/Hello, welcome to DSA Lab.
This blog is an archive of my journey to improve my problem-solving skills. I try to solve at least one problem daily, and keeping a public journal gives me another reason to be regular.
It’s all about practicing and continuous learning, until you see the patterns.
If you find the content useful, please spread the word by sharing the posts.
Stack Hosting Service: Gitlab pages Illustrations: draw.