Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In this tutorial, we will demonstrate how to implement the Selection Sort algorithm in Java using arrays. Selection Sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted part and moving it to the beginning of the sorted part.
public class SelectionSort { public static void main(String[] args) { int[] arr = {5, 3, 1, 4, 2}; selectionSort(arr); // Print the sorted array for (int num : arr) { System.out.print(num + " "); } } public static void selectionSort(int[] array) { int n = array.length; // One by one move boundary of unsorted subarray for (int i = 0; i < n - 1; i++) { // Find the minimum element in unsorted array int minIdx = i; for (int j = i + 1; j < n; j++) { if (array[j] < array[minIdx]) { minIdx = j; } } // Swap the found minimum element with the first element if (minIdx != i) { int temp = array[i]; array[i] = array[minIdx]; array[minIdx] = temp; } } } }
When you run this code, it will print the sorted array:
1 2 3 4 5
In this example, we defined a selectionSort
method that takes an array as input and sorts it using the Selection Sort algorithm. The outer loop iterates through the array, and the inner loop iterates from the current index of the outer loop to the end of the array to find the minimum element.
Once the minimum element is found, it is swapped with the current element of the outer loop. This process continues until the entire array is sorted.
Selection Sort has a worst-case and average-case time complexity of O(n^2), where n is the number of items being sorted. It is not suitable for large datasets, and more efficient sorting algorithms like QuickSort or MergeSort should be used in those cases.
How to Implement Selection Sort on Arrays in Java: Here's an implementation of selection sort on arrays in Java:
public class SelectionSort { public static void main(String[] args) { int[] array = {5, 2, 9, 1, 5, 6}; selectionSort(array); for (int num : array) { System.out.print(num + " "); } } public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // Swap arr[i] and arr[minIndex] int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } }
Java Selection Sort for Strings: Selection sort can be applied to arrays of strings by comparing string elements.
public static void selectionSortStrings(String[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j].compareTo(arr[minIndex]) < 0) { minIndex = j; } } // Swap arr[i] and arr[minIndex] String temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } }
Selection Sort with Java ArrayList:
Selection sort can be applied to ArrayList
using the set
method.
public static void selectionSortArrayList(ArrayList<Integer> list) { int n = list.size(); for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (list.get(j) < list.get(minIndex)) { minIndex = j; } } // Swap list.get(i) and list.get(minIndex) int temp = list.get(i); list.set(i, list.get(minIndex)); list.set(minIndex, temp); } }
Java Selection Sort Descending Order: To sort in descending order, adjust the comparison conditions.
if (arr[j] > arr[minIndex]) { // Swap for descending order int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; }
Selection Sort Visualization in Java: Visualization can be done by printing the array at each step.
public static void visualizeSelectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // Swap arr[i] and arr[minIndex] int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; // Print the array for visualization printArray(arr); } } public static void printArray(int[] arr) { for (int num : arr) { System.out.print(num + " "); } System.out.println(); }
Java Selection Sort Recursive Implementation: Selection sort is typically implemented iteratively. Recursive implementation is less common due to its inefficiency.
public static void recursiveSelectionSort(int[] arr, int n) { if (n <= 1) { return; } int maxIndex = 0; for (int i = 1; i < n; i++) { if (arr[i] > arr[maxIndex]) { maxIndex = i; } } // Swap arr[n-1] and arr[maxIndex] int temp = arr[n - 1]; arr[n - 1] = arr[maxIndex]; arr[maxIndex] = temp; // Recur for the remaining array recursiveSelectionSort(arr, n - 1); }