Visualization of state

Report a typo

This time your team decided to make a prototype of an algorithm visualization application. For starters, your teammates created a class called SelectionSort to run a selection sort algorithm and a class called AlgorithmVisualizer to control the algorithm execution step by step both forward and backward. All visualization logic is finalized but one required class and some methods in SelectionSort have not been implemented yet, and this will be your task.

Look carefully at the SelectionSort class and decide which fields represent its state, then implement the SelectionSort.SortState class to store snapshots of the SelectionSort state, and after that implement the getState and setState methods in SelectionSort to actually save and restore its state. That's it!

Write a program in Java 17
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

interface Algorithm<S extends AlgorithmState> {

boolean hasNextStep();

void nextStep();

S getState();

void setState(S state);
}

interface AlgorithmState { }

class SelectionSort<T extends Comparable<T>> implements Algorithm<SelectionSort.SortState<T>> {
private T[] array;
private int currentIndex = 0;
private int comparedIndex = 0;
private int currentMinIndex = 0;

SelectionSort(T[] array) {
this.array = array.clone();
}

@Override
public void nextStep() {
if (comparedIndex == array.length - 1 && currentIndex != currentMinIndex) {
T tmp = array[currentIndex];
array[currentIndex] = array[currentMinIndex];
array[currentMinIndex] = tmp;
currentMinIndex = currentIndex;
} else if (comparedIndex == array.length - 1) {
currentIndex++;
___

Create a free account to access the full topic