-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
91 lines (68 loc) · 1.58 KB
/
BubbleSort.java
File metadata and controls
91 lines (68 loc) · 1.58 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
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
/**
*
* @author sohamdessai
*
*/
public class BubbleSort {
private int[] array;
private int indicated = -1;
private int position = -1;
private JComponent component;
private static final int DELAY = 100;
public BubbleSort(int[] exarray, JComponent excomponent) {
this.array= exarray;
this.component= excomponent;
}
// Bubble sort swaping numbers
public void sort() throws InterruptedException {
boolean swap = true;
int j = 0;
while(swap) {
swap = false;
indicated = array.length - j - 1;
j++;
for(int i=0; i<array.length - j; i++) {
try {
if(array[i] > array[i+1]) {
int temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
position = i;
swap = true;
}
} finally {
}
pause(1);
}
if(swap == false) {
indicated = 0;
}
}
}
//indicates current graphic to be swapped
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.drawString("Bubble- An Algorithim that compares each pair of adjacent items and swaps them if they are in the wrong order.", 50, 550);
try {
for(int i=0; i<array.length; i++) {
if(i == position) {
g.setColor(Color.RED);
} else if(i >= indicated) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.BLUE);
}
g.fillRect(100 + (i*10), 600, 8, array[i]*2);
}
} finally {
}
}
public void pause(int steps) throws InterruptedException {
component.repaint();
Thread.sleep(DELAY * steps);
}
}