⚡ JavaScript

JavaScript (Node.js) में सॉर्टिंग एल्गोरिदम: एक विस्तृत गाइड

What is it and why? (a real-life analogy)

Imagine you have a huge box of Lego bricks with all the parts mixed together: red bricks, blue plates, green wheels. You want to build a spaceship, but first you need to find all the red bricks of the same size. What do you do? Dump everything on the floor and start sorting through it? That would take forever!

A much smarter approach would be to sort the parts: arrange them by color, and within each color, by size. Now finding the right part takes just seconds. This is exactly what sorting algorithms do in programming.

Sorting is the process of ordering a set of data according to a specific rule. Most often, this is numerical order (from smallest to largest) or alphabetical order. Why is this needed in real programming?

  • Searching is faster. If an array of numbers is sorted, you can find the desired number using binary search in a fraction of a second, instead of iterating through all the elements.
  • Data becomes readable. An alphabetical list of students, a leaderboard by points, a product catalog by price — all of these require sorting.
  • Processing is simplified. For example, finding duplicates in a sorted array is much easier than in a chaotic one.
💡 Important: In real projects, you will often use built-in sorting methods (e.g., .sort() in JavaScript). But understanding how the algorithms work internally distinguishes a true programmer from someone who just copies code. It develops algorithmic thinking.

Theory: how it works under the hood

Let's look under the hood. Sorting algorithms differ from each other in strategy. Imagine you are a PE teacher and you need to line up the class by height. Here is how different algorithms would approach it:

Bubble Sort

You walk along the line and compare two neighboring students. If the left one is taller than the right one, you swap them. You do this many times until everyone is in order. The tallest one "bubbles up" to the end, like a bubble in soda. Very slow, but very simple to understand.

Selection Sort

You look for the shortest student in the entire line and put them at the beginning. Then you look for the second shortest and put them second. And so on. You make exactly as many passes as there are students.

Insertion Sort

You start with the second student. If they are shorter than the first, you insert them in front. Then you take the third student and insert them into the correct place among the first two. Gradually, the left part of the line becomes sorted, and the right part remains unsorted. This is how you usually sort cards in your hand during a game.

Quick Sort

You choose one student as the "pivot". Everyone shorter than them goes to the left, everyone taller goes to the right. Then you repeat the same process for the left and right groups. This is a "divide and conquer" strategy. A very fast method.

Merge Sort

You split the line in half, then split each half in half again — and so on until you are left with pairs or single students. Then you start putting them back together, but in the correct order, comparing elements from different halves. Also a very fast and stable method.

Algorithm Complexity (Big O Notation)

To understand which algorithm is better, programmers use "Big O" notation. It is a way to measure how quickly an algorithm's runtime grows as the amount of data increases.

Algorithm Best Case Average Case Worst Case Memory
Bubble O(n) O(n²) O(n²) O(1)
Selection O(n²) O(n²) O(n²) O(1)
Insertion O(n) O(n²) O(n²) O(1)
Quick O(n log n) O(n log n) O(n²) O(log n)
Merge O(n log n) O(n log n) O(n log n) O(n)
⚡ Remember: O(n²) is "quadratic complexity". If there are 10 elements, 100 operations are needed. If there are 1000, it's already 1,000,000. O(n log n) is "linearithmic complexity". For 1000 elements, only about 10,000 operations are needed. The difference is huge!

Syntax in JavaScript (Node.js): all forms and variants

JavaScript has a built-in .sort() method, but it doesn't work exactly as beginners think. Let's break it down thoroughly.

Basic .sort() Syntax

// Create an array of numbers
let numbers = [3, 1, 4, 1, 5, 9, 2, 6];

// Just call sort() — and we get... a surprise!
numbers.sort();
console.log(numbers); // [1, 1, 2, 3, 4, 5, 6, 9] — seems to work

// Now let's try with other numbers
let trickyNumbers = [10, 5, 80, 2, 100];
trickyNumbers.sort();
console.log(trickyNumbers); // [10, 100, 2, 5, 80] — what nonsense?!

Why did this happen? The thing is, .sort() by default converts all elements to strings and sorts them as strings! The string "10" comes before the string "2" because the first character '1' is less than '2'.

Correct Syntax with a Compare Function

// To sort numbers correctly, pass a compare function
let numbers = [10, 5, 80, 2, 100];

// Sort in ascending order