BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program for Selection Sorting

Last Updated: April 15, 2019 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a Java program for selection sorting.

How Selection Sort algorithm works?

Selection sort algorithm works by dividing the original array in two subarrays: Sorted subarray and unsorted subarray, initially the sorted subarray is empty. This algorithm works by repeatedly finding the minimum element from the unsorted subarray and replacing it with the first element of the array, thus making that part of the array as sorted subarray. This happens repeatedly until the whole array is sorted.

Java Program to perform Selection Sort on Array

In the following example, we have defined a method selectionSort() that implements the selection sort algorithm. It finds the minimum element from the array and swaps it with the first element of the array.

We have created another method printArr() to display the elements of the array. We have called this method before and after sorting to display the array elements before and after selection sorting.

class JavaExample
{
    void selectionSort(int arr[])
    {
        int len = arr.length;
 
        for (int i = 0; i < len-1; i++)
        {
            // Finding the minimum element in the unsorted part of array
            int min = i;
            for (int j = i+1; j < len; j++)
                if (arr[j] < arr[min])
                    min = j;
 
            /* Swapping the found minimum element with the first
             * element of the sorted subarray using temp variable
             */
            int temp = arr[min];
            arr[min] = arr[i];
            arr[i] = temp;
        }
    }
 
    // Displays the array elements
    void printArr(int arr[])
    {
        for (int i=0; i<arr.length; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
 
    public static void main(String args[])
    {
        JavaExample obj = new JavaExample();
        int numarr[] = {101,5,18,11,80, 67};
        
        System.out.print("Original array: ");
        obj.printArr(numarr);
        
        //calling method for selection sorting
        obj.selectionSort(numarr);
        
        System.out.print("Sorted array: ");
        obj.printArr(numarr);
    }
}

Output:
Java Selection sort program with example

Related Java Examples

1. Java Program to perform bubble sort on Strings
2. Java program to sort an array in ascending order
3. Java program for bubble sort in ascending and descending order
4. Java program for binary search
5. Java Program for linear search

Top Related Articles:

  1. Java program to print number of elements in an array
  2. Java Program to Calculate average using Array
  3. Java Program to Print Hollow Square Star Pattern
  4. Java Program to print the duplicate elements of an array
  5. Java program to convert decimal to binary

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Asish Das says

    December 15, 2019 at 3:32 PM

    Please upload Quick Sort & Merge Sort program in Java

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap