Skip to content

Commit f95a497

Browse files
committed
initial commit.
1 parent 87e6184 commit f95a497

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.sorts;
2+
3+
/**
4+
* @author Aiswarya PM (https://github.com/aishuarya)
5+
* The SleepSort class demonstrates the Sleep Sort algorithm.
6+
* Sleep Sort is a sorting algorithm that works by leveraging the concurrency of threads.
7+
* For each number in the list, a thread is created that sleeps for a duration proportional
8+
* to the value of the number. After waking up, the thread prints the number.
9+
* Thus, numbers with smaller values wake up and are printed earlier than those with larger values.
10+
*
11+
* Note: The sleep duration is not always precise due to system scheduling and thread management.
12+
* As a result, this algorithm may not always produce correct results for all inputs, especially
13+
* with closely adjacent numbers. For such cases, using a BlockingQueue to store and retrieve
14+
* numbers in the order they are processed can ensure correct output.
15+
*/
16+
17+
import java.util.ArrayList;
18+
import java.util.Collections;
19+
20+
public class SleepSort {
21+
22+
public static void sleepSort(ArrayList<Integer> array) {
23+
ArrayList<Thread> threads = new ArrayList<>();
24+
25+
for (int numbers : array) {
26+
Thread thread = new Thread(() -> {
27+
try {
28+
Thread.sleep(numbers); // Sleep for 'num' milliseconds
29+
System.out.print(numbers + " "); // Print the number after sleeping
30+
} catch (InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
});
34+
35+
threads.add(thread); // Add the thread to the ArrayList
36+
thread.start(); // Start the thread
37+
}
38+
39+
for (Thread thread : threads) {
40+
try {
41+
thread.join(); // Wait for each thread to finish
42+
} catch (InterruptedException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
}
47+
48+
public static void main(String[] args) {
49+
// Example usage
50+
ArrayList<Integer> numbers = new ArrayList<>();
51+
Collections.addAll(numbers, 15, 23, 8, 41, 30);
52+
53+
System.out.println("Sorting numbers using Sleep Sort:");
54+
sleepSort(numbers);
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.thealgorithms.sorts;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.List;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
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+
}
32+
33+
@Test
34+
public void testSleepSort() {
35+
ArrayList<Integer> numbers = new ArrayList<>();
36+
Collections.addAll(numbers, 4, 6, 8, 1, 10);
37+
38+
// Capture output
39+
String output = captureOutput(() -> SleepSort.sleepSort(numbers));
40+
41+
// Expected output
42+
String expected = "1 4 6 8 10";
43+
44+
// Check if the captured output matches the expected output
45+
assertEquals(expected, output);
46+
}
47+
48+
@Test
49+
public void testSleepSortWithAdjacentNumbers() {
50+
ArrayList<Integer> numbers = new ArrayList<>();
51+
Collections.addAll(numbers, 1, 2, 3, 4);
52+
53+
// Capture output
54+
String output = captureOutput(() -> SleepSort.sleepSort(numbers));
55+
56+
// Expected output
57+
String expected = "1 2 3 4";
58+
59+
// Check if the output is sorted
60+
List<Integer> outputNumbers = parseOutput(output);
61+
assertTrue(isSorted(outputNumbers), "The output is not sorted.");
62+
63+
// Check if the output contains all expected numbers
64+
assertEquals(expected, output, "The output does not contain the expected numbers.");
65+
}
66+
67+
@Test
68+
public void testSleepSortWithLargeNumbers() {
69+
ArrayList<Integer> numbers = new ArrayList<>();
70+
Collections.addAll(numbers, 1000, 500, 2000, 1500);
71+
72+
// Capture output
73+
String output = captureOutput(() -> SleepSort.sleepSort(numbers));
74+
75+
// Expected output
76+
String expected = "500 1000 1500 2000";
77+
78+
// Check if the captured output matches the expected output
79+
assertEquals(expected, output);
80+
}
81+
82+
// Helper method to parse the output string to a list of integers
83+
private List<Integer> parseOutput(String output) {
84+
String[] parts = output.split("\\s+");
85+
List<Integer> numbers = new ArrayList<>();
86+
for (String part : parts) {
87+
numbers.add(Integer.parseInt(part));
88+
}
89+
return numbers;
90+
}
91+
private boolean isSorted(List<Integer> list) {
92+
for (int i = 1; i < list.size(); i++) {
93+
if (list.get(i - 1) > list.get(i)) {
94+
return false;
95+
}
96+
}
97+
return true;
98+
}
99+
}

0 commit comments

Comments
 (0)