Skip to content
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

feat: Add radixsort implementation #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions sort/radixsort.jule
Original file line number Diff line number Diff line change
@@ -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]