Java Program: Selection Sort
This program sorts an array of integers using the Selection Sort algorithm. Selection Sort divides the input list into two parts: a sorted and an unsorted part. It repeatedly selects the smallest (or largest) element from the unsorted part and moves it to the sorted part.
import java.util.Scanner;
public class SelectionSort {
// Method to sort an array using Selection Sort
public static void selectionSort(int[] array) {
int n = array.length;
// Traverse through all array elements
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted part
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
if (minIndex != i) {
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
}
// Main method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: User enters the number of elements
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] array = new int[n];
// Input: User enters the elements of the array
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
// Sort the array using Selection Sort
selectionSort(array);
// Output: Display the sorted array
System.out.println("Sorted array:");
for (int i : array) {
System.out.print(i + " ");
}
}
}
Code Breakdown
1. Import Statement
- Line:
import java.util.Scanner;
- This imports the
Scanner
class to read user input.
2. Class Declaration
- Line:
public class SelectionSort {
- This declares a public class named
SelectionSort
.
3. Selection Sort Method
- Line:
public static void selectionSort(int[] array) {
- This method sorts an integer array using the Selection Sort algorithm.
4. Traverse the Array
for (int i = 0; i < n - 1; i++) {
- This loop iterates through the array elements.
5. Find Minimum Element
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
- This nested loop finds the index of the minimum element in the unsorted part of the array.
6. Swap Elements
if (minIndex != i) {
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
- If the minimum element is not the first element of the unsorted part, swap them.
Main Method
7. Create Scanner Object
- Line:
Scanner scanner = new Scanner(System.in);
- Creates a
Scanner
object for reading user input.
8. User Input for Number of Elements
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] array = new int[n];
- Prompts the user for the number of elements and initializes the array.
9. User Input for Array Elements
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
}
- Reads the elements of the array from user input.
10. Sort the Array
- Line:
selectionSort(array);
- Calls the
selectionSort
method to sort the array.
11. Display the Result
System.out.println("Sorted array:");
for (int i : array) {
System.out.print(i + " ");
}
- Displays the sorted array to the user.
Conclusion
This program effectively sorts an array of integers using the Selection Sort algorithm. It demonstrates fundamental programming concepts such as methods, loops, and user input. If you have any questions or need clarification on any part, feel free to ask!
0 Comments