Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
70 changes: 70 additions & 0 deletions Sorts/BitonicSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Bitonic Sort is a parallel sorting algorithm that can be used to sort a sequence
// of numbers in ascending or descending order. It is based on the concept of
// bitonic sequences, which are sequences that first increase and then decrease
// or vice versa.

// Function to perform Bitonic Sort on an array
function bitonicSort(arr, ascending = true) {
const n = arr.length;
bitonicSortRecursive(arr, 0, n, ascending);
}

// Function to recursively perform Bitonic Sort
function bitonicSortRecursive(arr, low, count, ascending) {
if (count > 1) {
const k = count / 2;

// Sort the first half in ascending order
bitonicSortRecursive(arr, low, k, true);

// Sort the second half in descending order
bitonicSortRecursive(arr, low + k, k, false);

// Merge the two sorted subarrays
bitonicMerge(arr, low, count, ascending);
}
}

// Function to merge two sorted subarrays in Bitonic Sort
function bitonicMerge(arr, low, count, ascending) {
if (count > 1) {
const k = greatestPowerOfTwoLessThan(count);
for (let i = low; i < low + count - k; i++) {
compareAndSwap(arr, i, i + k, ascending);
}
bitonicMerge(arr, low, k, ascending);
bitonicMerge(arr, low + k, count - k, ascending);
}
}

// Function to find the greatest power of two less than a number
function greatestPowerOfTwoLessThan(n) {
let k = 1;
while (k > 0 && k < n) {
k = k * 2;
}
return k / 2;
}

// Function to compare and swap two elements in the array based on the sorting order
function compareAndSwap(arr, i, j, ascending) {
if ((arr[i] > arr[j] && ascending) || (arr[i] < arr[j] && !ascending)) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

// Example usage
const arr = [3, 7, 4, 8, 6, 2, 1, 5];
console.log("Original Array:", arr);

// Perform Bitonic Sort on the array in ascending order
bitonicSort(arr);

console.log("Bitonic-Sorted Array in Ascending Order:", arr);

// Perform Bitonic Sort on the same array in descending order
bitonicSort(arr, false);

console.log("Bitonic-Sorted Array in Descending Order:", arr);
44 changes: 44 additions & 0 deletions Sorts/SleepSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Sleep Sort is a sorting algorithm that sorts an array of non-negative integers.
* It works by "sleeping" for a duration proportional to each element's value.
* Elements with smaller values will "wake up" earlier and appear earlier in the sorted result.
*
* @param {number[]} arr - The array of non-negative integers to be sorted.
* @returns {Promise<number[]>} - A Promise that resolves to the sorted array.
*
* @example
* const input = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
* sleepSort(input).then((result) => {
* console.log(result); // Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
* });
*
* Time Complexity: O(k * n)
* - k is the maximum value in the input array.
* - Sleeping for each element takes O(k) time, and there are n elements in the array.
* - Therefore, the time complexity is O(k * n).
*
* Space Complexity: O(n)
* - Additional space is required for the result array, which has n elements.
* - The space complexity is O(n).
*/
export function sleepSort(arr) {
// Helper function to sleep for a given duration (in milliseconds)
function sleep(duration) {
return new Promise((resolve) => setTimeout(resolve, duration));
}

// Function to perform the sleep sort
async function performSleepSort() {
const result = [];
for (const num of arr) {
// Sleep for a duration proportional to the element's value
await sleep(num);
result.push(num);
}
return result;
}

return performSleepSort();
}


22 changes: 22 additions & 0 deletions Sorts/test/SleepSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Jest tests for the sleepSort function.
*/
import { alternativeBubbleSort, sleepSort } from '../SleepSort'
describe("sleepSort", () => {
it("should return an empty array when given an empty array", async () => {
const result = await sleepSort([]);
expect(result).toEqual([]);
});

it("should sort the input array using sleep sort", async () => {
const input = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
const result = await sleepSort(input);
expect(result).toEqual([1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]);
});

it("should handle duplicate values correctly", async () => {
const input = [3, 3, 3, 2, 2, 1];
const result = await sleepSort(input);
expect(result).toEqual([1, 2, 2, 3, 3, 3]);
});
});