@@ -92,16 +92,27 @@ protected void process(List<Integer[]> chunks) {
9292 }
9393 }
9494
95- // The amount of time this thread waits after sending a chunk to be drawn
96- private void sleep () {
95+ /*
96+ * sleep()
97+ *
98+ * This method is called when a sorting algorithm is being
99+ * visualized and the algorithm needs to sleep to slow down
100+ * the visualization of sorting.
101+ */
102+ private void sleep () {
97103 try {
98- Thread . sleep ( algVisualizer . getDelay ());
99- if (!algVisualizer .stopSort ()) {
104+
105+ if (!algVisualizer .stopSort ()) {
100106 algVisualizer .setTotalDelay (algVisualizer .getTotalDelay () + algVisualizer .getDelay ());
101107 }
102- } catch (InterruptedException e ) {
103- e .printStackTrace ();
104- }
108+
109+ Thread .sleep (algVisualizer .getDelay ());
110+
111+ } catch (InterruptedException e ) {
112+
113+ e .printStackTrace ();
114+
115+ }
105116 }
106117
107118 /*
@@ -120,32 +131,37 @@ private void sleep() {
120131
121132 private void bubbleSort () {
122133 for (int i = 0 ; i < n - 1 ; i ++) {
123- if (algVisualizer .stopSort ())
134+
135+ if (algVisualizer .stopSort ())
124136 break ;
125- for (int j = 0 ; j < n - i - 1 ; j ++) {
126- if (algVisualizer .stopSort ()) {
137+
138+ for (int j = 0 ; j < n - i - 1 ; j ++) {
139+
140+ if (algVisualizer .stopSort ()) {
127141 break ;
128142 } else {
129- algVisualizer .setIndexComparisons (algVisualizer .getIndexComparisons () + 1 );
130- if (arr [j ] > arr [j + 1 ]) {
131- // swap arr[j] and arr[j+1]
132- int temp = arr [j ];
143+
144+ algVisualizer .setIndexComparisons (algVisualizer .getIndexComparisons () + 1 );
145+
146+ if (arr [j ] > arr [j + 1 ]) {
147+
148+ int temp = arr [j ];
133149 arr [j ] = arr [j + 1 ];
134150 arr [j + 1 ] = temp ;
135- // Add the pair of indexes that were swapped to the list.
136- arrDisplay .addSwappedIndexes (j , j + 1 );
137- publish (arr .clone ());
138- sleep ();
139- }
151+
152+ arrDisplay .addSwappedIndexes (j , j + 1 );
153+ publish (arr .clone ());
154+ sleep ();
155+ }
140156 }
141157 }
142158 }
143159 // Sorting complete, loop done
144160 if (!algVisualizer .stopSort ()) {
145161 arrDisplay .setComplete (true );
146- publish (arr .clone ());
147- sleep ();
148- }
162+ publish (arr .clone ());
163+ sleep ();
164+ }
149165 }
150166
151167 private void selectionSort () {
0 commit comments