Golang Program to Perform the Comb Sort Algorithm


Comb sort algorithm is a simple and efficient comparison−based sorting algorithm, Comb Sort improves upon Bubble Sort by eliminating small values towards the end of the array, resulting in faster sorting times. We can use two methods to implement Comb Sort algorithm in Gzolang, the first method is using a naive approach and the other one is using an optimized approach in which we enhance the efficiency of the algorithm by introducing a flag variable to track swaps. In this article, we will discuss the principle of comb sort algorithm and provide the syntax for the program.

Explanation

The Comb Sort algorithm in Golang is like a smarter version of Bubble Sort. It's all about making the sorting process faster by dealing with small values first. Imagine you have an array of numbers that are all jumbled up. Comb Sort helps to straighten them out efficiently.

Here's how it works: Imagine you have a comb, and you're running it through your tangled hair. The gaps between the comb's teeth get smaller and smaller. Similarly, in Comb Sort, we start with a big gap between elements and gradually reduce it. As we do this, small values that were stuck in the wrong places get sorted out quickly.

Before sorting: [9 5 1 8 3 7 4 2 6]

After sorting: [1 2 3 4 5 6 7 8 9]

Algorithm

  • Start with a gap value initialised as the length of the array. Repeat the following steps until the gap is greater than 1:

  • Set the gap value as the floor division of (gap / shrinkFactor), where shrinkFactor is typically set to 1.3. Iterate through the array from index 0 to (length − gap):

  • Compare the element at the current index with the element at (current index + gap). If the element at the current index is greater than the element at (current index + gap), swap the two elements. Continue iterating until the gap becomes 1.

  • Perform a final pass through the array using the Bubble Sort algorithm to ensure any remaining small values are correctly placed.

Syntax

 func combSort(arr []int)

The syntax func combSort(arr []int) defines a function named combSort that takes an integer slice arr as input. This function implements the Comb Sort algorithm using a straightforward and iterative approach to sort the array.

func combSortOptimized(arr []int)

The syntax func combSortOptimized(arr []int) declares a function named combSortOptimized that accepts an integer slice arr as an argument. This function implements the Comb Sort algorithm using an optimised approach, enhancing the sorting process by tracking swaps and optimising the gap reduction for improved performance.

func bubbleSort(arr []int) 

The syntax defines a function named bubbleSort, which takes an input parameter arr, representing a slice of integers. The function's purpose is to sort the elements of the input slice using the bubble sort algorithm, arranging them in ascending order. After the function execution, the original slice arr will be modified to hold the sorted elements.

Example

In this example, we have an unsorted array arr with elements [9, 5, 1, 8, 3, 7, 4, 2, 6]. The combSort function is called to sort the array using the naive approach of Comb Sort. After sorting, the elements are rearranged in ascending order, and the sorted array is printed as the output. While this example is easy to understand, it may not provide optimal performance for larger datasets.

package main

import "fmt"

func combSort(arr []int) {
	length := len(arr)
	gap := length
	shrinkFactor := 1.3
	swapped := true

	for gap > 1 || swapped {
		gap = int(float64(gap) / shrinkFactor)
		if gap < 1 {
			gap = 1
		}

		swapped = false
		for i := 0; i < length-gap; i++ {
			if arr[i] > arr[i+gap] {
				arr[i], arr[i+gap] = arr[i+gap], arr[i]
				swapped = true
			}
		}
	}

	for i := 0; i < length-1; i++ {
		for j := 0; j < length-i-1; j++ {
			if arr[j] > arr[j+1] {
				arr[j], arr[j+1] = arr[j+1], arr[j]
			}
		}
	}
}

func main() {
	arr := []int{9, 5, 1, 8, 3, 7, 4, 2, 6}
	fmt.Println("Before sorting:", arr)

	combSort(arr)
	fmt.Println("After sorting:", arr)
}

Output

Before sorting: [9 5 1 8 3 7 4 2 6]
After sorting: [1 2 3 4 5 6 7 8 9]

Example

In this example, we will implement Comb Sort algorithm in golang and enhance the efficiency of the algorithm by introducing a flag variable to track swaps and exiting the loop early if no swaps occur, we improve the sorting process. Additionally, a final pass with Bubble Sort ensures that any remaining small values are correctly placed, here we start with an unsorted array arr with elements [9, 5, 1, 8, 3, 7, 4, 2, 6]. The combSortOptimized function is called to sort the array using the optimised approach of Comb Sort. After sorting, the elements are rearranged in ascending order, and the sorted array is printed as the output.

package main

import "fmt"

func combSortOptimized(arr []int) {
	length := len(arr)
	gap := length
	shrinkFactor := 1.3
	swapped := true

	for gap > 1 || swapped {
		gap = int(float64(gap) / shrinkFactor)
		if gap < 1 {
			gap = 1
		}

		swapped = false
		for i := 0; i < length-gap; i++ {
			if arr[i] > arr[i+gap] {
				arr[i], arr[i+gap] = arr[i+gap], arr[i]
				swapped = true
			}
		}
	}

	bubbleSort(arr)
}

func bubbleSort(arr []int) {
	length := len(arr)
	for i := 0; i < length-1; i++ {
		for j := 0; j < length-i-1; j++ {
			if arr[j] > arr[j+1] {
				arr[j], arr[j+1] = arr[j+1], arr[j]
			}
		}
	}
}

func main() {
	arr := []int{9, 5, 1, 8, 3, 7, 4, 2, 6}
	fmt.Println("Before sorting:", arr)

	combSortOptimized(arr)
	fmt.Println("After sorting:", arr)
}

Output

Before sorting: [9 5 1 8 3 7 4 2 6]
After sorting: [1 2 3 4 5 6 7 8 9]

Real life implementation

Network Traffic Optimization

In network traffic management systems, the Comb Sort algorithm can be employed to optimize the transmission of data packets. By sorting incoming packets based on priority or size, network congestion can be reduced, leading to more efficient data transfer and improved overall network performance.

File System Maintenance

File systems often require sorting files based on attributes such as creation date, file size, or file type. The Comb Sort algorithm can be used to rearrange files efficiently, ensuring smoother access and management. This is particularly useful in operating systems and file organization utilities.

Conclusion

The Comb Sort algorithm in Golang offers an efficient sorting solution by eliminating small values and optimising the gap reduction. In this article we have looked at how we can implement Comb Sort algorithm in golang, we have explored two methods, in the first method we start with a large gap value and progressively reduce it, we compare and swap elements based on their order until the gap becomes 1, completing the sorting process, and the second method is called optimised approach in which we significantly enhance the efficiency of the algorithm.

Updated on: 07-Sep-2023

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements