Sorting Algorithms Overview


Sorting Algorithms Overview

Michael
Sorting algorithms are everywhere in computer science. Today we're going to break down the most popular ones: selection sort, bubble sort, insertion sort, and merge sort. We'll see how they work and why some are faster than others.

Corey
Great. But before we jump into those, the transcript started with a really simple sorting algorithm. It's basically two nested loops that compare every element with every other element and swap them into place. That's the brute‑force way, right?

Michael
Exactly. It's not efficient, but it's easy to understand. You have an outer loop with index i, and an inner loop with j starting just after i. Whenever the element at j is smaller than the element at i, you swap them. After the first pass, the smallest element ends up at position 0, and so on.

Corey
I remember the visual: an array like 2, 3, 1, 5. You check 2 against 3 – no swap. Then 2 against 1 – swap, so now 1 is at position 0. Then 2 against 5 – no swap. Then i moves to position 1, and you keep going until the array is sorted. That's a simple selection sort in disguise.

Michael
Exactly. The proper selection sort formalises that idea. You have a separate swap function and a function to find the location of the smallest element in the unsorted part. You repeatedly find the smallest element from position i to the end and swap it into position i, then increment i.

Corey
So it's like you're choosing the next smallest and putting it where it belongs. The code has three functions: swap, locationOfSmallest, and selectionSort. The locationOfSmallest scans from i to n-1 and returns the index of the smallest value. Then you swap that element with the one at i.

Michael
Right. And the video showed a nice diagram: an array like 5, 4, 3, 7, 9. The algorithm starts at index 0, finds 3 as the smallest, swaps it with 5. Then i moves to index 1, finds 4 as the smallest in the remaining part, and leaves it because 4 is already there. Then i=2, compares 5, 7, 9 – no smaller, so it's fine. And so on.

Corey
That's clear. Now bubble sort works differently. Instead of looking for the smallest, it compares adjacent elements and swaps them if they're in the wrong order. It gets its name because larger elements "bubble up" to the end.

Michael
Yeah. The bubble sort implementation in the transcript has a bubbleProcess function that starts from the last element and works upward. It compares each pair (i and i‑1) and swaps if the lower one is smaller – effectively moving the smaller element upward. That's one pass. Multiple passes are needed until the array is sorted.

Corey
I liked the visual: an array 3, 5, 1, 4, 3. Starting at the bottom, you compare 3 and 4 – 3 is smaller so they stay. Then compare 1 and 4 – 1 is smaller, swap? Actually, the algorithm compares from the bottom: compare 4 and 3 (adjacent), 3 is smaller, swap. Then compare 3 and 1, swap. Then compare 1 and 5, swap. Then compare 1 and 3, swap. After the first pass, the smallest element (1) is at the top. Then you do another pass from the bottom, but you can ignore the last element because it's already in place.

Michael
Exactly. The code uses a while loop inside bubbleProcess that goes from n‑1 down to i, and inside that, if the current element is smaller than the one before it, they swap. After each full pass, the smallest unsorted element finds its place. That's why bubble sort is O(n²) – it makes many passes.

Corey
Now insertion sort takes a different approach. It assumes a part of the array is already sorted and inserts each new element into the correct position by shifting elements to the right.

Michael
Right. In the code, the main function calls InsertionSort, which loops i from 1 to n‑1. For each i, it calls insertElement, which takes the current element (called the key) and moves it left until it finds the correct spot. The key is stored, and a while loop shifts elements that are larger than the key to the right, making room.

Corey
The visual: an array 3, 7, 9, 4. The sorted part initially is just the first element (3). i=1, key is 7, compare with 3 – 3 is smaller, so no shift. i=2, key is 9, compare with 7 – no shift. i=3, key is 4, compare with 9 – 9 is larger, so shift 9 to the right. Then compare with 7 – shift 7. Then compare with 3 – 3 is smaller, so stop. Then place the key (4) in the gap. That yields 3, 4, 7, 9.

Michael
Insertion sort is efficient for small or nearly sorted arrays, but still O(n²) in the worst case. Now merge sort is a big step up – it uses a divide‑and‑conquer strategy and runs in O(n log n) time.

Corey
The code has a mergeSort wrapper that calls a helper function with start and end indices. That helper finds the midpoint, recursively sorts the left and right halves, and then calls a combine function to merge the two sorted halves.

Michael
Exactly. The combine function creates a temporary buffer, copies the whole segment into it, then uses two pointers – one for the left half (starting at start) and one for the right half (starting at mid+1) – and compares the elements, putting the smaller one into the original array. After one half is exhausted, the remaining elements from the other half are copied over.

Corey
The video showed a great example: two sorted halves – left half 2, 7, 4, 5 and right half 3, 11, 15, 19, 18, 21? Wait, the example in the transcript was a bit messy. But the key point is that merge sort always works in O(n log n) time, making it much faster for large arrays compared to the O(n²) sorts we discussed.

Michael
Right. The transcript mentioned that if you have a huge dataset, bubble sort or selection sort will slow down dramatically because the time grows quadratically. Merge sort stays manageable. That's why it's used in many real‑world applications.

Corey
So we've covered the main ideas: the simple brute‑force algorithm, selection sort, bubble sort, insertion sort, and finally merge sort. Each has its use cases, but merge sort wins on speed.

Michael
And the transcript ended with a teaser: they'll compare the actual performance of these algorithms with large data sets in future videos. But for now, understanding how each one works is a solid foundation. I'd recommend coding them yourself to really get the hang of it.

Corey
Absolutely. Thanks for walking through all these sorting algorithms with me. This was really helpful.

Michael
You're welcome. Keep practicing, and you'll have sorting down in no time.

Ask anything
We've read this whole study set, so questions about it get the best answers.