Bubble Sort is one of the oldest and simplest sorting algorithms in computer science. It works by comparing two neighboring values and swapping them if they are in the wrong order. This process repeats again and again until the whole list becomes sorted. The idea is easy to understand, which is why Bubble Sort is often the first sorting algorithm beginners learn when studying programming.
with hands-on learning.
get the skills and confidence to land your next move.
Even though Bubble Sort is not the fastest method, it still matters a lot. It helps beginners understand how sorting works behind the scenes, how loops behave, and how data moves inside an array. In real projects, developers usually use faster algorithms, but Bubble Sort builds strong foundations. Learning it in TypeScript is especially useful because TypeScript is widely used in modern web development and helps you write safer JavaScript code.
Program 1: Basic Bubble Sort Using Nested Loops
This first program shows the most traditional and commonly taught Bubble Sort approach. It uses two loops to repeatedly compare and swap values until the array is sorted. This version is perfect for beginners because it closely follows the textbook explanation.
let numbers: number[] = [64, 34, 25, 12, 22, 11, 90];
for (let i = 0; i < numbers.length; i++) {
for (let j = 0; j < numbers.length - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
let temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
console.log("Sorted array:", numbers);This program works by moving the largest value to the end during each pass, just like bubbles rising to the surface of water. The inner loop handles comparisons, while the outer loop controls how many passes are needed. Beginners can easily follow this logic and see how values slowly fall into the correct order.
Program 2: Bubble Sort Using a While Loop
This version uses a while loop instead of fixed loop counts. It keeps running until no swaps are needed, which means the array is already sorted. This approach feels more natural and closer to how humans think about sorting.
let values: number[] = [5, 1, 4, 2, 8];
let swapped = true;
while (swapped) {
swapped = false;
for (let i = 0; i < values.length - 1; i++) {
if (values[i] > values[i + 1]) {
let temp = values[i];
values[i] = values[i + 1];
values[i + 1] = temp;
swapped = true;
}
}
}
console.log("Sorted array:", values);Here, the program keeps checking if any swaps happen in a pass. If no swaps occur, the array is already sorted and the loop stops. This version helps beginners understand how conditions control program flow and how Bubble Sort can stop early when the data is nearly sorted.
Program 3: Bubble Sort Using a Function
In this program, Bubble Sort logic is placed inside a reusable function. This is a good step toward writing cleaner and more professional TypeScript code.
function bubbleSort(arr: number[]): number[] {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
let data: number[] = [10, 7, 8, 9, 1, 5];
console.log("Sorted array:", bubbleSort(data));This approach is useful because it allows you to sort different arrays without rewriting the logic. Beginners learn how functions work, how parameters are passed, and why separating logic makes programs easier to manage and understand.
Program 4: Recursive Bubble Sort in TypeScript
This version uses recursion, which means the function calls itself. While this is not the most efficient way, it helps beginners understand recursion with a familiar algorithm.
function recursiveBubbleSort(arr: number[], n: number): number[] {
if (n === 1) {
return arr;
}
for (let i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
return recursiveBubbleSort(arr, n - 1);
}
let nums: number[] = [3, 2, 1, 5, 4];
console.log("Sorted array:", recursiveBubbleSort(nums, nums.length));Each recursive call reduces the size of the problem until only one element remains. This program helps beginners see how a big problem can be broken into smaller ones. Understanding this builds confidence for learning more advanced topics later.
Program 5: Optimized Bubble Sort with Early Exit
This final program improves Bubble Sort slightly by stopping early if the array is already sorted. It is still simple, but more efficient than the basic version.
let items: number[] = [1, 2, 3, 4, 5];
let isSorted: boolean;
for (let i = 0; i < items.length; i++) {
isSorted = true;
for (let j = 0; j < items.length - i - 1; j++) {
if (items[j] > items[j + 1]) {
let temp = items[j];
items[j] = items[j + 1];
items[j + 1] = temp;
isSorted = false;
}
}
if (isSorted) {
break;
}
}
console.log("Sorted array:", items);This program checks if a full pass happens without any swaps. If so, it stops immediately. Beginners can see how small changes in logic can improve performance, which is an important lesson in programming.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Bubble Sort in TypeScript, using simple explanations to clear confusion.
Q1. Is Bubble Sort used in real applications?
Bubble Sort is rarely used in real systems because it is slow for large data. However, it is very useful for learning and teaching basic sorting logic.
Q2. Why should I learn Bubble Sort in TypeScript?
Learning Bubble Sort in TypeScript helps you understand arrays, loops, conditions, and functions while also practicing a modern programming language.
Q3. Is recursion better than loops for Bubble Sort?
Recursion is not better here, but it helps you understand how recursive thinking works. Loops are usually clearer and faster for this algorithm.
Q4. Can Bubble Sort handle negative numbers?
Yes, Bubble Sort works with negative numbers as long as comparisons are done correctly.
Q5. What should I learn after Bubble Sort?
After Bubble Sort, beginners often move on to algorithms like Selection Sort, Insertion Sort, and later faster ones like Quick Sort.
Conclusion
Bubble Sort may be simple, but it plays a big role in building strong programming basics. In this article, we explored multiple TypeScript programs that implement Bubble Sort using loops, functions, recursion, and small optimizations. Each version showed a different way to think about the same problem.
If you are just starting out, take time to run these programs, change the data, and watch how the output changes. Practice is the best teacher. As you grow more confident, you will find it easier to learn faster and more complex sorting algorithms. Keep exploring, keep coding, and enjoy the journey.




