@@ -59,7 +59,7 @@ public void paintComponent(Graphics g) {
5959 }
6060
6161 public void insertionSort () {
62- /*Insertion sort algorithm*/
62+ /*Insertion sort algorithm*/
6363 sorter = new SwingWorker <>() {
6464 @ Override
6565 public Void doInBackground () throws InterruptedException {
@@ -108,6 +108,7 @@ public Void doInBackground() throws InterruptedException {
108108 public void mergeSort () {
109109 /*Merge sorting algorithm*/
110110 // Change code from bubbleSort to mergeSort
111+ // TODO
111112
112113 sorter = new SwingWorker <>() {
113114 @ Override
@@ -131,6 +132,37 @@ public Void doInBackground() throws InterruptedException {
131132 };
132133 }
133134
135+ 136+ public void selectionSort () {
137+ /*Merge sorting algorithm*/
138+ // Change code from bubbleSort to mergeSort
139+ // TODO
140+ 141+ sorter = new SwingWorker <>() {
142+ @ Override
143+ public Void doInBackground () throws InterruptedException {
144+ for (current_index = 0 ; current_index < SIZE -1 ; current_index ++) {
145+ int min_index = current_index ;
146+ for (int traversing_index = current_index + 1 ; traversing_index < SIZE ; traversing_index ++) {
147+ if (bar_height [traversing_index ] < bar_height [min_index ]) {
148+ min_index = traversing_index ;
149+ }
150+ }
151+ 152+ swap (current_index , min_index );
153+ Thread .sleep (1 ); // controls the speed
154+ repaint (); // we need it because we ofter replace the contents of a JPanel
155+ 156+ }
157+ 158+ current_index = 0 ;
159+ traversing_index = 0 ;
160+ 161+ return null ;
162+ }
163+ };
164+ }
165+ 134166 public void initShuffler () {
135167 /*Shuffles each bar*/
136168 shuffler = new SwingWorker <>() {
0 commit comments