K
K
kate2019-03-02 17:34:05
Java
kate, 2019-03-02 17:34:05

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

1 answer(s)
K
kate, 2019-03-03
@kkate4ka

thank you all, you did it!!!

if (count < 5) {
        	System.out.print ("Step " + (count + 1) + ": " + " ");
        	for (int n = 0; n < arr.length; n++) {
        		System.out.print (arr[n] + " ");// Выводим заполненый массив на экран          
        		}
        		count++;	
        		System.out.println();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question