# Selection Sort in One-Dimensional Arrays in Java

In this tutorial, we will explore the concept of selection sort in one-dimensional arrays using Java programming language. We will understand the working principle of selection sort, its syntax, and how to implement it in Java using BlueJ. Additionally, we will provide examples of selection sort on both numeric and string arrays, and also demonstrate the use of the Scanner class to input array elements. So, let’s dive into the world of selection sort!

## What is Selection Sort?

Selection sort is a simple comparison-based sorting algorithm that works by repeatedly finding the minimum element from the unsorted part of the array and swapping it with the element at the beginning of the sorted part. This process is repeated until the entire array is sorted.

Here’s how the selection sort algorithm works:

1. Start with the first element of the array as the current minimum.
2. Scan the remaining elements of the array to find the minimum element.
3. If a smaller element is found, update the current minimum.
4. After scanning the entire array, swap the minimum element with the first element of the unsorted part.
5. Move the boundary between the sorted and unsorted parts one position to the right.
6. Repeat steps 2 to 5 until the entire array is sorted.

## Syntax of Selection Sort in Java

The syntax of the selection sort algorithm can be summarized as follows:

for (int i = 0; i < array.length – 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

}}

## Implementing Selection Sort in Java

To implement the selection sort algorithm in Java, follow these steps:

1. Create a new Java class in BlueJ.
2. Declare a one-dimensional array to store the elements.
3. Use the Scanner class to input the elements into the array.
4. Implement the selection sort algorithm using the provided syntax.
5. Display the sorted array elements as output.

## Example of Selection Sort on Numeric Array

Let’s consider an example of selection sort on a numeric array. We will input an array of integers using the Scanner class and sort it using selection sort.

import java.util.Scanner;

public class SelectionSortExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter the size of the array: “);
int size = scanner.nextInt();
int[] array = new int[size];

System.out.println(“Enter the elements of the array:”);
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

// Selection Sort Algorithm
for (int i = 0; i < array.length – 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

System.out.println(“Sorted Array:”);
for (int element : array) {
System.out.print(element + ” “);
}
}

}

## Conclusion

In this tutorial, we have explored the concept of selection sort in one-dimensional arrays using Java. We have discussed the working principle, provided the syntax, and demonstrated the implementation of selection sort on numeric arrays. By using the Scanner class, we were able to input array elements dynamically. Selection sort is a fundamental sorting algorithm that helps organize data efficiently. Remember to experiment with different inputs and arrays to gain a deeper understanding of selection sort in Java.