Commit f9c5bb2d authored by edmundszagars's avatar edmundszagars
Browse files

Implementing Sortable interface in all sorting implementation classes

parent 12fbd8a4
......@@ -3,6 +3,8 @@ package gui;
import helper.GetDataFromCSV;
import helper.Result;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
......@@ -47,6 +49,10 @@ public class MainWindow extends Application {
CheckBox bubbleSortCheckBox = new CheckBox();
bubbleSortCheckBox.setText("Bubble sort");
bubbleSortCheckBox.setSelected(true);
bubbleSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if(bubbleSortCheckBox.isSelected())
System.out.println("Bubble selected");
});
CheckBox selectionSortCheckBox = new CheckBox();
selectionSortCheckBox.setText("Selection sort");
......@@ -95,13 +101,13 @@ public class MainWindow extends Application {
BubbleSort bs = new BubbleSort();
double[] ar = dataFromCSV.getDoubleArrayFromListItem((ArrayList) list.get(0));
anResult.setStartDataString(ar);
double[] resultArray = bs.bubbleSort(ar);
double[] resultArray = bs.sort(ar);
long tree = bs.getExecutionStart();
long end = bs.getExecutionEnd();
long tree = bs.getExecutionStartTime();
long end = bs.getExecutionEndTime();
anResult.setResultDataString(resultArray);
long free = bs.getExecutionTime();
long free = bs.getExecutionEndTime();
anResult.setExecutionResultTime(free);
long tt = anResult.getExecutionResultTime();
table.getItems().add(anResult);
......
......@@ -2,12 +2,22 @@ package sorting_algorithms;
import helper.*;
public class BubbleSort implements ExecutionTime {
private long executionStart;
private long executionEnd;
public class BubbleSort implements Sortable {
public double[] bubbleSort(double[] array) {
executionStart = System.currentTimeMillis();
private long executionStartTime;
private long executionEndTime;
private static void swapNumbers(int i, int k, double[] array) {
double tmp;
tmp = array[i];
array[i] = array[k];
array[k] = tmp;
}
@Override
public double[] sort(double[] array) {
setExecutionStartTime(System.currentTimeMillis());
int n = array.length;
int k;
for (int m = n; m >= 0; m--) {
......@@ -18,35 +28,27 @@ public class BubbleSort implements ExecutionTime {
}
}
}
setExecutionEnd(System.currentTimeMillis() - executionStart);
setExecutionEndTime(System.currentTimeMillis() - getExecutionStartTime());
return array;
}
private static void swapNumbers(int i, int k, double[] array) {
double tmp;
tmp = array[i];
array[i] = array[k];
array[k] = tmp;
}
public long getExecutionStart() {
return this.executionStart;
}
private void setExecutionStart(long executionStart) {
this.executionStart = executionStart;
@Override
public void setExecutionEndTime(long executionEndTime) {
this.executionEndTime = executionEndTime;
}
public long getExecutionEnd() {
return this.executionEnd;
@Override
public void setExecutionStartTime(long executionStartTime) {
this.executionStartTime = executionStartTime;
}
private void setExecutionEnd(long executionEnd) {
this.executionEnd = executionEnd;
@Override
public long getExecutionStartTime() {
return executionStartTime;
}
@Override
public long getExecutionTime() {
return executionEnd;
public long getExecutionEndTime() {
return executionEndTime;
}
}
package sorting_algorithms;
public class InsertionSort {
public class InsertionSort implements Sortable {
private long executionStartTime;
private long executionEndTime;
public static double[] insertionSort(double[] arr){
@Override
public double[] sort(double[] array) {
double temp;
for(int i = 0; i < arr.length;i++){
setExecutionStartTime(System.currentTimeMillis());
for(int i = 0; i < array.length;i++){
for (int j = i; j>0;j--){
if(arr[j] < arr[j-1]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
if(array[j] < array[j-1]){
temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
}
return arr;
setExecutionEndTime(System.currentTimeMillis() - getExecutionStartTime());
return array;
}
@Override
public void setExecutionEndTime(long executionEndTime) {
this.executionEndTime = executionEndTime;
}
@Override
public void setExecutionStartTime(long executionStartTime) {
this.executionStartTime = executionStartTime;
}
@Override
public long getExecutionStartTime() {
return executionStartTime;
}
@Override
public long getExecutionEndTime() {
return executionEndTime;
}
}
package sorting_algorithms;
public class MergeSort {
public class MergeSort implements Sortable{
private double[] array;
private double[] tempMergArr;
public void sort(double inputArr[]) {
this.array = inputArr;
int length = inputArr.length;
private long executionStartTime;
private long executionEndTime;
@Override
public double[] sort(double[] array) {
setExecutionStartTime(System.currentTimeMillis());
this.array = array;
int length = array.length;
this.tempMergArr = new double[length];
doMergeSort(0, length - 1);
setExecutionEndTime(System.currentTimeMillis() - getExecutionStartTime());
return array;
}
private void doMergeSort(int lowerIndex, int higherIndex) {
@Override
public void setExecutionEndTime(long executionEndTime) {
this.executionEndTime = executionEndTime;
}
@Override
public void setExecutionStartTime(long executionStartTime) {
this.executionStartTime = executionStartTime;
}
@Override
public long getExecutionStartTime() {
return executionStartTime;
}
@Override
public long getExecutionEndTime() {
return executionEndTime;
}
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
doMergeSort(lowerIndex, middle);
......
package sorting_algorithms;
public class QuickSort {
public class QuickSort implements Sortable{
private double array[];
public void sort(double[] inputArr) {
private long executionStartTime;
private long executionEndTime;
if (inputArr == null || inputArr.length == 0) {
return;
@Override
public double[] sort(double[] array) {
setExecutionStartTime(System.currentTimeMillis());
if (array == null || array.length == 0) {
return null;
}
this.array = inputArr;
int length = inputArr.length;
this.array = array;
int length = array.length;
quickSort(0, length - 1);
setExecutionEndTime(System.currentTimeMillis() - getExecutionStartTime());
return array;
}
private void quickSort(int lowerIndex, int higherIndex) {
......@@ -43,4 +49,24 @@ public class QuickSort {
array[i] = array[j];
array[j] = temp;
}
@Override
public void setExecutionEndTime(long executionEndTime) {
this.executionEndTime = executionEndTime;
}
@Override
public void setExecutionStartTime(long executionStartTime) {
this.executionStartTime = executionStartTime;
}
@Override
public long getExecutionStartTime() {
return executionStartTime;
}
@Override
public long getExecutionEndTime() {
return executionEndTime;
}
}
package sorting_algorithms;
public class SelectionSort {
public class SelectionSort implements Sortable{
private long executionStartTime;
private long executionEndTime;
public static double[] selectionSort(double[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
@Override
public double[] sort(double[] array) {
setExecutionStartTime(System.currentTimeMillis());
for (int i = 0; i < array.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[index]) {
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[index]) {
index = j;
}
}
double smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
double smallerNumber = array[index];
array[index] = array[i];
array[i] = smallerNumber;
}
return arr;
setExecutionEndTime(System.currentTimeMillis() - getExecutionStartTime());
return array;
}
@Override
public void setExecutionEndTime(long executionEndTime) {
this.executionEndTime = executionEndTime;
}
@Override
public void setExecutionStartTime(long executionStartTime) {
this.executionStartTime = executionStartTime;
}
@Override
public long getExecutionStartTime() {
return executionStartTime;
}
@Override
public long getExecutionEndTime() {
return executionEndTime;
}
}
package sorting_algorithms;
public interface Sortable {
double[] sort(double[] array);
void setExecutionEndTime(long endTime);
void setExecutionStartTime(long startTime);
long getExecutionStartTime();
long getExecutionEndTime();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment