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
13 changes: 5 additions & 8 deletions src/main/java/algorithms/InsertionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public void sort() {
int n = array.size();

for (int i = 1; i < n; i++) {
int sleepDuration = 40 ; // Dynamic delay for shifts and comparisons
int highlightSortedElementDelay = 20; // Delay specifically for highlighting sorted elements
int sleepDuration = 20 ; // Dynamic delay for shifts and comparisons
int highlightSortedElementDelay = 40; // Delay specifically for highlighting sorted elements


int key = array.get(i);
Expand Down Expand Up @@ -68,7 +68,6 @@ public void sort() {
private static void highlightElement(ArrayList<Integer> array, int index, String color, Set<Integer> sortedIndices, int sleepDuration) {
final String RESET = "\033[0m";
final String GREEN = "\033[32m";
final String SQUARE = "◼";
StringBuilder output = new StringBuilder();

int maxHeight = Utils.findMax(array);
Expand All @@ -78,12 +77,10 @@ private static void highlightElement(ArrayList<Integer> array, int index, String
int element = array.get(col);
if (element >= row) {
if (col == index) {
output.append(color).append(SQUARE).append(RESET); // Highlight element in specified color
} //else if (sortedIndices.contains(col)) {
//output.append(GREEN).append(SQUARE).append(RESET);
// }
output.append(color).append(Utils.getCurrentSymbol()).append(RESET); // Highlight element in specified color
}
else {
output.append(SQUARE);
output.append(Utils.getCurrentSymbol());
}
} else {
output.append(" ");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ui/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public UI(Terminal terminal, LineReader reader) {
UI.terminal = terminal;
UI.reader = reader;
array = new ArrayList<>();
Utils.setSymbolBasedOnOS();
}

public void start() {
Expand Down
35 changes: 24 additions & 11 deletions src/main/java/ui/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ui;


import java.util.ArrayList;
import java.util.Random;
import java.util.Set;
Expand All @@ -9,6 +8,13 @@
import static ui.UI.terminal;

public abstract class Utils {
final static String RESET = "\033[0m";
final static String RED = "\033[31m"; // comparison element
final static String BLUE = "\033[34m"; // target element
final static String GREEN = "\033[32m"; // sorted elements
final static char linuxSymbol = '◼';
final static char windowsSymbol = '#';
private static char currentSymbol;

public static int findMax(ArrayList<Integer> array) {
int max = 0;
Expand All @@ -33,10 +39,6 @@ public static void swapHighlighted(ArrayList<Integer> arr, int currentIndex, int


public static void displayVerticalArray(ArrayList<Integer> arrayList, int currentIndex, int targetIndex, Set<Integer> sortedIndices) {
final String RESET = "\033[0m";
final String RED = "\033[31m"; // comparison element
final String BLUE = "\033[34m"; // target element
final String GREEN = "\033[32m"; // sorted elements
StringBuilder output = new StringBuilder();

int maxHeight = findMax(arrayList);
Expand All @@ -48,21 +50,20 @@ public static void displayVerticalArray(ArrayList<Integer> arrayList, int curren
int element = arrayList.get(col);
if (element >= row) {
if (sortedIndices.contains(col)) {
output.append(GREEN).append("◼").append(RESET);
output.append(GREEN).append(currentSymbol).append(RESET);
} else if (col == currentIndex) {
output.append(RED).append("◼").append(RESET);
output.append(RED).append(currentSymbol).append(RESET);
} else if (col == targetIndex) {
output.append(BLUE).append("◼").append(RESET);
output.append(BLUE).append(currentSymbol).append(RESET);
} else {
output.append("◼");
output.append(currentSymbol);
}
} else {
output.append(" ");
}
}
output.append("\n");
}
//printInCenter(output.toString(), " ");
System.out.print(output);

/*
Expand All @@ -87,7 +88,7 @@ public static void displayVerticalArray(ArrayList<Integer> arrayList) {
for (int row = maxHeight; row > 0; row--) {
output.append("\033[2K"); // Clear the whole line
for (int element : arrayList) {
if (element >= row) output.append("◼");
if (element >= row) output.append(currentSymbol);
else output.append(" ");
}
output.append("\n");
Expand Down Expand Up @@ -140,4 +141,16 @@ public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}

public static void setSymbolBasedOnOS() {
String os = System.getProperty("os.name"); // Test on macOS and add it later
if (os.equals("Linux")) {
currentSymbol = linuxSymbol;
} else {
currentSymbol = windowsSymbol;
}
}
public static char getCurrentSymbol(){
return currentSymbol;
}
}