
In real-world projects, if you need to swap two variables, nobody stops you from creating a third variable. We write a statement from left to right. But a statement is executed from right to left. The same variable can have different values if it shows up on both sides of the assignment operator. We can take advantage of these two features.
Usually, we make a decision using the IF statement and then return a conclusion. Why not just directly return the expression we put in the IF statement. The expression will be executed first and its value will be returned. This way, we can save an IF statement, making our program more concise and easier to maintain.
Program efficiency is always important. Iterating over an array is a rather expensive operation. You should avoid unnecessary iterations.
The Array.reduce() method is the most versatile method for working with arrays, especially when you need to draw a "conclusion" by analyzing all elements. This method offers us the perfect opportunity to develop our creativity as a programmer.
By iterating over an array and examine every element, we can deduplicate a given array. Can you fit the deduplicating program into the frame of the Array.reduce() method?
Let's say we want to find the items with the highest price and collect them in an array. The challenge is there may be multiple matches. So the solution is to find the highest price value first. Then we select elements with that value.
We want to find the highest price and items with the highest price at the same time, within one loop. Can this be done? Most importantly, can this be done using only one Array.reduce() method?
We still have an array with duplicated elements. We want to know the number of occurrences of each element. Then we want to find the most frequent elements and the max number of occurrences.
We want to know the number of occurrences of the most frequent elements, and we also want to collect all the most frequent elements. So we want to find these two pieces of information. Can we do this using only one Array.reduce()?
Group objects by a property with a single reduce, turning an array into an object of arrays based on rating, while handling missing ratings (undefined) and validating input.
Bubble sorting is the most fundamental sorting algorithm. It is essentially two nested loops. But there are several improvements we can make to enhance the program efficiency.
Implement bubble sort in JavaScript by iterating the array and swapping neighboring elements. Use a temporary variable and the for loop to move the biggest or smallest value to end.
Bubble sorting backwards moves the smallest element to the beginning by iterating from right to left, swapping with the previous element using an end index and a decreasing index.
Explore how bubble sort reorganizes a descending four-element array into ascending order by using nested loops, moving biggest elements to the end, and extending to handle any length.
Improve program efficiency by ending the outer loop early when no elements relocate. Use a sorted flag and break to skip remaining loops as the array becomes sorted.
Optimize the inner for loop by stopping early to reduce unnecessary comparisons when sorting an array, illustrating how outer loops find the smallest elements and improve efficiency.
Use the right sorting border to split sorted and unsorted sections and stop the inner loop at the last unsorted index, tracked by last exchange index, to avoid unnecessary comparisons.
Learn backwards iteration with a left sort border to avoid unnecessary comparisons when sorting in descending order, updating the border after exchanges for efficient, fully rearranged arrays.
Cocktail sorting blends forward and backward iteration to optimize array sorting, reducing outer loops compared to bubble sorting, and highlighting pendulum-like, alternating directions for improved efficiency.
Introduce left and right sort borders to limit inner for loops to the unsorted middle, revealing two sorted sections at the ends.
Explore the quick sort algorithm, a divide-and-conquer method using a pivot and partitioning to sort arrays through dual recursion until sections reach one or zero elements.
Implement quicksort in JavaScript by reviewing concatenate, slice, and splice, then define a recursive quicksort with a middle pivot to improve efficiency and sort left and right sections in order.
Explore counting sort, a no-comparison algorithm for sorting positive integers by using values as array indices to build ascending and descending results, noting its limits.
Learn to handle duplicated elements by counting occurrences with a Q array, initializing counts, and building ascending and descending arrays from the element values.
Improve counting sort efficiency by limiting the scan to the effective range from the minimum to the maximum, and use minimum-based for loop bounds to achieve ascending and descending order.
Improve program efficiency by shrinking the Q array through subtracting the smallest element and using a proxy element to index the Q array, then restoring values.
Learn how the insertion sort algorithm builds a sorted left section from an unsorted array by comparing and swapping elements to achieve ascending order, then implement it in JavaScript.
Create an insert sort function, iterate over the unsorted array, and use outer and inner loops to swap elements until the sorted portion grows in ascending order.
Learn bucket sort for JavaScript developers, including how to set buckets, distribute elements, and keep each bucket in ascending order before concatenating them into a sorted array.
Learn how to implement a bucket sort in JavaScript, from computing min and max values to distributing elements into buckets and merging them into a sorted array.
Explore the selection sort algorithm by locating the smallest element with a min index, swapping it into place, and repeating for the remaining array to produce a sorted list.
Implement selection sort in JavaScript using the array length, an outer loop, and an inner loop to find the min index, swap when needed, and verify with console logs.
Explore implementing a binary heap in JavaScript, using index math to locate parent and children, and percolate up by swapping until the root to maintain the mean heap.
Convert an ascending array into a max heap by building and pushing elements, using parent comparisons to bubble up, illustrating binary heap concepts and the basics of heap sort.
Demonstrates heap sorting with binary heaps, showing max and min heaps for descending and ascending order, and how shifting elements builds a sorted array in JavaScript.
Develop a heap sort function that converts an array to a max heap, extracts the root with shift, and rebuilds the heap until the array is sorted in descending order.
This course helps you become "programming smart." It offers training in logic reasoning, enabling you to come up with better solutions for real-world projects.
The length of this course is a little bit longer than two hours. But these two hours are challenging and really push your limits.
Every programmer needs to take a leap--from learning the syntax of a programming language or framework to solving real-world problems using what you have learned.
For example, a WHILE or FOR loop is very easy to learn. But nesting two loops together and adding an IF statement, you got yourself a bubble sorting algorithm. From there, you can make further improvements, turning the bubble sorting into the much more efficient cocktail sorting.
This leap is vital but not easy to take. It calls for creativity, which can be trained. This is also the goal of this lesson, helping and forcing you to take the vital leap.
In the first section, we will show you a couple of creative techniques that can significantly improve your code quality, giving you a new perspective of code design.
Very often, the solution also imposes obstacles. The solution for problem A is often incompatible with that of problem B. This requires you to be flexible, finding ways to get around obstacles.
The second section covers eight carefully picked sorting algorithms.
Sorting algorithms are the barbell for your brain. Honestly speaking, you probably never need to write a sorting program in your real-world projects. Basically, every language has built-in methods dedicated to sorting array elements. But it is the process, not the result, that matters. The process of creating a sorting program forces you to exercise your brain and enhance your logical reasoning. You won't see the benefit right away, but in the long term, you will benefit immensely from it.