📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Selection Sort Algorithm in JavaScript
function selectionSort(arr) {
for (var i = 0; i < arr.length; i++) {
let min = i; // storing the index of minimum element
for (var j = i + 1; j < arr.length; j++) {
if (arr[min] > arr[ j ]) {
min = j; // updating the index of minimum element
}
}
if (i !== min) {
let temp = arr[ i ];
arr[ i ] = arr[min];
arr[min] = temp;
}
}
return arr
}
Using Bubble Sort Algorithm
const array = [2,4,3,1,6,5,7,8,9];
console.log("Before selection sort - " + array);
let array1 = selectionSort(array);
console.log("After selection sort - "+ array1);
Output
Before selection sort - 2,4,3,1,6,5,7,8,9
After selection sort - 1,2,3,4,5,6,7,8,9
Comments
Post a Comment
Leave Comment