Skip to content

Commit

Permalink
feat: created bucket_sort.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
YashTariyal authored Oct 13, 2024
1 parent 44127b2 commit ed0d23c
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions sorts/bucket_sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Import the prompt-sync package
const prompt = require('prompt-sync')();

// Function to sort individual buckets using insertion sort
function insertionSort(bucket: number[]): number[] {
for (let i = 1; i < bucket.length; i++) {
let key = bucket[i];
let j = i - 1;
while (j >= 0 && bucket[j] > key) {
bucket[j + 1] = bucket[j];
j--;
}
bucket[j + 1] = key;
}
return bucket;
}

// Function to sort arr[] using bucket sort
function bucketSort(arr: number[]): number[] {
const n = arr.length;

// 1) Create n empty buckets
let buckets: number[][] = new Array(n);
for (let i = 0; i < n; i++) {
buckets[i] = [];
}

// 2) Put array elements in different buckets
for (let i = 0; i < n; i++) {
const bi = Math.floor(n * arr[i]); // Index in the bucket array
buckets[bi].push(arr[i]);
}

// 3) Sort individual buckets using insertion sort
for (let i = 0; i < n; i++) {
buckets[i] = insertionSort(buckets[i]);
}

// 4) Concatenate all buckets into the original array
let index = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < buckets[i].length; j++) {
arr[index++] = buckets[i][j];
}
}

return arr;
}

// Function to get user input and handle sorting
function main() {
// Take the number of elements from the user
const n = parseInt(prompt('Enter the number of elements: '));

// Initialize an array and take user input for each element
let arr: number[] = [];
for (let i = 0; i < n; i++) {
const element = parseFloat(prompt(`Enter element ${i + 1}: `));
arr.push(element);
}

// Sort the array using bucket sort
const sortedArr = bucketSort(arr);

// Print the sorted array
console.log('Sorted array is:', sortedArr);
}

// Run the main function to start the program
main();

0 comments on commit ed0d23c

Please sign in to comment.