# Selection Sort Algorithm

May 15, 2019

It is one of a simple algorithm to study for a beginner to understanding sorting algorithms.

In this algorithm, we start with 0th index value. We compare it in whole array to find if any other number is smaller than this. So effectively, we find the minimum number through rest of the array from our starting index.

And, we swap the numbers of the initial number and the smalles number found in array.

## Selection Sort Algorithm

• Inner loop finds the minimum number
• Swap the indexes, if found the smaller number
• Inner loop breaks, Outer loop move ahead.
• So, we left the smallest number found so far in beginning. And, sort rest of the array

See the code here:

``````public void sort(int[] arr) {
int l = arr.length;
for (int i=0; i<l-1; i++) {
int smallestNumIndex = i;
for (int j=i+1; j<l; j++) {
if (arr[smallestNumIndex] > arr[j]) {
smallestNumIndex = j;
}
}
if (i != smallestNumIndex) {
//swap
int temp = arr[i];
arr[i] = arr[smallestNumIndex];
arr[smallestNumIndex] = temp;
}
}
}``````

## Key Points

• Its an in-place sorting algorithm
• Performance is usually worse than Insertion sort
• Applicable for small set of input only
• Its very simple algorithm

## Runtime complexity

The algorithm runs on O(n^2) in worst/average case.

## Similar Posts

### List of Sorting Algorithms

This topic is one of the most common studied. When somebody started preparation…

### Rotate Array - Leet Code Solution

Problem Statement Given an array, rotate the array to the right by k steps…

### Graph Topological Sorting - Build System Order Example

Graph Topological Sorting This is a well known problem in graph world…

### Leetcode - Rearrange Spaces Between Words

Problem Statement You are given a string text of words that are placed among…

### Max Priority Queue Implementation with Heap Data structure

Max Priority Queue is a data structure which manage a list of keys(values). And…

### Min Priority Queue Implementation with Heap Data structure

Min Priority Queue is a data structure which manage a list of keys(values). And…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…