Golang program to implement bucket sort


In this article we will learn to develop a go language program to implement the bucket sort through using custom sorting Algorithm. In Bucket sort we sort the unsorted array in different bucket every bucket contains a wide range of elements. The elements present in each bucket are then sorted by using a different sorting Algorithm, such as insertion sort or quicksort. Then the sorted buckets are merged back together.

Algorithm

  • Step 1 − First, we need to import the fmt package. Then create a function called bucketSort() which accepts the array to be sorted as an argument and returns the sorted array.

  • Step 2 − Initialize an empty bucket and store in it the maximum value of the array.

  • Step 3 − Calculate the index of the bucket that the current element belongs to by dividing the current element by the length of the input array and multiplying it by the number of buckets.

  • Step 4 − Append the current element to the bucket at the calculated index.

  • Step 5 − Now use a for loop to iterate through each bucket and sort each bucket using any sorting Algorithm of choice. In this Example, we are using the insertion sort Algorithm.

  • Step 6 − Now, arrange all the sorted buckets in correct order, starting from the first bucket to the last bucket and return the sorted buckets.

  • Step 7 − Start the main() function. Inside the main() initialize an array to be sorted. Call the bucketSort() function and pass the array to be sorted as argument to it.

  • Step 8 − Store the sorted array in a variable and print it on the screen by using fmt.Println() function.

Example 1

In this Example we will write a go language program to sort an array by using the simple bucket sort.

This method works by dividing the elements into a fixed number of buckets, each containing a range of values.

package main

import (
   "fmt"
)

func bucketSort(arr []int) []int {
   maxVal := 0
   for _, val := range arr {
      if val > maxVal {
         maxVal = val
      }
   }

   buckets := make([][]int, maxVal+1)
   for i := range buckets {
      buckets[i] = make([]int, 0)
   }

   for _, val := range arr {
      buckets[val] = append(buckets[val], val)
   }

   result := make([]int, 0)
   for _, bucket := range buckets {
      result = append(result, bucket...)
   }

   return result
}

func main() {
   arr := []int{67, 32, 12, 54, 43, 57}
   fmt.Println("The given unsorted array is:", arr)
   sortedArr := bucketSort(arr)
   fmt.Println("The obtained sorted array is:", sortedArr)
}

Output

The given unsorted array is: [67 32 12 54 43 57]
The obtained sorted array is: [12 32 43 54 57 67]

Example 2

In this Example we will write a go language program to implement the bucket sort along with custom sorting Algorithm.

package main

import (
   "fmt"
)

func bucketSortCustom(arr []float64) []float64 {
   buckets := make([][]float64, len(arr))
   for i := range buckets {
      buckets[i] = make([]float64, 0)
   }

   for _, val := range arr {
      index := int(val * float64(len(arr)))
      buckets[index] = append(buckets[index], val)
   }

   for _, bucket := range buckets {
      for i := 1; i < len(bucket); i++ {
         j := i
         for j > 0 && bucket[j-1] > bucket[j] {
            bucket[j-1], bucket[j] = bucket[j], bucket[j-1]
            j--
         }
      }
   }

   result := make([]float64, 0)
   for _, bucket := range buckets {
      result = append(result, bucket...)
   }
   return result
}

func main() {
   arr := []float64{0.42, 0.32, 0.67, 0.89, 0.12, 0.57}
   fmt.Println("Original array:", arr)
   sortedArr := bucketSortCustom(arr)
   fmt.Println("Sorted array:", sortedArr)
}

Output

Original array: [0.42 0.32 0.67 0.89 0.12 0.57]
Sorted array: [0.12 0.32 0.42 0.57 0.67 0.89]

Conclusion

In this article we have successfully compiled and executed a go language program to to implement the bucket sort Algorithm. Here we have used two programs. in the first program we are using the simple bucket sort while in the other we are using bucket sort with custom sorting to implement our result.

Updated on: 05-Apr-2023

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements