In this article, we will explain the idea behind Selection Sort and implement it in JavaScript and how to work Selection Short in JavaScript.
Selection sort is the easiest and most intuitive sorting algorithm and is a comparison algorithm.
If you want then buy a good, reliable, secure web hosting service from here: click here
Now, we can say that it transforms the input collection without using any auxiliary data structure and the input is overridden by the output.
Previous JavaScript Articles
- JavaScript Variables
- JavaScript Operators
- Creating an Object in JavaScript
- Introduction to Asynchronous JavaScript
- Control Flow in JavaScript
- What is JavaScript Regex?
- JavaScript Events Example
- How to create a preloader in JavaScript?
- forEach method in JavaScript
- Sorting Arrays in JavaScript
- Linear Search in JavaScript
- Pagination in Vanilla JavaScript
- Transform Arrays with Map() Method
- How to change an element’s class with JavaScript
Basically, it’s usually taught early in most computer science courses, because it’s so easy to understand and translate into code.
Despite its quadratic time complexity, it has performance advantages over more complex sorting algorithms in certain cases, such as Quicksort or Merge Sort.
Selection Sort
Hence, the algorithm splits the input array into two sublists – a sorted and an unsorted sublist.
You can purchase your hosting from Cloudsurph.com, Cloudsurph hosting is a reliable hosting option for business and personal projects. We offer insight and help on system configuration issues and code errors or bugs.
So, in the beginning, the sorted list is located in the collection and all elements to the right of the final sorted element are treated as unsorted.
If you want then buy a good, reliable, secure web hosting service from here: click here
function selectionSort(arr) { const len = arr.length; for (let i = 0; i < len - 1; i++) { let minIndex = i; for (let j = i + 1; j < len; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } if (minIndex !== i) { [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; } } return arr; }
How to work Selection Short in JavaScript
- The function selectionSort takes an array arr as input.
- The length of the array is stored in the variable lane.
- The outer loop iterates through the array from the first element to the second-to-last element.
- The variable minIndex is set to the current index i.
- The inner loop iterates through the array from the element after the current index to the last element.
- If the current element is less than the element at the minIndex, the minIndex is set to the index of the current element.
- If minIndex is not equal to i, then the elements at indices i and minIndex are swapped.
- Once the outer loop completes, the sorted array is returned.
Note that this implementation modifies the input array in place rather than returning a new array.
Conclusion
Selection sort is a basic and simple, therefore inefficient sorting algorithm. It served as a basis for some of the widely used and adopted algorithms and isn’t used very often in the development industry.
If you want then buy a good, reliable, secure web hosting service from here: click here
That’s it. If you enjoyed reading this article and have more questions please reach out to our support team via live chat or email and we would be glad to help you. we provide server hosting for all types of need and we can even get your server up and running with the service of your choice.