Skip to content

Commit 33bd28b

Browse files
committed
sleep Sort
Added Sleep Sort
1 parent dd9e9f2 commit 33bd28b

File tree

4 files changed

+7362
-1412
lines changed

4 files changed

+7362
-1412
lines changed

Sorts/SleepSort.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,32 @@
2121
* - Additional space is required for the result array, which has n elements.
2222
* - The space complexity is O(n).
2323
*/
24-
export function sleepSort(arr) {
25-
// Helper function to sleep for a given duration (in milliseconds)
26-
function sleep(duration) {
27-
return new Promise((resolve) => setTimeout(resolve, duration));
28-
}
29-
30-
// Function to perform the sleep sort
31-
async function performSleepSort() {
32-
const result = [];
33-
for (const num of arr) {
34-
// Sleep for a duration proportional to the element's value
35-
await sleep(num);
36-
result.push(num);
37-
}
38-
return result;
39-
}
40-
41-
return performSleepSort();
24+
25+
async function sleep(ms) {
26+
return new Promise(resolve => setTimeout(resolve, ms));
27+
}
28+
29+
async function sleepSort(arr) {
30+
if (!Array.isArray(arr)) {
31+
throw new Error('Input must be an array.');
32+
}
33+
34+
const sortedArray = [];
35+
const promises = [];
36+
37+
for (const num of arr) {
38+
promises.push(new Promise(resolve => {
39+
setTimeout(async () => {
40+
sortedArray.push(num);
41+
resolve();
42+
}, num);
43+
}));
4244
}
4345

44-
46+
await Promise.all(promises);
47+
return sortedArray.sort((a, b) => a - b);
48+
}
49+
50+
export default sleepSort;
51+
52+

Sorts/test/SleepSort.test.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
/**
22
* Jest tests for the sleepSort function.
33
*/
4-
import { alternativeBubbleSort, sleepSort } from '../SleepSort'
5-
describe("sleepSort", () => {
6-
it("should return an empty array when given an empty array", async () => {
7-
const result = await sleepSort([]);
8-
expect(result).toEqual([]);
9-
});
10-
11-
it("should sort the input array using sleep sort", async () => {
12-
const input = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
13-
const result = await sleepSort(input);
14-
expect(result).toEqual([1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]);
15-
});
16-
17-
it("should handle duplicate values correctly", async () => {
18-
const input = [3, 3, 3, 2, 2, 1];
19-
const result = await sleepSort(input);
20-
expect(result).toEqual([1, 2, 2, 3, 3, 3]);
21-
});
22-
});
4+
import sleepSort from '../SleepSort'; // Update the import path as needed
5+
6+
describe('sleepSort', () => {
7+
it('should return an empty array when given an empty array', async () => {
8+
const result = await sleepSort([]);
9+
expect(result).toEqual([]);
10+
});
11+
12+
it('should sort the input array using sleep sort', async () => {
13+
const input = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
14+
const result = await sleepSort(input);
15+
const sortedInput = input.slice().sort((a, b) => a - b);
16+
expect(result).toEqual(sortedInput);
17+
});
18+
19+
it('should handle duplicate values correctly', async () => {
20+
const input = [3, 2, 1, 9, 8, 4, 2];
21+
const result = await sleepSort(input);
22+
const sortedInput = input.slice().sort((a, b) => a - b);
23+
expect(result).toEqual(sortedInput);
24+
});
25+
});
26+

0 commit comments

Comments
 (0)