Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5d0b09f

Browse files
committed
bubble sort added
1 parent 3e339be commit 5d0b09f

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

‎README.md‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# Algorithm Visualizer in Java
2-
3-
By @Alimov-8👨🏻‍💻 and @Rustam-Z👨🏼‍💻
42

5-
Updated: December 5, 2020
3+
By @Alimov-8👨🏻‍💻 and @Rustam-Z👨🏼‍💻
64

7-
### References:
5+
Updated: December 13, 2020
6+
7+
- Sorting Algorithms Visualizer
8+
- Picture Puzzle Sorter `TODO`
9+
- Pathfinding algorithm `TODO`
10+
11+
## Skills Learned:
12+
- Multi-Threading using Swing Worker
13+
- Customising Swing Components
14+
- Graphics 2D
15+
- Apply Sorting Algorithms
16+
17+
## References:
818
- https://clementmihailescu.github.io/Sorting-Visualizer/
919

1020
- https://www.geeksforgeeks.org/java-swing-jmenubar/

‎Sorting.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ public void actionPerformed(ActionEvent e) {
7272
}
7373
else if (e.getSource() == jbtMerge)
7474
System.out.println("jbtMerge button clicked");
75-
else if (e.getSource() == jbtBubble)
75+
else if (e.getSource() == jbtBubble) {
76+
sortAlgo.bubbleSort(); // Bubble sort algotithm
77+
sortAlgo.initShuffler(); // shuffling
7678
System.out.println("jbtBubble button clicked");
79+
}
7780
else if (e.getSource() == jbtInsertion) {
78-
sortAlgo.insertionSort(); // Sorting algotithm
79-
sortAlgo.initShuffler();
81+
sortAlgo.insertionSort(); // Insertion algotithm
82+
sortAlgo.initShuffler(); // shuffling
83+
System.out.println("jbtInsertion button clicked");
8084
}
8185
else if (e.getSource() == jbtSelection)
8286
System.out.println("jbtSelection button clicked");

‎SortingAlgorithm.java‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class SortingAlgorithm extends JPanel {
1212
private final int WIDTH = 800, HEIGHT = WIDTH * 9 /16;
13-
private final int SIZE = 200; // the number if sorting elements
13+
private final int SIZE = 100; // the number if sorting elements
1414
private final float BAR_WIDTH = (float)WIDTH / SIZE; // bar width
1515
private float[] bar_height = new float[SIZE]; // height of bars
1616
private SwingWorker<Void, Void> shuffler, sorter;
@@ -20,7 +20,7 @@ public class SortingAlgorithm extends JPanel {
2020
setBackground(Color.BLACK);
2121
setPreferredSize(new Dimension(WIDTH, HEIGHT));
2222
initBarHeight(); // initialize the height of each bar
23-
initShuffler(); // shuffle each bar
23+
//initShuffler(); // shuffle each bar
2424
}
2525

2626
@Override
@@ -56,7 +56,6 @@ public void paintComponent(Graphics g) {
5656

5757
public void insertionSort() {
5858
/*Insertion sort algorithm*/
59-
6059
sorter = new SwingWorker<>() {
6160
@Override
6261
public Void doInBackground() throws InterruptedException {
@@ -78,9 +77,31 @@ public Void doInBackground() throws InterruptedException {
7877
};
7978
}
8079

80+
public void bubbleSort() {
81+
/*Buuble sort algorithm*/
82+
sorter = new SwingWorker<>() {
83+
@Override
84+
public Void doInBackground() throws InterruptedException {
85+
for(current_index = 0; current_index < SIZE; current_index++) {
86+
for(traversing_index = 1; traversing_index < (SIZE - current_index); traversing_index++) {
87+
if(bar_height[traversing_index - 1] > bar_height[traversing_index]) {
88+
swap(traversing_index, traversing_index - 1);
89+
90+
Thread.sleep(1);
91+
repaint();
92+
}
93+
}
94+
}
95+
current_index = 0;
96+
traversing_index = 0;
97+
98+
return null;
99+
}
100+
};
101+
}
102+
81103
public void initShuffler() {
82104
/*Shuffles each bar*/
83-
84105
shuffler = new SwingWorker<>() {
85106
@Override
86107
public Void doInBackground() throws InterruptedException {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /