Skip to main content

Posts

Quick Sort in JavaScript

Quick Sort, devised by Tony Hoare in 1959, leverages the divide-and-conquer strategy. It selects a pivot element and then arranges all smaller elements to one side and larger ones to the other. Quick Sort Under the Hook Quick Sort operates in three steps: Selecting a pivot. Shifting elements smaller than the pivot to one side, and moving elements larger than the pivot to the other side. Repeating these steps for both sides separately. Consider, for instance, sorting [3, 9, 4, 7, 5, 1, 6, 2, 8] with 7 as the pivot. After one round, it becomes [3, 4, 5, 1, 6, 2, 7, 9, 8]. The pivot 7 is correctly placed, and we can then divide the array into two parts: [3, 4, 5, 1, 6] and [9, 8], which can be sorted separately. Quick Sort Implementation in JavaScript : Partition Let's implement Quick Sort in JavaScript. First, we partition the array around the pivot in our partition function: In this portion of the code, we selected the last element as the pivot and placed smaller elements on the le...
Recent posts

Advanced Binary Search Problems

  Advanced Binary Search Problems We'll apply binary search to complex data structures, such as bitonic arrays and rotated sorted arrays , to find specific elements efficiently. Problem 1: Searching in a Bitonic Array Consider a bitonic array as a numerical sequence simulating the trajectory of a roller coaster — first, it rises to a peak, then descends. For example, consider the array [1, 2, 3, 4, 5, 3, 1]: its first part ascends, and the last descends, making it bitonic. Your goal is to find a value in such an array. You might walk the entire path, which is exhaustive and represents our naive approach with a time complexity of O(n). Our aim today is a more efficient method. Efficient Approach Explained To apply binary search, we first locate the peak of the array, then perform binary search on either side of the peak: one for the increasing sub-array and one for the decreasing sub-array. The first step is akin to finding a vantage point at the carnival for a better view: Solutio...

Binary Search in JavaScript

It's like finding a house number on a long street — instead of starting from one end, you begin in the middle, and, based on whether the house number is higher or lower, you search the right or left half of the list. We'll learn to: Understand Binary Search. Implement Binary Search using recursion and iteration in JavaScript. Analyse the time complexity of Binary Search. Unveiling Binary Search Binary Search follows a divide-and-conquer strategy. It starts in the middle of a sorted list. If the middle value is the desired one, great! If not, it uses the sorted nature of the list to eliminate half of it. The side to eliminate is selected based on whether the target is smaller or larger than the middle value. Implementing Binary Search Using Recursion in JavaScript Let's implement Binary Search in JavaScript using recursion. Here's the code, accompanied by detailed comments: This function calls itself recursively, gradually shrinking the search area until it finds the ta...

Exploring the magic of Recursion in JavaScript

W e are going to dive into the intriguing concept of "Recursion" — a concept reminiscent of the seemingly endless reflections in a room of mirrors. We will dissect recursion, understand its intricacies, and learn to use it in JavaScript . Understanding Recursion Think about a stack of pancakes. To get to the bottom, you must lift each pancake from the top, one at a time. This repeated action is a basic example of recursion. In programming, recursion involves a function calling itself repeatedly until a specific condition, known as the base case, is satisfied. This is like walking down a staircase, step by step until you reach the bottom. Here's a simple JavaScript function to illustrate recursion: In this function, x decrements by one in each recursive call until it is no longer greater than 0, at which point recursion stops. Defining the Base Case Think of the base case as a stop sign guiding our function, indicating when recursion should cease. In our pancake stack exam...

JavaScript Maps

Dive Into JavaScript Maps A Map stores data as key-value pairs. We'll recall how to create Maps, implement them, and delve into the details of their memory management. Understanding JavaScript Maps Maps are versatile data structures in JavaScript. They store key-value pairs and accept any data type as a key — even objects and functions! Here is how we create an empty Map: let myMap = new Map(); // creates an empty Map Here, myMap is a new JavaScript Map, eagerly awaiting to store your keys and values. Meander Through Map Methods Maps provide some essential built-in methods: set(key, value): Stores a key-value pair. get(key): Retrieves the value of a key. has(key): Checks if a key exists and returns true or false. delete(key): Erases a key-value pair. size: Returns the count of key-value pairs. To gain a better understanding, let's apply these methods: let myMap = new Map(); // Add pairs with set myMap.set('apples', 10); // Adds a new pair myMap.set('bananas', 6...

How to work with Set in JavaScript

Understanding JavaScript Sets Set in JavaScript is an unordered collection of unique values. We can examine the size of the set using .size method. Notice that the set is unordered, and we can't guarantee that elements will be shown in the order we added them. Sets work similarly to JavaScript objects but are designed for uniqueness. They use hashing, a way to convert a given pearl into a unique code, which facilitates rapid storage and retrieval. When checking if an item is in a Set, JavaScript computes its hash code to locate it, much like a map leading to a treasure. Sets have numerous practical uses in database management, data analysis , and more . Problem 1: Check if Two Sets are Disjoint Let's begin by considering the function, areDisjoint which takes two arrays and determines if they are disjoint, meaning they have no elements in common. This is crucial when analysing datasets for overlapping values, similar to ensuring that two puzzle pieces from different puzzles d...