Mark As Completed Discussion

Selection Sort:

This algorithm is an in-place comparison-based algorithm that divided the list into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right.

The working of the selection sort algorithm is simulated in the below-mentioned figure:

Selection Sort

Here's some pseudo-code to follow:

SNIPPET
1procedure selection sort
2   arr  : array of items
3   N    : size of list
4
5   for i = 1 to N - 1
6   /* set current element as minimum*/
7      min = i      
8      /* check the element to be minimum */
9      for j = i+1 to N
10         if arr[j] < arr[min] then
11            min = j;
12         end if
13      end for
14      /* swap the minimum element with the current element*/
15      if indexMin != i  then
16         swap arr[min] and arr[i]
17      end if
18   end for
19	
20end procedure

Time Complexity: O(N2)

Space Complexity: O(1)

Best Suited Scenario:

The selection sort algorithm is best suited with Array data structures and the given collection is in completely unsorted order. This algorithm is not feasible for a very large number of data set and this is likely due to its time complexity.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment