Question: Need help fixing this code (This is based on the solution here in Chegg but there's a mistake with mine): Write an animation for selection

Need help fixing this code (This is based on the solution here in Chegg but there's a mistake with mine):

Write an animation for selection sort, insertion sort, and bubble sort, as shown below. Create an array of integers 1, 2, .50. Shuffle it randomly. Create a pane to display the array in a histogram. You should invoke each sort method in a separate thread. Each algorithm uses two nested loops. When the algorithm completes an iteration in the outer loop, put the thread to sleep for 0.5 seconds, and redisplay the array in the histogram. Color the last bar in the sorted subarray.

Need help fixing this code (This is based on the solution here

CODE (help me with the errors please):

import javafx.application.Application;

import javafx.application.Platform;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Label;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Pane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.stage.Stage;

public class SortingAnimation extends Application

{

private Pane_Histogram ArrayPane1_display = new

Pane_Histogram();

private Pane_Histogram ArrayPane2_display = new

Pane_Histogram();

private Pane_Histogram ArrayPane3_display = new

Pane_Histogram();

private int[] nmbr1 = initialize_nmbr();

private int[] nmbr2 = nmbr1.clone();

private int[] nmbr3 = nmbr1.clone();

public final static int PAUSE_TIME = 500;

public final static int ARRAY_SIZE = 50;

@Override

public void start(Stage primaryStage)

{

ArrayPane1_display.setnmbr(nmbr1);

ArrayPane2_display.setnmbr(nmbr2);

ArrayPane3_display.setnmbr(nmbr3);

BorderPane p1 = new BorderPane();

BorderPane p2 = new BorderPane();

BorderPane p3 = new BorderPane();

p1.setCenter(ArrayPane1_display);

Label lbl1 = new Label("selection Sort");

p1.setTop(lbl1);

BorderPane.setAlignment(lbl1, Pos.CENTER);

p2.setCenter(ArrayPane2_display);

Label lbl2 = new Label("Insertion Sort");

p2.setTop(lbl2);

BorderPane.setAlignment(lbl2, Pos.CENTER);

p3.setCenter(ArrayPane3_display);

Label lbl3 = new Label("Bubble Sort");

p3.setTop(lbl3);

BorderPane.setAlignment(lbl3, Pos.CENTER);

HBox hBox = new HBox(15);

hBox.setAlignment(Pos.CENTER);

hBox.getChildren().addAll(p1, p2, p3);

Scene scene = new Scene(hBox, 980, 350);

primary_Stage.setTitle("Exercise30_19");

primary_Stage.setScene(scene);

primary_Stage.show();

new Thread(new Runnable()

{

public void run()

{

selectionSort(nmbr1);

}

}).start();//start()method to

new Thread(new Runnable()

{

public void run()

{

insertionSort(nmbr2);

}

}).start();

new Thread(new Runnable()

{

public void run()

{

bubbleSort(nmbr3);

}

}).start();

}

int sk = 0;

public void selectionSort(int[]list)

{

for(sk = 0; sk

{

int current_Min = list[sk];

int current_MinIndex = sk;

for (int j = sk + 1; j

{

if (current_Min > list [j])

{

current_Min = list [j];

current_MinIndex = j;

}

}

if (current_MinIndex != sk)

{

list[current_MinIndex] = list[sk];

list[sk] = current_Min;

}

try

{

Thread.sleep(PAUSE_TIME);

}

catch (InterruptedException e) {}

Platform.runLater(() ->

ArrayPane1_display.setcolored_BarIndex(sk));

}

}

int i1;

public void insertionSort(int[] list)

{

for (i1 = 1; i1

{

int currentElement = list[i1];

int k;

for (k = i1 - 1; k>=0

&& list[k] > currentElement; k--)

{

list[k + 1] = list[k];

}

list[k+1] = currentElement;

try

{

Thread.sleep(PAUSE_TIME);

}

catch (InterruptedException e ) {}

Platform.runLater(() ->

ArrayPane2_display.setcolored_BarIndex(i1));

}

}

int idx;

public void bubbleSort(int[] list)

{

boolean needNextPass = true;

for (idx = 1; idx

{

needNextPass = false;

for(int i = 0; i

{

if (list[i] > list[i+1]) {

int temp = list[i];

list[i] = list[i+1];

list[i+1] = temp;

needNextPass = true;

}

}

try

{

Thread.sleep(PAUSE_TIME);

}

catch (InterruptedException e ) {}

Platform.runLater(() ->

ArrayPane3_display.setcolored_BarIndex

(list.length - idx));

}

}

public int[] initialize_nmbr()

{

int[] nmbr = new int[ARRAY_SIZE];

for (int i = 0; i

{

nmbr [i] = i + 1;

}

for (int i = 0; i

{

int index = (int)(Math.Random()

* ARRAY_SIZE);

int temp = nmbr[i];

nmbr[i] = nmbr[index];

nmbr[index] = temp;

}

return nmbr;

}

class Pane_Histogram extends Pane {

private int[] nmbr;

private int colored_BarIndex;

public void setnmbr(int[]nmbr)

{

this.nmbr=nmbr;

repaint();

public void setcolored_BarIndex(int index)

{

colored_BarIndex = index;

repaint();

}

protected void repaint()

{

getChildren().clear()

int max = nmbr[0];

for (int i = 1; i

{

if (max

{

max = nmbr[i];

}

}

double h = 200;

double w = 200;

double height = h* 0.98;

double width = w;

double uWidth = width / nmbr.length;

for (int i = 0; i

{

Rectangle rect = new Rectangle(

i* uWidth, h - (nmbr[i]

* 1.0 / max) * height,

uWidth, (nmbr[i] * 1.0 / max)

* height);

rect.setFill(Color.WHITE);

rect.setStroke(Color.Black);

getChildren().add(rect);

)

if (colored_BarIndex

&& colored_BarIndex > 0)

{

int i = colored_BarIndex;

Rectangle rect = new Rectangle(

i* uWidth, h - (nmbr[i]

* 1.0 / max)*height,

uWidth, (nmbr[i] * 1.0/max)

*height);

getChildren().add(rect);

}

}

}

public static void main (String[]args)

{

launch(args);

}

Sorting Animation Selection Sort Bubble Sort

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!