Answer the question
In order to leave comments, you need to log in
How to display each sort step?
I have a selection sort and I need to show the first 3 steps.
How to write it? what I didn’t try, then an error or an infinite loop ..
Help, please.
import java.util.Scanner;
class Sorting {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Fill the array: ");
int arr[] = new int[6];//создаем массив размером 16
int i = 0;
for (i = 0; i < arr.length; i++) {
arr[i] = input.nextInt();// заполняем массив с клавиатуры
}
for (i = 0; i < arr.length - 1; i++) {
int least = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[least]) {
least = j;
}
}
int tmp = arr[i];
arr[i] = arr[least];
arr[least] = tmp;
}
System.out.println ("Result:");
for (i = 0; i < arr.length; i++) {
System.out.print (arr[i] + " ");// Выводим заполненый массив на экран
}
System.out.println();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question