Comb sort program in C#
Comb sort is a comparison-based sorting algorithm that improves upon the bubble sort algorithm. It works by repeatedly comparing and swapping adjacent elements with a fixed gap size and gradually reducing the gap until it reaches 1. Comb sort has a time complexity of O(n^2), but with the right gap sequence, it can have better average-case performance than bubble sort.
Here's an explanation of the comb sort algorithm step by step:
-
Choose a shrink factor and gap: The shrink factor determines the amount by which the gap is reduced on each iteration. Common shrink factors include 1.3 and 1.4. The initial gap size is typically set to the length of the array.
-
Compare and swap elements with a fixed gap: Starting from the first element, compare it with the element at a fixed gap distance. If they are in the wrong order, swap them. Repeat this process for all elements in the array, using the same fixed gap.
-
Reduce the gap: After comparing and swapping elements with a fixed gap, reduce the gap size by multiplying it with the shrink factor. If the gap becomes 1, the algorithm proceeds to the next step.
-
Repeat until sorted: Repeat steps 2 and 3 until the gap becomes 1. At this point, the algorithm performs a final pass using a gap of 1, which is essentially a regular bubble sort. This final pass ensures that any remaining small inversions are sorted.
Here's an example implementation of comb sort in C#:
using System;
public class CombSort
{
public static void Main()
{
int[] arr = { 64, 25, 12, 22, 11 };
Console.WriteLine("Original array:");
PrintArray(arr);
Sort(arr);
Console.WriteLine("Sorted array:");
PrintArray(arr);
}
public static void Sort(int[] arr)
{
int n = arr.Length;
int gap = n;
double shrinkFactor = 1.3;
bool swapped = true;
while (gap > 1 || swapped)
{
// Update the gap size
gap = (int)(gap / shrinkFactor);
if (gap < 1)
{
gap = 1;
}
swapped = false;
// Compare and swap elements with a fixed gap
for (int i = 0; i + gap < n; i++)
{
if (arr[i] > arr[i + gap])
{
Swap(arr, i, i + gap);
swapped = true;
}
}
}
}
public static void Swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void PrintArray(int[] arr)
{
foreach (int num in arr)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
}
In this example, the Sort method implements the comb sort algorithm. The Swap method is used to swap elements in the array. The Main method initializes an array, calls the Sort method to sort the array, and then prints both the original and sorted arrays using the PrintArray method.
When you run the program, it will output:
Original array:
64 25 12 22 11
Sorted array:
11 12 22 25 64
The program demonstrates the step-by-step execution of the comb sort algorithm on the given array and outputs the sorted array.
Comb sort improves upon bubble sort by allowing elements to move more quickly towards their correct positions. The choice of shrink factor affects the algorithm's performance, and a smaller shrink factor typically results in better average-case performance. Comb sort is not as widely used as other sorting algorithms like quicksort or mergesort but can be a good alternative to bubble sort in certain scenarios.