Skip to content

Commit

Permalink
refactor radix sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoanh An committed Oct 3, 2019
1 parent df0d197 commit b6439da
Showing 2 changed files with 36 additions and 226 deletions.
222 changes: 0 additions & 222 deletions other/bit_operations_test.go

This file was deleted.

40 changes: 36 additions & 4 deletions other/radix_sort_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Problem:
// Implement radix sort.
//
// Mechanic:
// Sort the input numbers one digit at a time.
//
// // Mechanic: // Sort the input numbers one digit at a time. //
// Cost:
// O(n) time and O(n) space.

@@ -39,6 +36,27 @@ func TestRadixSort(t *testing.T) {
}
}

func TestGetBit(t *testing.T) {
tests := []struct {
in1 int
in2 int
expected uint
}{
{6, 0, 0},
{6, 1, 1},
{6, 2, 1},
{6, 3, 0},
{6, 4, 0},
{6, 5, 0},
{6, 6, 0},
}

for _, tt := range tests {
result := getBit(tt.in1, tt.in2)
common.Equal(t, tt.expected, result)
}
}

func radixSort(in []int) []int {
out := []int{}
for i := 0; i < 64; i++ {
@@ -79,3 +97,17 @@ func sortingBit(in []int, bit int) []int {

return sorted
}

// getBit returns the value of ith bit for a given number.
func getBit(number, i int) uint {
// shift 1 over by i bits, creating a bitmask value.
mask := 1 << uint(i)

// perform an AND with number to clear all bits other than the one at bit i.
// if the value is not 0, bit 1 must have a 1.
if number&mask != 0 {
return 1
}

return 0
}

0 comments on commit b6439da

Please sign in to comment.