Java
Java

 

 

Program Structure

The Selection Sort algorithm sorts an array by repeatedly finding the minimum element from the unsorted part and moving it to the beginning. The process continues until the array is sorted.

Java Code


/**
 * This class implements the Selection Sort algorithm.
 */
public class SelectionSort {

    /**
     * The method to perform selection sort on an array.
     *
     * @param arr The array to be sorted.
     */
    public static void selectionSort(int[] arr) {
        int n = arr.length;

        // Traverse through all array elements
        for (int i = 0; i < n - 1; i++) {
            // Find the minimum element in the unsorted array
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }

            // Swap the found minimum element with the first element
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }

    /**
     * A utility method to print the contents of an array.
     *
     * @param arr The array to print.
     */
    public static void printArray(int[] arr) {
        for (int value : arr) {
            System.out.print(value + " ");
        }
        System.out.println();
    }

    /**
     * Main method to test the selection sort algorithm.
     *
     * @param args Command line arguments.
     */
    public static void main(String[] args) {
        int[] arr = {64, 25, 12, 22, 11};
        System.out.println("Original array:");
        printArray(arr);

        selectionSort(arr);

        System.out.println("Sorted array:");
        printArray(arr);
    }
}

Explanation of the Code

The program consists of a class named SelectionSort which contains the following methods:

  • selectionSort(int[] arr): This method implements the selection sort algorithm. It iterates through the array, finds the minimum element in the unsorted portion, and swaps it with the first unsorted element.
  • printArray(int[] arr): A utility method that prints the elements of the array.
  • main(String[] args): The entry point of the program, which initializes an array, calls the sorting method, and prints the original and sorted arrays.

How to Run the Program

To run this program, you need to have Java installed on your machine. Save the code in a file named SelectionSort.java, and then follow these commands in your terminal:


javac SelectionSort.java
java SelectionSort

Conclusion

Selection Sort is a simple sorting algorithm that is inefficient on large lists. However, it is easy to understand and implement, making it suitable for educational purposes.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)