Mark As Completed Discussion

Selection Sort

Selection Sort is a simple comparison-based sorting algorithm. It works by dividing the input list into two parts: the sorted part and the unsorted part. Initially, the sorted part is empty, and the unsorted part contains all the elements. The algorithm repeatedly selects the smallest element from the unsorted part and moves it to the sorted part.

Steps of Selection Sort

  1. Find the minimum element in the unsorted part.
  2. Swap the minimum element with the first element of the unsorted part.
  3. Increment the boundary of the sorted part.
  4. Repeat steps 1-3 until the entire array is sorted.

Time Complexity

The time complexity of Selection Sort is O(n^2) in all cases, where n is the number of elements in the array. This is because the algorithm involves two nested loops, resulting in quadratic time complexity. Selection Sort is not efficient for large data sets.

Example

Let's see an implementation of Selection Sort in Java:

TEXT/X-JAVA
1${code}
JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment