Selection Sort

  • In selection sort, we split the sequence of elements into two parts: sorted and unsorted. At the beginning, all elements are in the unsorted part.

  • We go through the sequence of elements NN times. Each time, we find the smallest element and append it to the end of the sorted part. We do so by swapping the smallest element with the first element in the sorted part. In other words, every time, we select the smallest element in the unsorted part; that element will be the next largest element in the sorted part.

Example

Complexity

  • This sort is in-place and always takes Θ(N2)\Theta(N^2) time, since we have to make NN selections and it takes NN time to go through the sequence of input and make a selection.

Visualization

results matching ""

    No results matching ""