Embed presentation
Download to read offline
![(JAVA NetBeans) Write a Java program able to perform selection sort.
Solution
import java.util.Scanner;
/**
* @author
*
*/
public class SelectionSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = setArray();
System.out.println(" Before Sorting:");
printArray(arr);
arr = selectionSort(arr);
System.out.println(" After Sorting:");
printArray(arr);
}
/**
* reads and stores the data in the one-dimensional array
*/
/**
* @return
*/
public static int[] setArray() {
int arr[] = new int[12];
System.out.println("Enter 12 integers.");](https://image.slidesharecdn.com/javanetbeanswriteajavaprogramabletoperformselectionsort-so-230207033424-3119929c/75/JAVA-NetBeans-Write-a-Java-program-able-to-perform-selection-sort-So-docx-1-2048.jpg)
![Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
return arr;
}
/**
* prints the data in the one-dimensional array
*/
public static void printArray(int[] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
/**
* method will sort the array in ascending order
*/
public static int[] selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
}
OUTPUT:](https://image.slidesharecdn.com/javanetbeanswriteajavaprogramabletoperformselectionsort-so-230207033424-3119929c/85/JAVA-NetBeans-Write-a-Java-program-able-to-perform-selection-sort-So-docx-2-320.jpg)

The document outlines a Java program that implements the selection sort algorithm. It includes methods to read an array of integers, print the array before and after sorting, and the core selection sort logic. The program prompts the user to input 12 integers, displays the array before and after the sorting process.
![(JAVA NetBeans) Write a Java program able to perform selection sort.
Solution
import java.util.Scanner;
/**
* @author
*
*/
public class SelectionSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = setArray();
System.out.println(" Before Sorting:");
printArray(arr);
arr = selectionSort(arr);
System.out.println(" After Sorting:");
printArray(arr);
}
/**
* reads and stores the data in the one-dimensional array
*/
/**
* @return
*/
public static int[] setArray() {
int arr[] = new int[12];
System.out.println("Enter 12 integers.");](https://image.slidesharecdn.com/javanetbeanswriteajavaprogramabletoperformselectionsort-so-230207033424-3119929c/75/JAVA-NetBeans-Write-a-Java-program-able-to-perform-selection-sort-So-docx-1-2048.jpg)
![Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
return arr;
}
/**
* prints the data in the one-dimensional array
*/
public static void printArray(int[] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
/**
* method will sort the array in ascending order
*/
public static int[] selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
}
OUTPUT:](https://image.slidesharecdn.com/javanetbeanswriteajavaprogramabletoperformselectionsort-so-230207033424-3119929c/85/JAVA-NetBeans-Write-a-Java-program-able-to-perform-selection-sort-So-docx-2-320.jpg)
