search
HomeJavajavaTutorialSelection Sort In Java

Selection Sort in Java is a sorting method that continually finds the smallest element in the unsorted part and keeps it in the beginning (for sorting in ascending order). The process will be repeated until the input array is sorted. Also, in Selection Sort, we will be dividing the input array into two subarrays where one array is used for sorted elements and the other array is for unsorted elements. In the beginning, there won’t be any elements in the sorted subarray. Let us see the working of the selection sort in detail in the following section.

How Selection Sort Works in Java

Selection sort works in a simple manner where it keeps two subarrays from the input array. They are:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • Sorted subarray to keep the sorted elements
  • Unsorted subarray to keep the unsorted elements.

Algorithm

Following is the algorithm that is used for Selection sort

  1. Set the minimum (MIN) pointer to location 0.
  2. Find the smallest element from the list of elements in the array
  • Swap the minimum element with the location 0
  1. Move the MIN pointer to the next position
  2. Repeat the process until the input array is sorted.

Let us understand the selection sort with an example. Following is the input array that has to be sorted. The elements in Bold Blue color will be as part of the sorted array.

Selection Sort In Java

Step 1: Set the MIN pointer to the first location. So, the MIN pointer points to 15.

Selection Sort In Java

Smallest: = 15

Step 2: Find the smallest element by comparing it with the rest of the elements. Comparing 15 and 21, 15 is the smallest. So, the smallest won’t change in this case.

Selection Sort In Java

Smallest: = 15

Comparing 15 and 6, 6 is the smallest.

Selection Sort In Java

Smallest: = 6

Comparing 6 and 3, 3 is the smallest.

Selection Sort In Java

Smallest: = 3

3 will be smaller in this case also, as 19 is greater than 3.

Selection Sort In Java

Smallest: = 3

Selection Sort In Java

Smallest: = 3

Finally, in this iteration, 3 is found to be the smallest.

Step 3: Swap the smallest element with the element in location 0.

Selection Sort In Java

Step 4: Increment the MIN pointer to the next position.

Selection Sort In Java

Step 5: Find the next smallest element by comparing it with the rest of the elements.

Selection Sort In Java

Smallest: = 21

Selection Sort In Java

Smallest: = 6

Selection Sort In Java

Smallest: = 6

Selection Sort In Java

Smallest: = 6

Selection Sort In Java

Smallest: = 6

Step 6: Swap the smallest element with the element in location 1.

Selection Sort In Java

Repeat the process until a sorted array is formed, as shown below.

Selection Sort In Java

Examples to Implement Selection Sort in Java

As already mentioned above, selection sort is based on finding the minimum and swapping. Now, let us see how to implement the selection sort using Java.

Java Program to Sort the Elements in an Array using Selection Sort

Code:

import java.util.*;
public class SelSortExample {
//Method that implements Selectionsort
public static void selsort(int[] arr)
{
int n=arr.length; //length of the array
for(int i=0;i<n-1 int min="i;" the first position as minimum system.out.println based on number smallest element by comparing with in for j="i+1;j<n;j++)" arr and if is greater than temp="arr[i];" public static void main args input array before sorting: arrays.>toString(arr));
<em>selsort</em>(arr);//calling the selection sort method
System.out.println("Elements in the array after Sorting: "+Arrays.<em>toString</em>(arr));
}
}</n-1>

Sample Output:

Selection Sort In Java

In the above program, we have two methods-main methods and the sell sort method. The main method calls the sell sort method passing the input array as the argument. The minimum element will be identified and swapped with the element pointed by MIN.

The selection sort can also be used where the input array is not defined in code. Let us see how it works using the below program.

Java Program to Sort the Elements using Selection Sort

Code:

import java.util.*;
public class SelectionSortExample
{
public static void main(String args[])
{
int n, i, j, tempvar;
Scanner <u>sc</u> = new Scanner(System.in); //To take the input from user
System.out.print("Enter the size of array : \n");
n = sc.nextInt();
int array[] = new int[n]; //<u>initialising</u> the array
System.out.print("Enter the elements that need to be inserted in the array : \n");
//inserting the elements to the array
for(i=0; i<n i array sc.nextint system.out.print before sorting: arrays.tostring begins here.. for j if> array[j])
{
tempvar = array[i];
array[i] = array[j];
array[j] = tempvar;
}
}
}
System.out.print("Array after Sorting is :\n");
for(i=0; i<n i system.out.print>
<p><strong>Sample Output:</strong></p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500309081990.png?x-oss-process=image/resize,p_40" class="lazy" alt="Selection Sort In Java" ></p>
<p>Here, the input elements given by the user will be compared with the temporary variable and swapped. The process will be repeated until a sorted array is formed.</p>
<h3 id="Performance-of-Selection-Sort">Performance of Selection Sort</h3>
<p>This sorting technique is used for its simplicity and certain other performance advantages over other more sorting techniques.</p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500309350697.png?x-oss-process=image/resize,p_40" class="lazy" alt="Selection Sort In Java" ></p>
<h3 id="Conclusion">Conclusion</h3>
<p>The selection sort does not work efficiently on large lists as it consumes more time for comparison. Selection sort is a method in which an input array will be divided into two subarrays in order to keep them sorted and unsorted elements. The minimum element in the array will be swapped with the element in the first position, and the process continues until a sorted array is formed.</p></n></n>

The above is the detailed content of Selection Sort In Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools