Skip to content

Commit bf2110c

Browse files
committed
review comments
1 parent 47f0a86 commit bf2110c

File tree

2 files changed

+52
-73
lines changed

2 files changed

+52
-73
lines changed

src/main/java/com/thealgorithms/sorts/SleepSort.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,53 @@
1616

1717
import java.util.ArrayList;
1818
import java.util.Collections;
19+
import java.util.List;
1920

2021
final class SleepSort {
2122

2223
private SleepSort() {
2324
}
2425

25-
public static void sleepSort(ArrayList<Integer> array) {
26-
ArrayList<Thread> threads = new ArrayList<>();
26+
/**
27+
* @param array the list of integers to sort
28+
* @return the sorted list of integers
29+
*/
30+
public static List<Integer> sleepSort(List<Integer> array) {
2731

28-
for (int numbers : array) {
32+
// List to collect sorted elements
33+
List<Integer> sortedList = Collections.synchronizedList(new ArrayList<>());
34+
List<Thread> threads = new ArrayList<>();
35+
36+
for (int number : array) {
37+
if (number < 0) {
38+
throw new IllegalArgumentException("All numbers must be non-negative. Found: " + number);
39+
}
2940
Thread thread = new Thread(() -> {
3041
try {
31-
Thread.sleep(numbers); // Sleep for 'num' milliseconds
32-
System.out.print(numbers + " "); // Print the number after sleeping
42+
Thread.sleep(number); // Sleep for 'number' milliseconds
43+
sortedList.add(number); // Add the number to the list after sleeping
3344
} catch (InterruptedException e) {
45+
Thread.currentThread().interrupt(); // Restore interrupted status
3446
e.printStackTrace();
3547
}
3648
});
3749

38-
threads.add(thread); // Add the thread to the ArrayList
50+
threads.add(thread); // Add the thread to the list
3951
thread.start(); // Start the thread
4052
}
4153

54+
// Wait for all threads to complete
4255
for (Thread thread : threads) {
4356
try {
4457
thread.join(); // Wait for each thread to finish
4558
} catch (InterruptedException e) {
4659
e.printStackTrace();
4760
}
4861
}
49-
}
50-
51-
public static void main(String[] args) {
52-
// Example usage
53-
ArrayList<Integer> numbers = new ArrayList<>();
54-
Collections.addAll(numbers, 15, 23, 8, 41, 30);
5562

56-
System.out.println("Sorting numbers using Sleep Sort:");
57-
sleepSort(numbers);
63+
// Return the sorted list
64+
// The list is synchronized, so no need for additional synchronization here
65+
Collections.sort(sortedList);
66+
return sortedList;
5867
}
5968
}
Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,64 @@
11
package com.thealgorithms.sorts;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
import java.io.ByteArrayOutputStream;
7-
import java.io.PrintStream;
86
import java.util.ArrayList;
9-
import java.util.Arrays;
107
import java.util.Collections;
118
import java.util.List;
12-
import java.util.stream.Collectors;
139
import org.junit.jupiter.api.Test;
14-
public class SleepSortTest {
15-
16-
// Method to capture the output of the SleepSort
17-
private String captureOutput(Runnable task) {
18-
// Create a stream to capture output
19-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
20-
PrintStream originalOut = System.out;
21-
System.setOut(new PrintStream(outputStream));
22-
23-
// Run the task
24-
task.run();
25-
26-
// Restore original output
27-
System.setOut(originalOut);
28-
29-
// Return the captured output
30-
return outputStream.toString().trim();
31-
}
3210

11+
public class SleepSortTest {
3312
@Test
3413
public void testSleepSort() {
35-
ArrayList<Integer> numbers = new ArrayList<>();
14+
List<Integer> numbers = new ArrayList<>();
3615
Collections.addAll(numbers, 4, 6, 8, 1, 10);
3716

38-
// Capture output
39-
String output = captureOutput(() -> SleepSort.sleepSort(numbers));
17+
// Get sorted result from sleepSort
18+
List<Integer> sortedNumbers = SleepSort.sleepSort(numbers);
4019

41-
// Expected output
42-
String expected = "1 4 6 8 10";
20+
// Expected sorted result
21+
List<Integer> expected = List.of(1, 4, 6, 8, 10);
4322

44-
// Check if the captured output matches the expected output
45-
assertEquals(expected, output);
23+
// Check if the sorted list matches the expected result
24+
assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list.");
4625
}
4726

4827
@Test
4928
public void testSleepSortWithAdjacentNumbers() {
50-
ArrayList<Integer> numbers = new ArrayList<>();
29+
List<Integer> numbers = new ArrayList<>();
5130
Collections.addAll(numbers, 1, 2, 3, 4);
5231

53-
// Capture output
54-
String output = captureOutput(() -> SleepSort.sleepSort(numbers));
55-
56-
// Expected output
57-
String expected = "1 2 3 4";
32+
// Get sorted result from sleepSort
33+
List<Integer> sortedNumbers = SleepSort.sleepSort(numbers);
5834

59-
// Check if the output is sorted
60-
List<Integer> outputNumbers = parseOutput(output);
61-
assertTrue(isSorted(outputNumbers), "The output is not sorted.");
35+
// Expected sorted result
36+
List<Integer> expected = List.of(1, 2, 3, 4);
6237

63-
// Check if the output contains all expected numbers
64-
assertEquals(expected, output, "The output does not contain the expected numbers.");
38+
// Check if the sorted list matches the expected result
39+
assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list.");
6540
}
6641

6742
@Test
6843
public void testSleepSortWithLargeNumbers() {
69-
ArrayList<Integer> numbers = new ArrayList<>();
44+
List<Integer> numbers = new ArrayList<>();
7045
Collections.addAll(numbers, 1000, 500, 2000, 1500);
7146

72-
// Capture output
73-
String output = captureOutput(() -> SleepSort.sleepSort(numbers));
47+
// Get sorted result from sleepSort
48+
List<Integer> sortedNumbers = SleepSort.sleepSort(numbers);
7449

75-
// Expected output
76-
String expected = "500 1000 1500 2000";
50+
// Expected sorted result
51+
List<Integer> expected = List.of(500, 1000, 1500, 2000);
7752

78-
// Check if the captured output matches the expected output
79-
assertEquals(expected, output);
53+
// Check if the sorted list matches the expected result
54+
assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list.");
8055
}
8156

82-
// Helper method to parse the output string to a list of integers
83-
private List<Integer> parseOutput(String output) {
84-
return Arrays.stream(output.trim().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
85-
}
86-
private boolean isSorted(List<Integer> list) {
87-
for (int i = 1; i < list.size(); i++) {
88-
if (list.get(i - 1) > list.get(i)) {
89-
return false;
90-
}
91-
}
92-
return true;
57+
@Test
58+
public void testSleepSortWithNegativeNumbers() {
59+
List<Integer> numbers = List.of(15, -23, 8, 41, 30);
60+
61+
// Expect IllegalArgumentException when a negative number is present
62+
assertThrows(IllegalArgumentException.class, () -> SleepSort.sleepSort(numbers), "Expected sleepSort() to throw IllegalArgumentException when negative number is present");
9363
}
9464
}

0 commit comments

Comments
 (0)