1010
1111public 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