Skip to content

Commit 6e789ad

Browse files
committed
initial commit.
1 parent 3e75e73 commit 6e789ad

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.thealgorithms.sorts;
22

3-
/**
3+
/*
44
* @author Aiswarya PM (https://github.com/aishuarya)
55
* The SleepSort class demonstrates the Sleep Sort algorithm.
66
* Sleep Sort is a sorting algorithm that works by leveraging the concurrency of threads.
@@ -17,16 +17,19 @@
1717
import java.util.ArrayList;
1818
import java.util.Collections;
1919

20-
public class SleepSort {
20+
final class SleepSort {
21+
22+
private SleepSort() {
23+
}
2124

2225
public static void sleepSort(ArrayList<Integer> array) {
2326
ArrayList<Thread> threads = new ArrayList<>();
2427

2528
for (int numbers : array) {
2629
Thread thread = new Thread(() -> {
2730
try {
28-
Thread.sleep(numbers); // Sleep for 'num' milliseconds
29-
System.out.print(numbers + " "); // Print the number after sleeping
31+
Thread.sleep(numbers); // Sleep for 'num' milliseconds
32+
System.out.print(numbers + " "); // Print the number after sleeping
3033
} catch (InterruptedException e) {
3134
e.printStackTrace();
3235
}

src/test/java/com/thealgorithms/sorts/SleepSortTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.thealgorithms.sorts;
22

3-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
46
import java.io.ByteArrayOutputStream;
57
import java.io.PrintStream;
68
import java.util.ArrayList;
9+
import java.util.Arrays;
710
import java.util.Collections;
811
import java.util.List;
9-
import static org.junit.jupiter.api.Assertions.assertEquals;
10-
import static org.junit.jupiter.api.Assertions.assertTrue;
11-
12+
import java.util.stream.Collectors;
13+
import org.junit.jupiter.api.Test;
1214
public class SleepSortTest {
1315

1416
// Method to capture the output of the SleepSort
@@ -79,12 +81,7 @@ public void testSleepSortWithLargeNumbers() {
7981

8082
// Helper method to parse the output string to a list of integers
8183
private List<Integer> parseOutput(String output) {
82-
String[] parts = output.split("\\s+");
83-
List<Integer> numbers = new ArrayList<>();
84-
for (String part : parts) {
85-
numbers.add(Integer.parseInt(part));
86-
}
87-
return numbers;
84+
return Arrays.stream(output.trim().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
8885
}
8986
private boolean isSorted(List<Integer> list) {
9087
for (int i = 1; i < list.size(); i++) {

0 commit comments

Comments
 (0)