Question: Would you please show me easy and simple code for java? Selection sort is a sorting algorithm that treats the input as two parts, a

Would you please show me easy and simple code for java?

Selection sort is a sorting algorithm that treats the input as two parts, a sorted part and an unsorted part, and repeatedly selects the proper next value to move from the unsorted part to the end of the sorted part.

The array is divided into two parts, the sorted part being at the left end and the unsorted part at the right end. The sorted part is initially empty and the full array is initially the unsorted part. The smallest element from the unsorted part is chosen and swapped with the furthest left element in the unsorted part, essentially making that smallest element part of the sorted array, as the 'boundary' between the sorted and unsorted parts is incremented by one each time this swap is performed.

A visual aid for selection sort can be seen here.

public class Sorting { public static void main(String[] args) { // Example input. int numbers[] = {10, 2, 78, 4, 45, 32, 7, 11}; int i;

int numbersSize = numbers.length; // Print array pre sorting. System.out.print("UNSORTED: "); for (i = 0; i < numbersSize; i++) { System.out.print(numbers[i] + " "); } System.out.println();

// Run Selection Sort. selectionSort(numbers);

// Print array post sorting. System.out.print("SORTED: "); for (i = 0; i < numbersSize; i++) { System.out.print(numbers[i] + " "); } System.out.println(); }

public static void selectionSort(int numbers[]) { // FIXME } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!