Repository navigation
Expand file tree
/
Copy pathAlgorithmVisualizer.java
More file actions
173 lines (141 loc) · 5.13 KB
/
Copy pathAlgorithmVisualizer.java
File metadata and controls
173 lines (141 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import javax.swing.*;
import java.awt.*;
/**
* AlgorithmVisualizer - Visualizes sorting algorithms in real-time using Swing.
* Currently implements Merge Sort with animated visualization.
*/
public class AlgorithmVisualizer extends JPanel {
private static final int WINDOW_WIDTH = 1000;
private static final int WINDOW_HEIGHT = 600;
private static final int BAR_WIDTH = 8;
private static final int DELAY = 10;
private static final Color COMPARING_COLOR = new Color(255, 165, 0); // Orange
private static final Color SORTED_COLOR = new Color(50, 205, 50); // Green
private static final Color DEFAULT_COLOR = new Color(70, 130, 180); // Steel Blue
private int[] array;
private boolean[] sorted;
private int comparingIndex1 = -1;
private int comparingIndex2 = -1;
public AlgorithmVisualizer(int[] array) {
this.array = array;
this.sorted = new boolean[array.length];
setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
setBackground(Color.WHITE);
}
// ========== Setter Methods for Animation ========== //
public void setComparingIndices(int index1, int index2) {
this.comparingIndex1 = index1;
this.comparingIndex2 = index2;
repaint();
}
public void setSorted(int index) {
this.sorted[index] = true;
repaint();
}
public void resetComparingIndices() {
this.comparingIndex1 = -1;
this.comparingIndex2 = -1;
repaint();
}
// ========== Merge Sort Logic ========== //
/**
* Main merge sort method - public entry point.
*/
public void mergeSort(int[] arr) throws InterruptedException {
mergeSortHelper(arr, 0, arr.length - 1);
// Mark all as sorted
for (int i = 0; i < arr.length; i++) {
setSorted(i);
Thread.sleep(10);
}
}
/**
* Recursive merge sort helper.
*/
private void mergeSortHelper(int[] arr, int start, int end) throws InterruptedException {
if (start < end) {
int mid = (start + end) / 2;
mergeSortHelper(arr, start, mid);
mergeSortHelper(arr, mid + 1, end);
merge(arr, start, mid, end);
}
}
/**
* Merge two sorted subarrays.
*/
private void merge(int[] arr, int start, int mid, int end) throws InterruptedException {
int[] temp = new int[end - start + 1];
int i = start, j = mid + 1, k = 0;
while (i <= mid && j <= end) {
setComparingIndices(i, j);
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
Thread.sleep(DELAY);
}
while (i <= mid) {
setComparingIndices(i, -1);
temp[k++] = arr[i++];
Thread.sleep(DELAY);
}
while (j <= end) {
setComparingIndices(-1, j);
temp[k++] = arr[j++];
Thread.sleep(DELAY);
}
for (int p = 0; p < temp.length; p++) {
arr[start + p] = temp[p];
}
resetComparingIndices();
}
// ========== Drawing and Visualization ========== //
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = 0;
for (int i = 0; i < array.length; i++) {
int value = array[i];
int height = value * 4;
int y = WINDOW_HEIGHT - height;
// Determine color based on state
if (sorted[i]) {
g2d.setColor(SORTED_COLOR);
} else if (i == comparingIndex1 || i == comparingIndex2) {
g2d.setColor(COMPARING_COLOR);
} else {
g2d.setColor(DEFAULT_COLOR);
}
g2d.fillRect(x, y, BAR_WIDTH, height);
// Add border for better visibility
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(1));
g2d.drawRect(x, y, BAR_WIDTH, height);
x += BAR_WIDTH + 2;
}
}
// ========== Main ========== //
public static void main(String[] args) throws Exception {
int[] arr = {5, 3, 8, 4, 2, 1, 7, 6, 12, 45, 22, 9, 18, 30, 11, 25, 50, 15, 35, 40};
AlgorithmVisualizer panel = new AlgorithmVisualizer(arr);
JFrame frame = new JFrame("Merge Sort Visualizer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Start sorting in a separate thread
new Thread(() -> {
try {
panel.mergeSort(arr);
System.out.println("Sorting complete!");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Sorting interrupted: " + e.getMessage());
}
}).start();
}
}