Introduction
Selection Sort is a simple comparison-based sorting algorithm that divides an array into sorted and unsorted portions. During each pass, it finds the smallest element from the unsorted portion and places it at the beginning of that portion. In this chapter, we will practice Selection Sort using JavaScript through different practical questions, including ascending and descending order, swap counting, duplicates, strings, objects, and reusable functions. Data Structure Selection Sort practice questions with solutions help to understand the concepts.
Question 1: Sort an Array Using Selection Sort
Questions
Use Selection Sort to arrange the following numbers in ascending order.
let numbers = [64, 25, 12, 22, 11];
Solution
let numbers = [64, 25, 12, 22, 11];
for (let i = 0; i < numbers.length - 1; 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(numbers);
Output
[11, 12, 22, 25, 64]
Selection Sort finds the smallest element and places it at the beginning of the unsorted portion.
Question 2: Sort an Array in Descending Order
Questions
Use Selection Sort to arrange the following numbers from largest to smallest.
let numbers = [29, 10, 14, 37, 13];
Solution
let numbers = [29, 10, 14, 37, 13];
for (let i = 0; i < numbers.length - 1; i++) {
let maxIndex = i;
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[j] > numbers[maxIndex]) {
maxIndex = j;
}
}
let temp = numbers[i];
numbers[i] = numbers[maxIndex];
numbers[maxIndex] = temp;
}
console.log(numbers);
Output
[37, 29, 14, 13, 10]
For descending order, we search for the largest element instead of the smallest element.
Question 3: Count the Number of Swaps
Questions
Use Selection Sort and count how many swaps are performed.
let numbers = [29, 10, 14, 37, 13];
Solution
let numbers = [29, 10, 14, 37, 13];
let swaps = 0;
for (let i = 0; i < numbers.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j;
}
}
if (minIndex !== i) {
let temp = numbers[i];
numbers[i] = numbers[minIndex];
numbers[minIndex] = temp;
swaps++;
}
}
console.log("Sorted array:", numbers);
console.log("Total swaps:", swaps);
Output
Sorted array: [10, 13, 14, 29, 37]
Total swaps: 3
The swap counter increases only when the minimum element is different from the current position.
Question 4: Display the Array After Every Pass
Questions
Use Selection Sort and display the array after every pass.
let numbers = [5, 3, 4, 1, 2];
Solution
let numbers = [5, 3, 4, 1, 2];
for (let i = 0; i < numbers.length - 1; 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("Pass " + (i + 1) + ":", numbers);
}
Output
Pass 1: [1, 3, 4, 5, 2]
Pass 2: [1, 2, 4, 5, 3]
Pass 3: [1, 2, 3, 5, 4]
Pass 4: [1, 2, 3, 4, 5]
Each pass places one more element into its correct position.
Question 5: Find the Minimum Element During Selection Sort
Questions
Use the Selection Sort approach to find the smallest element in the array.
let numbers = [45, 12, 78, 3, 29];
Solution
let numbers = [45, 12, 78, 3, 29];
let minIndex = 0;
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] < numbers[minIndex]) {
minIndex = i;
}
}
console.log("Smallest element:", numbers[minIndex]);
Output
Smallest element: 3
The minimum element can be identified by continuously updating minIndex.
Question 6: Selection Sort an Array with Duplicate Values
Questions
Sort the array using Selection Sort while keeping all duplicate values.
let numbers = [4, 2, 7, 2, 5, 4];
Solution
let numbers = [4, 2, 7, 2, 5, 4];
for (let i = 0; i < numbers.length - 1; 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(numbers);
Output
[2, 2, 4, 4, 5, 7]
Selection Sort does not remove duplicate values; it simply places them in sorted order.
Question 7: Selection Sort Strings Alphabetically
Questions
Use Selection Sort to arrange the following strings alphabetically.
let languages = ["Python", "C", "Java", "Ruby"];
Solution
let languages = ["Python", "C", "Java", "Ruby"];
for (let i = 0; i < languages.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < languages.length; j++) {
if (languages[j] < languages[minIndex]) {
minIndex = j;
}
}
let temp = languages[i];
languages[i] = languages[minIndex];
languages[minIndex] = temp;
}
console.log(languages);
Output
["C", "Java", "Python", "Ruby"]
JavaScript compares the strings lexicographically to determine their alphabetical order.
Question 8: Selection Sort Students by Marks
Questions
Use Selection Sort to arrange students from the highest marks to the lowest marks.
let students = [
{ name: "Rahul", marks: 72 },
{ name: "Priya", marks: 91 },
{ name: "Aman", marks: 65 },
{ name: "Neha", marks: 84 }
];
Solution
let students = [
{ name: "Rahul", marks: 72 },
{ name: "Priya", marks: 91 },
{ name: "Aman", marks: 65 },
{ name: "Neha", marks: 84 }
];
for (let i = 0; i < students.length - 1; i++) {
let maxIndex = i;
for (let j = i + 1; j < students.length; j++) {
if (students[j].marks > students[maxIndex].marks) {
maxIndex = j;
}
}
let temp = students[i];
students[i] = students[maxIndex];
students[maxIndex] = temp;
}
console.log(students);
Output
[
{ name: "Priya", marks: 91 },
{ name: "Neha", marks: 84 },
{ name: "Rahul", marks: 72 },
{ name: "Aman", marks: 65 }
]
The marks property is used to identify which student should be placed at each position.
Question 9: Create a Reusable Selection Sort Function
Questions
Create a reusable function called selectionSort() that accepts an array and returns the sorted array.
let numbers = [20, 5, 15, 10, 1];
Solution
function selectionSort(array) {
for (let i = 0; i < array.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
if (minIndex !== i) {
let temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
return array;
}
let numbers = [20, 5, 15, 10, 1];
console.log(selectionSort(numbers));
Output
[1, 5, 10, 15, 20]
The function can now be reused with different arrays.
Question 10: Sort Products by Price Using Selection Sort
Questions
Use Selection Sort to arrange the products from the lowest price to the highest price.
let products = [
{ name: "Laptop", price: 55000 },
{ name: "Mouse", price: 800 },
{ name: "Keyboard", price: 1500 },
{ name: "Monitor", price: 12000 }
];
Solution
let products = [
{ name: "Laptop", price: 55000 },
{ name: "Mouse", price: 800 },
{ name: "Keyboard", price: 1500 },
{ name: "Monitor", price: 12000 }
];
for (let i = 0; i < products.length - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < products.length; j++) {
if (products[j].price < products[minIndex].price) {
minIndex = j;
}
}
let temp = products[i];
products[i] = products[minIndex];
products[minIndex] = temp;
}
console.log(products);
Output
[
{ name: "Mouse", price: 800 },
{ name: "Keyboard", price: 1500 },
{ name: "Monitor", price: 12000 },
{ name: "Laptop", price: 55000 }
]
The algorithm compares the price property and places the cheapest product first.
Key Takeaways
- Selection Sort divides the array into sorted and unsorted portions.
- It searches for the minimum element in the unsorted portion.
- The minimum element is placed at the beginning of the unsorted portion.
- For descending order, Selection Sort can search for the maximum element instead.
- Selection Sort works with numbers, strings, and objects.
- Duplicate values can be sorted normally.
- A
minIndexvariable is commonly used to track the smallest element. - Selection Sort generally performs at most
n - 1swaps. - Its best-case, average-case, and worst-case time complexity is O(n²).
- Its extra space complexity is O(1) when implemented in-place.
- Selection Sort is simple to understand but is usually inefficient for large datasets.
FAQs
1. What is Selection Sort?
Selection Sort is a comparison-based sorting algorithm that repeatedly finds the smallest element from the unsorted portion and places it in the correct position.
2. How does Selection Sort work?
It searches the unsorted portion for the minimum element, swaps it with the first unsorted element, and then repeats the process for the remaining elements.
3. What is the time complexity of Selection Sort?
Selection Sort has O(n²) time complexity in the best, average, and worst cases.
4. Does Selection Sort work with duplicate values?
Yes. Selection Sort can sort arrays containing duplicate values without removing them.
5. Can Selection Sort sort in descending order?
Yes. Instead of finding the minimum element, the algorithm can find the maximum element during each pass.
6. What is the space complexity of Selection Sort?
The standard in-place implementation uses O(1) extra space.
7. Is Selection Sort suitable for large datasets?
Generally, no. Its O(n²) time complexity makes it inefficient for large datasets. More efficient algorithms are usually preferred for large collections.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
