> For the complete documentation index, see [llms.txt](https://blog.sunilgudivada.dev/notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.sunilgudivada.dev/notebook/data-structures-and-algorithms/topics/sorting/selection-sort.md).

# Selection Sort

* Select the min `j` `( j > i )`, swap with `i`

## Java Implementation

```java
/*
==========================================================
Generic Implementation of selection sort
Input: Used Comparable interface as input

Input: [ 3, 0, 5, 2, 1, 0, 0, 0, 4 ]
Output: [ 0, 0, 0, 0, 1, 2, 3, 4, 5 ]

Input: [ "three", "0", "five", "2", "one", "seven", "4" ]
Output: [ "0", "2", "4", "five", "one", "seven", "three" ]

==========================================================
*/

public class Selection {
    public void sort(Comparable[] a) {
      int N = a.length;
      for (int i = 0; i < N; i++) {
        int min = i;
        for (int j = i + 1; j < N; j++){
           if (compare(a[j], a[min])){
              min = j;
            }
        }
        swap(a, i, min);
      }
    }

    private void swap(Comparable[] a, int i, int j) {
      Comparable swap = a[i];
      a[i] = a[j];
      a[j] = swap;
    }

    private boolean compare(Comparable comparable, Comparable comparable1) {
      return comparable.compareTo(comparable1) < 0;
    }
  }
```

{% hint style="success" %}

### **Properties**

* $$O ( 1 )$$ extra space
* $$\theta(n^{2})$$ comparisons
* $$\theta(n)$$ swaps
* Not stable
* Not adaptive
  {% endhint %}

## Reference

* <https://www.toptal.com/developers/sorting-algorithms/selection-sort>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.sunilgudivada.dev/notebook/data-structures-and-algorithms/topics/sorting/selection-sort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
