TypeScript Program to Implement Selection Sort

TypeScript Program to Implement Selection Sort

Selection Sort is a simple and classic sorting algorithm that every beginner should know. The main idea behind Selection Sort is very easy to understand. The algorithm looks for the smallest value in a list, places it at the beginning, and then repeats the same process for the remaining part of the list. Step by step, the array becomes sorted from left to right. Because of this clear logic, Selection Sort is often taught early in programming courses.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Even though Selection Sort is not the fastest algorithm for large data, it is still very important. It helps beginners learn how arrays work, how comparisons are done, and how swapping values changes data. Selection Sort is commonly used in learning environments, interviews for basic understanding, and small programs where simplicity matters more than speed. Implementing Selection Sort in TypeScript is also helpful because TypeScript is widely used in modern web applications and builds strong JavaScript fundamentals.

Program 1: Basic Selection Sort Using Nested Loops

This first program shows the most traditional way to implement Selection Sort. It uses two loops to find the smallest element and place it in the correct position. This version is perfect for beginners who want to clearly see how the algorithm works.

let numbers: number[] = [64, 25, 12, 22, 11];

for (let i = 0; i < numbers.length; i++) {

    let minIndex = i;

    for (let j = i + 1; j < numbers.length; j++) {

        if (numbers[j] < numbers[minIndex]) {
            minIndex = j;
        }

    }

    let temp = numbers[i];
    numbers[i] = numbers[minIndex];
    numbers[minIndex] = temp;

}

console.log("Sorted array:", numbers);

This program works by dividing the array into a sorted and an unsorted part. In each pass, it finds the smallest value from the unsorted section and swaps it with the first unsorted element. Beginners can easily follow this logic and understand how values slowly move into their correct positions.

Program 2: Selection Sort Using a Function

This version places the Selection Sort logic inside a function. It helps beginners understand how functions make code reusable and cleaner. You can call the same function for different arrays without rewriting the logic.

function selectionSort(arr: number[]): number[] {

    for (let i = 0; i < arr.length; i++) {

        let minIndex = i;

        for (let j = i + 1; j < arr.length; j++) {

            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }

        }

        let temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;

    }

    return arr;

}

let data: number[] = [29, 10, 14, 37, 13];

console.log("Sorted array:", selectionSort(data));

By using a function, the program becomes more organized and easier to maintain. Beginners also learn how parameters and return values work in TypeScript. This approach is useful in real projects where the same sorting logic is needed in multiple places.

Program 3: Selection Sort Using a While Loop

This program uses a while loop instead of a for loop to control the sorting process. It shows that Selection Sort can be written in different ways while still following the same idea.

let values: number[] = [5, 3, 6, 2, 10];
let i = 0;

while (i < values.length) {

    let minIndex = i;
    let j = i + 1;

    while (j < values.length) {

        if (values[j] < values[minIndex]) {
            minIndex = j;
        }

        j++;

    }

    let temp = values[i];
    values[i] = values[minIndex];
    values[minIndex] = temp;

    i++;

}

console.log("Sorted array:", values);

This version helps beginners understand how while loops work and how loop variables are controlled manually. It is useful for learning different looping styles and seeing that the core Selection Sort logic stays the same.

Program 4: Recursive Selection Sort in TypeScript

This program uses recursion to implement Selection Sort. While recursion is not commonly used for this algorithm, it is a great learning exercise for beginners.

function recursiveSelectionSort(arr: number[], startIndex: number): number[] {

    if (startIndex >= arr.length - 1) {
        return arr;
    }

    let minIndex = startIndex;

    for (let i = startIndex + 1; i < arr.length; i++) {

        if (arr[i] < arr[minIndex]) {
            minIndex = i;
        }

    }

    let temp = arr[startIndex];
    arr[startIndex] = arr[minIndex];
    arr[minIndex] = temp;

    return recursiveSelectionSort(arr, startIndex + 1);

}

let nums: number[] = [8, 4, 1, 9, 3];

console.log("Sorted array:", recursiveSelectionSort(nums, 0));

Each recursive call sorts one position and then moves to the next index. This program helps beginners understand how a problem can be broken into smaller parts. Learning recursion early makes it easier to understand more advanced algorithms later.

Program 5: Selection Sort in Descending Order

This final program sorts the array in descending order instead of ascending order. It shows how small changes in comparison logic can change the result.

let items: number[] = [20, 12, 10, 15, 2];

for (let i = 0; i < items.length; i++) {

    let maxIndex = i;

    for (let j = i + 1; j < items.length; j++) {

        if (items[j] > items[maxIndex]) {
            maxIndex = j;
        }

    }

    let temp = items[i];
    items[i] = items[maxIndex];
    items[maxIndex] = temp;

}

console.log("Sorted array in descending order:", items);

Instead of finding the smallest value, this program finds the largest value and places it at the correct position. Beginners can clearly see how changing one condition affects the entire output. This flexibility is useful in real-world sorting tasks.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about Selection Sort in TypeScript using simple explanations.

Q1. What is Selection Sort used for?
Selection Sort is mainly used for learning and understanding sorting basics. It is simple and clear, making it great for beginners.

Q2. Is Selection Sort better than Bubble Sort?
Selection Sort usually performs fewer swaps than Bubble Sort, but both are slow for large data. They are mainly used for learning purposes.

Q3. Can Selection Sort handle negative numbers?
Yes, Selection Sort works perfectly with negative numbers as long as comparisons are done correctly.

Q4. Is recursion necessary for Selection Sort?
No, recursion is not necessary. It is only used here to help beginners learn recursive thinking.

Q5. What should I learn after Selection Sort?
After Selection Sort, beginners often move on to Insertion Sort, Merge Sort, and Quick Sort.

Conclusion

Selection Sort is a simple yet powerful algorithm for building strong programming fundamentals. In this article, we explored multiple TypeScript programs that implement Selection Sort using loops, functions, recursion, and even sorting in reverse order. Each approach showed a different way to solve the same problem while keeping the logic easy to understand.

If you are new to TypeScript or sorting algorithms, take time to run these programs and modify the data. Practice will help you gain confidence and prepare you for more advanced algorithms. Keep learning, keep experimenting, and enjoy your coding journey.

Scroll to Top