Selection Sort(Ascending) in Java

Selection sort in ascending order is a sorting algorithm that repeatedly selects the minimum element from the unsorted portion of the array and places it at the beginning of the sorted portion. This process continues until the entire array is sorted in ascending order, with the smallest element at the beginning and the largest at the end.

JAVA PROGRAMMING

Raki Adhikary

1/12/20231 min read

import java.util.*;

public class SelectionSortAscending {

public static void main(String args[]) {

int size;

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the size of the array:");

size = scanner.nextInt();

int array[] = new int[size];

int i, j, minIndex, temp;

/* Taking input in the array */

for (i = 0; i < size; i++) {

System.out.println("Enter element at index " + i + ":");

array[i] = scanner.nextInt();

}

/* Selection Sorting the array in ascending order */

for (i = 0; i < size - 1; i++) {

minIndex = i;

for (j = i + 1; j < size; j++) {

if (array[j] < array[minIndex]) {

// Finding the minimum element in the unsorted part of the array

minIndex = j;

}

}

// Swapping the minimum element with the current element

temp = array[minIndex];

array[minIndex] = array[i];

array[i] = temp;

}

/* Displaying the sorted Array */

System.out.println("The sorted Array in ascending order is:");

for (i = 0; i < size; i++)

System.out.print(array[i] + " ");

scanner.close();

}

}

Your Code is here