Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions src/main/java/algorithms/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,41 @@
import ui.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class BubbleSort extends AlgorithmSort {
public BubbleSort(ArrayList<Integer> array) {
super(array);
super(array);
}


@Override
public void sort() {
int SLEEP_DURATION = 5;

if (array.isEmpty()) {
System.out.println("Array is empty, nothing to sort.");
return;
}

int n = array.size();
Set<Integer> sortedIndices = new HashSet<>();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
// Visualize the comparison
Utils.displayVerticalArray(array, j, j + 1, sortedIndices);

if (array.get(j) > array.get(j + 1)) {
int temp = array.get(j);
array.set(j, array.get(j + 1));
array.set(j + 1, temp);
Utils.printArray(array);
// Swap and visualize the result of the selection for this iteration
Utils.swapHighlighted(array, j, j + 1, sortedIndices, SLEEP_DURATION);
}
}
// Mark the current position as sorted
sortedIndices.add(n - i - 1);
}
}

// Final visualization with all elements highlighted in green
Utils.displayVerticalArray(array, -1, -1, sortedIndices);
}
}
65 changes: 64 additions & 1 deletion src/main/java/algorithms/InsertionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ui.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class InsertionSort extends AlgorithmSort {

Expand All @@ -12,23 +14,84 @@ public InsertionSort(ArrayList<Integer> array) {

@Override
public void sort() {
int SLEEP_DURATION = 10;
Set<Integer> sortedIndices = new HashSet<>();

if (array.isEmpty()) {
System.out.println("Array is empty, nothing to sort.");
return;
}

int n = array.size();
for (int i = 1; i < n; i++) {
int key = array.get(i);
int j = i - 1;

// Highlight the initial key element before shifting
highlightInsertion(array, i, -1, -1, sortedIndices, SLEEP_DURATION);

// Shift elements to the right and highlight each shift
while (j >= 0 && array.get(j) > key) {
array.set(j + 1, array.get(j));
highlightInsertion(array, i, j, j + 1, sortedIndices, SLEEP_DURATION);
j = j - 1;
}

// Place the key in the correct position
array.set(j + 1, key);
highlightInsertion(array, -1, j + 1, j + 1, sortedIndices, SLEEP_DURATION);

// Mark the element as sorted
sortedIndices.add(i);
}

// Mark all elements as sorted for the final display
Utils.displayVerticalArray(array, -1, -1, sortedIndices);
}

static void highlightInsertion(ArrayList<Integer> arrayList, int keyIndex, int shiftIndex, int targetIndex, Set<Integer> sortedIndices, int sleepDuration) {
final String RESET = "\033[0m";
final String YELLOW = "\033[33m"; // Key element
final String RED = "\033[31m"; // Shifted elements
final String BLUE = "\033[34m"; // Target position
final String GREEN = "\033[32m"; // Sorted elements

Utils.printArray(array);
StringBuilder output = new StringBuilder();
int maxHeight = Utils.findMax(arrayList);

for (int row = maxHeight; row > 0; row--) {
output.append("\033[2K"); // Clear the whole line

for (int col = 0; col < arrayList.size(); col++) {
int element = arrayList.get(col);

if (element >= row) {
if (sortedIndices.contains(col)) {
output.append(GREEN).append("#").append(RESET);
} else if (col == keyIndex) {
output.append(YELLOW).append("#").append(RESET);
} else if (col == shiftIndex) {
output.append(RED).append("#").append(RESET);
} else if (col == targetIndex) {
output.append(BLUE).append("#").append(RESET);
} else {
output.append("#");
}
} else {
output.append(" ");
}
}
output.append("\n");
}

System.out.print(output.toString());
System.out.print("\033[" + maxHeight + "F");

try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

}
25 changes: 16 additions & 9 deletions src/main/java/algorithms/QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ui.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class QuickSort extends AlgorithmSort {

Expand All @@ -12,32 +14,37 @@ public QuickSort(ArrayList<Integer> array) {

@Override
public void sort() {
quickSort(0, array.size() - 1);
int SLEEP_DURATION = 30;

Set<Integer> sortedIndices = new HashSet<>();
quickSort(0, array.size() - 1, sortedIndices, SLEEP_DURATION);
Utils.displayVerticalArray(array, -1, -1, sortedIndices);
}

void quickSort(int start, int end) {
void quickSort(int start, int end, Set<Integer> sortedIndices, int sleepDuration) {
if (start < end) {
// pi is the partition return index of pivot
int pi = partition(start, end);
int pi = partition(start, end, sortedIndices, sleepDuration);

// Recursion calls for smaller elements
// and greater or equals elements
quickSort(start, pi - 1);
quickSort(pi + 1, end);
quickSort(start, pi - 1, sortedIndices, sleepDuration);
quickSort(pi + 1, end, sortedIndices, sleepDuration);
}
}

// Partition function
int partition(int start, int end) {
int partition(int start, int end, Set<Integer> sortedIndices, int sleepDuration) {
int pivot = array.get(end);

// Index of smaller element and indicates
// the right position of pivot found so far
int pIndex = start;

for (int i = start; i <= end - 1; i++) {
Utils.displayVerticalArray(array, i, end, sortedIndices);
if (array.get(i) <= pivot) {
Utils.swap(array, pIndex, end);
Utils.swapHighlighted(array, pIndex, i, sortedIndices, sleepDuration);
pIndex++;
}
}
Expand All @@ -48,9 +55,9 @@ int partition(int start, int end) {

// Move pivot after smaller elements and
// return its position
Utils.swap(array, pIndex, end);
Utils.swapHighlighted(array, pIndex, end, sortedIndices, sleepDuration); // Move pivot to correct position
sortedIndices.add(pIndex);
return pIndex;
}


}
25 changes: 21 additions & 4 deletions src/main/java/algorithms/SelectionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ui.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class SelectionSort extends AlgorithmSort {

Expand All @@ -12,29 +14,44 @@ public SelectionSort(ArrayList<Integer> array) {

@Override
public void sort() {
int SLEEP_DURATION = 25;

if (array.isEmpty()) {
System.out.println("Array is empty, nothing to sort.");
return;
}

int n = array.size();
Set<Integer> sortedIndices = new HashSet<>();

for (int i = 0; i < n - 1; i++) {
int currentMin = i;

for (int j = i + 1; j < n; j++) {
// Visualize the comparison
Utils.displayVerticalArray(array, i, j, sortedIndices);

if (array.get(j) < array.get(currentMin)) {
currentMin = j;
}
}

// Swap and visualize the result of the selection for this iteration
if (currentMin != i) {
int temp = array.get(i);
array.set(i, array.get(currentMin));
array.set(currentMin, temp);
Utils.swapHighlighted(array, i, currentMin, sortedIndices, SLEEP_DURATION);
}

Utils.printArray(array);
// Mark the current position as sorted
sortedIndices.add(i);
}

// Ensure all elements are marked as sorted for the final display
for (int i = 0; i < n; i++) {
sortedIndices.add(i);
}

// Final visualization with all elements highlighted in green
Utils.displayVerticalArray(array, -1, -1, sortedIndices);
}
}

24 changes: 17 additions & 7 deletions src/main/java/ui/TestUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ui;

import algorithms.BubbleSort;
import algorithms.InsertionSort;
import algorithms.QuickSort;
import algorithms.SelectionSort;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,17 +13,24 @@ public static void start() {

ArrayList<Integer> list = new ArrayList<>(List.of(
10, 17, 9, 19, 15, 7, 6, 4, 7, 6,
6, 20, 17, 6, 5, 8, 8, 5, 17, 18,
16, 17, 10, 1, 18, 4, 6, 20, 9, 14,
7, 4, 15, 7, 6, 2, 15, 15, 3, 1, 9,
2, 19, 8, 16, 14, 19, 5, 20, 9
6, 20, 17, 6, 5, 8, 8, 5, 17, 18,
16, 17, 10, 1, 18, 4, 6, 20, 9, 14,
7, 4, 15, 7, 6, 2, 15, 15, 3, 1, 9,
2, 19, 8, 16, 14, 19, 5, 20, 9
));
BubbleSort bubbleSort = new BubbleSort(list);
//bubbleSort.sort();

bubbleSort.sort();
SelectionSort selectionSort = new SelectionSort(list);
//selectionSort.sort();

Utils.printArray(list);
}

QuickSort quickSort = new QuickSort(list);
//quickSort.sort();

InsertionSort insertionSort = new InsertionSort(list);
insertionSort.sort();


}
}
Loading