Selection Sort Program in Java

Selection sort is an in-place comparison sorting algorithm. It has an O(n²) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort.

import java.util.*;

public class SelectionSort
{
    public static void main(String[] args)
    {
        int ar[] = new int[10];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 10; i++)
        {
            System.out.print("Enter the number ar[" + i + "]:");
            ar[i] = sc.nextInt();
        }
        System.out.println("The numbers are:");
        int n = ar.length; 
  
        for (int i = 0; i < n-1; i++) 
        { 
            int min = i; 
            for (int j = i+1; j < n; j++) 
            {
                if (ar[j] < ar[min]) 
                {
                    min = j;
                } 
            }
            int temp = ar[min]; 
            ar[min] = ar[i]; 
            ar[i] = temp; 
        } 
        
        System.out.println("Sorted order:");
        for (int i = 0; i < 10; i++)
        {
            System.out.println(ar[i]);
        }
    }
}


Output:

Enter the number ar[0]:12
Enter the number ar[1]:54
Enter the number ar[2]:21
Enter the number ar[3]:36
Enter the number ar[4]:25
Enter the number ar[5]:14
Enter the number ar[6]:47
Enter the number ar[7]:58
Enter the number ar[8]:69
Enter the number ar[9]:85
Sorted order:
12
14
21
25
36
47
54
58
69
85
learn java study java