Commit 791a92ea authored by edmundszagars's avatar edmundszagars
Browse files

Adding: "SelectionSort","InsertionSort","MergeSort","QuickSort" classes

parent eb15169c
1, 2, 3
2, 1, 3
1, 2, 3, 4
\ No newline at end of file
......@@ -7,9 +7,12 @@ import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import sorting_algorithms.BubbleSort;
import java.io.File;
import java.io.FileNotFoundException;
......@@ -17,15 +20,12 @@ import java.util.ArrayList;
import java.util.Arrays;
public class MainWindow extends Application {
private Stage window;
private Scene mainScene;
private FileChooser fileChooser = new FileChooser();
private String filePath;
ArrayList list;
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
public void start(Stage primaryStage) {
Label label = new Label("Select file location: ");
......@@ -70,12 +70,18 @@ public class MainWindow extends Application {
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(bubbleSortCheckBox.isSelected()){
double[] ar = dataFromCSV.getDoubleArrayFromListItem((ArrayList<Double>) list.get(0));
System.out.println(Arrays.toString(BubbleSort.bubbleSort(ar)));
System.out.println("Bouble sort will be executed");
}
double[] ar = dataFromCSV.getDoubleArrayFromListItem((ArrayList<Double>) list.get(0));
System.out.println(Arrays.toString(ar));
});
VBox layout = new VBox(15);
layout.maxHeight(50);
layout.getChildren().addAll(label,
fileLocation,
selectFileButton,
......@@ -86,9 +92,9 @@ public class MainWindow extends Application {
mergeSortCheckBox,
startButton);
mainScene = new Scene(layout, 700, 500);
Scene mainScene = new Scene(layout, 700, 500);
window.setScene(mainScene);
window.show();
primaryStage.setScene(mainScene);
primaryStage.show();
}
}
package sorting_algorithms;
public class InsertionSort {
public static double[] insertionSort(double[] arr){
double temp;
for(int i = 0; i < arr.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;
}
}
}
return arr;
}
}
package sorting_algorithms;
public class MergeSort {
private double[] array;
private double[] tempMergArr;
public void sort(double inputArr[]) {
this.array = inputArr;
int length = inputArr.length;
this.tempMergArr = new double[length];
doMergeSort(0, length - 1);
}
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
doMergeSort(lowerIndex, middle);
doMergeSort(middle + 1, higherIndex);
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
}
package sorting_algorithms;
public class QuickSort {
private double array[];
public void sort(double[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
int length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
double pivot = array[lowerIndex + (higherIndex - lowerIndex) / 2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
double temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
package sorting_algorithms;
public class SelectionSort {
public static double[] selectionSort(double[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[index]) {
index = j;
}
}
double smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
}
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