Java program to print the selection sort using array elements

Java program to print the selection sort using array elements. In Java programming, there is a selection sort program. This selection sort program is a very advanced sorting program that is also very relevant. It will choose the item and sort the elements.

public class Main
{ 
public static void sorting(int[] arr)
{ 
for (int i = 0; i < arr.length - 1; i++) 
{ 
int index = i; 
for (int j = i + 1; j < arr.length; j++)
{ 
if (arr[j] < arr[index])
{ 
index = j;
} 
} 
int small = arr[index]; 
arr[index] = arr[i]; 
arr[i] = small; 
} 
} 
public static void main(String a[])
{ 
int[] arr1 = {45,56,67,1,34,46,67}; 
System.out.println("Before Selection sort"); 
for(int i:arr1){ 
System.out.print(i+" "); 
} 
System.out.println(); 
sorting(arr1);
System.out.println("After Selection sort"); 
for(int i:arr1){ 
System.out.print(i+" "); 
} 
} 
} 

 

Output:

Before Selection sort 45 56 67 1 34 46 67 After Selection sort 1 34 45 46 56 67 67

What is selection sort algorithm in java

A simple sorting algorithm is selection sort. The unsorted array’s smallest element is chosen and swapped with the leftmost element, resulting in that element becoming a part of the sorted array. The unsorted array border is moved from one element to the right in this operation.