Home > Backend Development > C#.Net Tutorial > How to implement the bucket sort algorithm in C#

How to implement the bucket sort algorithm in C#

WBOY
Release: 2023-09-19 11:24:33
Original
1383 people have browsed it

How to implement the bucket sort algorithm in C#

How to implement the bucket sort algorithm in C

#Bucket Sort is a sorting algorithm that divides the elements to be sorted into different categories according to their size. buckets, and each bucket is sorted separately. Then merge the elements in each bucket together in order to get an ordered result. The time complexity of bucket sorting is O(n), and in some specific cases, it can even achieve the efficiency of linear sorting.

The following will introduce how to implement the bucket sorting algorithm in C#, and give specific code examples:

using System;
using System.Collections.Generic;

class BucketSort
{
    /// <summary>
    /// 桶排序算法实现
    /// </summary>
    /// <param name="data">待排序的数组</param>
    public static void Sort(double[] data)
    {
        if (data == null || data.Length <= 1)
        {
            return;
        }

        int bucketCount = data.Length;
        List<double>[] buckets = new List<double>[bucketCount];
        for (int i = 0; i < bucketCount; i++)
        {
            buckets[i] = new List<double>();
        }

        // 将数据分配到各个桶中
        for (int i = 0; i < data.Length; i++)
        {
            int bucketIndex = (int)(data[i] * bucketCount);
            buckets[bucketIndex].Add(data[i]);
        }

        // 对每个桶中的数据进行插入排序
        for (int i = 0; i < bucketCount; i++)
        {
            InsertionSort(buckets[i]);
        }

        // 合并各个有序桶中的数据
        int dataIndex = 0;
        for (int i = 0; i < bucketCount; i++)
        {
            for (int j = 0; j < buckets[i].Count; j++)
            {
                data[dataIndex++] = buckets[i][j];
            }
        }
    }

    /// <summary>
    /// 插入排序算法实现
    /// </summary>
    /// <param name="data">待排序的数组</param>
    private static void InsertionSort(List<double> data)
    {
        for (int i = 1; i < data.Count; i++)
        {
            double temp = data[i];
            int j = i - 1;
            while (j >= 0 && data[j] > temp)
            {
                data[j + 1] = data[j];
                j--;
            }
            data[j + 1] = temp;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        double[] data = { 0.5, 0.2, 0.8, 0.3, 0.6, 0.1, 0.9, 0.7, 0.4 };
        Console.WriteLine("原始数组:");
        PrintData(data);
        BucketSort.Sort(data);
        Console.WriteLine("排序后的数组:");
        PrintData(data);
    }

    /// <summary>
    /// 打印数组元素
    /// </summary>
    /// <param name="data">待打印的数组</param>
    private static void PrintData(double[] data)
    {
        foreach (var item in data)
        {
            Console.Write(item + " ");
        }
        Console.WriteLine();
    }
}
Copy after login

The above is the sample code for implementing the bucket sorting algorithm in C#. In the main function, an array containing some random decimals is created and sorted using the BucketSort.Sort method. Finally, the sorted array is output to the console by calling the PrintData function.

By running the above code, you will get output similar to the following:

原始数组:
0.5 0.2 0.8 0.3 0.6 0.1 0.9 0.7 0.4
排序后的数组:
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Copy after login

Bucket sorting is an efficient sorting algorithm, especially suitable for situations where the distribution of data elements is relatively even. Through the above code examples, you can learn how to implement the bucket sorting algorithm in C#, and you can use bucket sorting to perform sorting operations in your own projects. At the same time, you can also modify and expand the code according to actual needs to meet specific sorting needs.

The above is the detailed content of How to implement the bucket sort algorithm in C#. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template