From 0961d64caaae7b7d8ee6229ec0dc2c86fb1e5835 Mon Sep 17 00:00:00 2001 From: Aman Upadhyay Date: Wed, 16 Oct 2024 14:13:07 +0100 Subject: [PATCH] feat: Add radixsort implementation --- sort/radixsort.jule | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 sort/radixsort.jule diff --git a/sort/radixsort.jule b/sort/radixsort.jule new file mode 100644 index 0000000..9afbd92 --- /dev/null +++ b/sort/radixsort.jule @@ -0,0 +1,51 @@ +// description: Implementation of in-place Radix Sort algorithm +// details: +// A simple in-place Radix Sort algorithm implementation. [Wikipedia](https://en.wikipedia.org/wiki/Radix_sort) +// author(s) [Aman Upadhyay](https://github.com/amanxupadhyay) + +fn CountingSort[T: int](mut arr: []T, exp: int) { + n := len(arr) + output := make([]T, n) + count := make([]int, 10) + + for i := 0; i < n; i++ { + count[(arr[i] / exp) % 10] += 1 + } + + for i := 1; i < 10; i++ { + count[i] += count[i-1] + } + + for i := n-1; i >= 0; i-- { + output[count[(arr[i] / exp) % 10] - 1] = arr[i] + count[(arr[i] / exp) % 10] -= 1 + } + + for i := 0; i < n; i++ { + arr[i] = output[i] + } +} + +fn RadixSort[T: int](mut arr: []T): []T { + max := arr[0] + for i := 1; i < len(arr); i++ { + if arr[i] > max { + max = arr[i] + } + } + + for exp := 1; max / exp > 0; exp *= 10 { + CountingSort(arr, exp) + } + + ret arr +} + +// Example usage +fn main() { + arr := [170, 45, 75, 90, 802, 24, 2, 66] + arr = RadixSort(arr) + println(arr) +} + +// Output: [2, 24, 45, 66, 75, 90, 170, 802] \ No newline at end of file