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
- Set the minimum (MIN) pointer to location 0.
- Find the smallest element from the list of elements in the array
- Swap the minimum element with the location 0
- Move the MIN pointer to the next position
- 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.
Step 1: Set the MIN pointer to the first location. So, the MIN pointer points to 15.
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.
Smallest: = 15
Comparing 15 and 6, 6 is the smallest.
Smallest: = 6
Comparing 6 and 3, 3 is the smallest.
Smallest: = 3
3 will be smaller in this case also, as 19 is greater than 3.
Smallest: = 3
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.
Step 4: Increment the MIN pointer to the next position.
Step 5: Find the next smallest element by comparing it with the rest of the elements.
Smallest: = 21
Smallest: = 6
Smallest: = 6
Smallest: = 6
Smallest: = 6
Step 6: Swap the smallest element with the element in location 1.
Repeat the process until a sorted array is formed, as shown below.
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:
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!

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools