-
Notifications
You must be signed in to change notification settings - Fork 20.8k
sleep sort #5285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
sleep sort #5285
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
47f0a86
initial commit.
aishuarya bf2110c
review comments
aishuarya 79551af
Merge branch 'master' into feature/sleepSort
aishuarya 3d357b6
Review comments
aishuarya e469a66
Merge branch 'master' into feature/sleepSort
aishuarya 4e3b94d
Merge branch 'master' into feature/sleepSort
aishuarya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.thealgorithms.sorts; | ||
|
|
||
| /* | ||
| * @author Aiswarya PM (https://github.com/aishuarya) | ||
| * The SleepSort class demonstrates the Sleep Sort algorithm. | ||
| * Sleep Sort is a sorting algorithm that works by leveraging the concurrency of threads. | ||
| * For each number in the list, a thread is created that sleeps for a duration proportional | ||
| * to the value of the number. After waking up, the thread prints the number. | ||
| * Thus, numbers with smaller values wake up and are printed earlier than those with larger values. | ||
| * | ||
| * Note: The sleep duration is not always precise due to system scheduling and thread management. | ||
| * As a result, this algorithm may not always produce correct results for all inputs, especially | ||
| * with closely adjacent numbers. For such cases, using a BlockingQueue to store and retrieve | ||
| * numbers in the order they are processed can ensure correct output. | ||
| */ | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| final class SleepSort { | ||
|
|
||
| private SleepSort() { | ||
| } | ||
|
|
||
| /** | ||
| * @param array the list of integers to sort | ||
| * @return the sorted list of integers | ||
| */ | ||
| public static List<Integer> sleepSort(List<Integer> array) { | ||
|
|
||
| // List to collect sorted elements | ||
| List<Integer> sortedList = Collections.synchronizedList(new ArrayList<>()); | ||
| List<Thread> threads = new ArrayList<>(); | ||
|
|
||
| // Validate that all numbers are non-negative before starting any threads | ||
| for (int number : array) { | ||
| if (number < 0) { | ||
| throw new IllegalArgumentException("All numbers must be non-negative. Found: " + number); | ||
| } | ||
|
Comment on lines
+38
to
+40
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should happen, before starting any of the new threads. |
||
| } | ||
|
|
||
| for (final int number : array) { | ||
| Thread thread = new Thread(() -> { | ||
| try { | ||
| Thread.sleep(number); // Sleep for 'number' milliseconds | ||
| sortedList.add(number); // Add the number to the list after sleeping | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); // Restore interrupted status | ||
| e.printStackTrace(); | ||
| } | ||
| }); | ||
|
|
||
| threads.add(thread); // Add the thread to the list | ||
| thread.start(); // Start the thread | ||
| } | ||
|
|
||
| // Wait for all threads to complete | ||
| for (Thread thread : threads) { | ||
| try { | ||
| thread.join(); // Wait for each thread to finish | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| // Return the sorted list | ||
| // The list is synchronized, so no need for additional synchronization here | ||
| Collections.sort(sortedList); | ||
| return sortedList; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.thealgorithms.sorts; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class SleepSortTest { | ||
| @Test | ||
| public void testSleepSort() { | ||
| List<Integer> numbers = new ArrayList<>(); | ||
| Collections.addAll(numbers, 4, 6, 8, 1, 10); | ||
|
|
||
| // Get sorted result from sleepSort | ||
| List<Integer> sortedNumbers = SleepSort.sleepSort(numbers); | ||
|
|
||
| // Expected sorted result | ||
| List<Integer> expected = List.of(1, 4, 6, 8, 10); | ||
|
|
||
| // Check if the sorted list matches the expected result | ||
| assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSleepSortWithAdjacentNumbers() { | ||
| List<Integer> numbers = new ArrayList<>(); | ||
| Collections.addAll(numbers, 1, 2, 3, 4); | ||
|
|
||
| // Get sorted result from sleepSort | ||
| List<Integer> sortedNumbers = SleepSort.sleepSort(numbers); | ||
|
|
||
| // Expected sorted result | ||
| List<Integer> expected = List.of(1, 2, 3, 4); | ||
|
|
||
| // Check if the sorted list matches the expected result | ||
| assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSleepSortWithLargeNumbers() { | ||
| List<Integer> numbers = new ArrayList<>(); | ||
| Collections.addAll(numbers, 1000, 500, 2000, 1500); | ||
|
|
||
| // Get sorted result from sleepSort | ||
| List<Integer> sortedNumbers = SleepSort.sleepSort(numbers); | ||
|
|
||
| // Expected sorted result | ||
| List<Integer> expected = List.of(500, 1000, 1500, 2000); | ||
|
|
||
| // Check if the sorted list matches the expected result | ||
| assertEquals(expected, sortedNumbers, "The sorted numbers should match the expected list."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSleepSortWithNegativeNumbers() { | ||
| List<Integer> numbers = List.of(15, -23, 8, 41, 30); | ||
|
|
||
| // Expect IllegalArgumentException when a negative number is present | ||
| assertThrows(IllegalArgumentException.class, () -> SleepSort.sleepSort(numbers), "Expected sleepSort() to throw IllegalArgumentException when negative number is present"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.