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 ddc1838

Browse files
committed
selection sort is working
1 parent 3f9c1da commit ddc1838

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

‎Sorting.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ else if (e.getSource() == jbtInsertion) {
9494
sortAlgo.initShuffler(); // shuffling
9595
}
9696
else if (e.getSource() == jbtSelection) {
97-
System.out.println("jbtSelection button clicked");
97+
sortAlgo.selectionSort(); // Insertion algotithm
98+
sortAlgo.initShuffler(); // shuffling
9899
}
99100
else if (e.getSource() == jbtStart) {
100101
System.out.println("jbtStart button clicked");

‎SortingAlgorithm.java‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
(0)

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