当前位置: 首页 > news >正文

成品网站w灬源码16伊园中山哪家做网站的好

成品网站w灬源码16伊园,中山哪家做网站的好,扁平 网站模板,梅州网站开发以下是常见的十大排序算法(按照学习和实现的顺序排列): 冒泡排序(Bubble Sort)选择排序(Selection Sort)插入排序(Insertion Sort)希尔排序(Shell Sort&…

以下是常见的十大排序算法(按照学习和实现的顺序排列):

  1. 冒泡排序(Bubble Sort)
  2. 选择排序(Selection Sort)
  3. 插入排序(Insertion Sort)
  4. 希尔排序(Shell Sort)
  5. 归并排序(Merge Sort)
  6. 快速排序(Quick Sort)
  7. 堆排序(Heap Sort)
  8. 计数排序(Counting Sort)
  9. 桶排序(Bucket Sort)
  10. 基数排序(Radix Sort)

这些排序算法具有不同的时间复杂度、空间复杂度和稳定性,适用于不同的排序场景。每种算法都有其独特的思想和实现方式,您可以根据具体的需求选择适合的排序算法。

C#实现的十大排序算法的示例代码如下:

1、冒泡排序(Bubble Sort):
class BubbleSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = 0; j < n - i - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
2、选择排序(Selection Sort):
class SelectionSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;

        for (int i = 0; i < n - 1; i++)
        {
            int minIndex = i;
            for (int j = i + 1; j < n; j++)
            {
                if (arr[j] < arr[minIndex])
                {
                    minIndex = j;
                }
            }

            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
}
3、插入排序(Insertion Sort):
class InsertionSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;
        for (int i = 1; i < n; ++i)
        {
            int key = arr[i];
            int j = i - 1;

            while (j >= 0 && arr[j] > key)
            {
                arr[j + 1] = arr[j];
                j = j - 1;
            }

            arr[j + 1] = key;
        }
    }
}
4、希尔排序(Shell Sort):
class ShellSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;

        for (int gap = n / 2; gap > 0; gap /= 2)
        {
            for (int i = gap; i < n; i++)
            {
                int temp = arr[i];
                int j;
                for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
                {
                    arr[j] = arr[j - gap];
                }
                arr[j] = temp;
            }
        }
    }
}
5、归并排序(Merge Sort):
class MergeSort
{
    public static void Sort(int[] arr)
    {
        if (arr.Length <= 1)
            return;

        int mid = arr.Length / 2;
        int[] leftArr = new int[mid];
        int[] rightArr = new int[arr.Length - mid];

        for (int i = 0; i < mid; i++)
        {
            leftArr[i] = arr[i];
        }

        for (int i = mid; i < arr.Length; i++)
        {
            rightArr[i - mid] = arr[i];
        }

        Sort(leftArr);
        Sort(rightArr);
        Merge(leftArr, rightArr, arr);
    }

    private static void Merge(int[] leftArr, int[] rightArr, int[] arr)
    {
        int leftIndex = 0;
        int rightIndex = 0;
        int current = 0;

        while (leftIndex < leftArr.Length && rightIndex < rightArr.Length)
        {
            if (leftArr[leftIndex] <= rightArr[rightIndex])
            {
                arr[current] = leftArr[leftIndex];
                leftIndex++;
            }
            else
            {
                arr[current] = rightArr[rightIndex];
                rightIndex++;
            }
            current++;
        }

        while (leftIndex < leftArr.Length)
        {
            arr[current] = leftArr[leftIndex];
            leftIndex++;
            current++;
        }

        while (rightIndex < rightArr.Length)
        {
            arr[current] = rightArr[rightIndex];
            rightIndex++;
            current++;
        }
    }
}
6、快速排序(Quick Sort):
class QuickSort
{
    public static void Sort(int[] arr, int low, int high)
    {
        if (low < high)
        {
            int pivotIndex = Partition(arr, low, high);
            Sort(arr, low, pivotIndex - 1);
            Sort(arr, pivotIndex + 1, high);
        }
    }

    private static int Partition(int[] arr, int low, int high)
    {
        int pivot = arr[high];
        int i = low - 1;

        for (int j = low; j < high; j++)
        {
            if (arr[j] < pivot)
            {
                i++;
                Swap(arr, i, j);
            }
        }

        Swap(arr, i + 1, high);
        return i + 1;
    }

    private static void Swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
7、堆排序(Heap Sort):
class HeapSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;

        for (int i = n / 2 - 1; i >= 0; i--)
        {
            Heapify(arr, n, i);
        }

        for (int i = n - 1; i > 0; i--)
        {
            Swap(arr, 0, i);
            Heapify(arr, i, 0);
        }
    }

    private static void Heapify(int[] arr, int n, int i)
    {
        int largest = i;
        int left = 2 * i + 1;
        int right = 2 * i + 2;

        if (left < n && arr[left] > arr[largest])
        {
            largest = left;
        }

        if (right < n && arr[right] > arr[largest])
        {
            largest = right;
        }

        if (largest != i)
        {
            Swap(arr, i, largest);
            Heapify(arr, n, largest);
        }
    }

    private static void Swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
8、计数排序(Counting Sort):
class CountingSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;
        int[] output = new int[n];

        int max = arr[0];
        for (int i = 1; i < n; i++)
        {
            if (arr[i] > max)
            {
                max = arr[i];
            }
        }

        int[] count = new int[max + 1];

        for (int i = 0; i < n; i++)
        {
            count[arr[i]]++;
        }

        for (int i = 1; i <= max; i++)
        {
            count[i] += count[i - 1];
        }

        for (int i = n - 1; i >= 0; i--)
        {
            output[count[arr[i]] - 1] = arr[i];
            count[arr[i]]--;
        }

        for (int i = 0; i < n; i++)
        {
            arr[i] = output[i];
        }
    }
}

9、桶排序(Bucket Sort)
using System;
using System.Collections.Generic;

class BucketSort
{
    public static void Sort(int[] arr)
    {
        int minValue = arr[0];
        int maxValue = arr[0];

        for (int i = 1; i < arr.Length; i++)
        {
            if (arr[i] < minValue)
                minValue = arr[i];
            else if (arr[i] > maxValue)
                maxValue = arr[i];
        }

        int bucketSize = maxValue - minValue + 1;
        List<int>[] buckets = new List<int>[bucketSize];

        for (int i = 0; i < bucketSize; i++)
            buckets[i] = new List<int>();

        for (int i = 0; i < arr.Length; i++)
            buckets[arr[i] - minValue].Add(arr[i]);

        int index = 0;
        for (int i = 0; i < bucketSize; i++)
        {
            int[] temp = buckets[i].ToArray();
            if (temp.Length > 0)
            {
                Array.Sort(temp);
                for (int j = 0; j < temp.Length; j++)
                {
                    arr[index] = temp[j];
                    index++;
                }
            }
        }
    }

    static void Main(string[] args)
    {
        int[] arr = { 4, 2, 7, 1, 9, 5, 3, 6, 8 };
        Console.WriteLine("Before sorting:");
        foreach (int element in arr)
            Console.Write(element + " ");

        Sort(arr);
        
        Console.WriteLine("\n\nAfter sorting:");
        foreach (int element in arr)
            Console.Write(element + " ");
        
        Console.ReadLine();
    }
}
10、基数排序(Radix Sort)
using System;

class RadixSort
{
    public static void Sort(int[] arr)
    {
        int max = FindMax(arr);

        for (int exp = 1; max / exp > 0; exp *= 10)
            CountSort(arr, exp);
    }

    public static void CountSort(int[] arr, int exp)
    {
        int n = arr.Length;
        int[] output = new int[n];
        int[] count = new int[10];

        for (int i = 0; i < 10; i++)
            count[i] = 0;

        for (int i = 0; i < n; i++)
            count[(arr[i] / exp) % 10]++;

        for (int i = 1; i < 10; i++)
            count[i] += count[i - 1];

        for (int i = n - 1; i >= 0; i--)
        {
            output[count[(arr[i] / exp) % 10] - 1] = arr[i];
            count[(arr[i] / exp) % 10]--;
        }

        for (int i = 0; i < n; i++)
            arr[i] = output[i];
    }

    public static int FindMax(int[] arr)
    {
        int max = arr[0];
        for (int i = 1; i < arr.Length; i++)
        {
            if (arr[i] > max)
                max = arr[i];
        }
        return max;
    }

    static void Main(string[] args)
    {
        int[] arr = { 170, 45, 75, 90, 802, 24, 2, 66 };
        Console.WriteLine("Before sorting:");
        foreach (int element in arr)
            Console.Write(element + " ");

        Sort(arr);

        Console.WriteLine("\n\nAfter sorting:");
        foreach (int element in arr)
            Console.Write(element + " ");

        Console.ReadLine();
    }
}

        以上代码分别实现了10大算法。请注意,如果需要对其他类型的数据进行排序,需要进行相应的修改。

http://www.15wanjia.com/news/188050.html

相关文章:

  • 百度不到公司网站wordpress滑动门短代码
  • 做网站博客怎么推广wordpress PHP合并js
  • 婚庆网站策划免费做明信片的网站
  • 如何用excel做网站wordpress 域名更改 页面链接
  • 好的移动端网站模板下载线上营销和线下营销的区别
  • 做php网站用什么软件好安徽seo网站
  • 网站 栏目 英语技术支持 张家港网站建设
  • 太原网站建设外包价格医药销售网站开发背景
  • 过界女主个人做网站的头条网站怎么做的
  • 做网站一定要域名嘛新开传奇最大网站999
  • 网站建设对接流程图网站没后台怎么修改类容
  • 广州市建设工程造价站网站商务网站开发综合实训
  • 网站制作的英文邢台网站开发培训学校
  • 郑州网站模板哪里有阳江房产网
  • 静态网站模板下载高端企业门户网站建设
  • wordpress比较慢网站站seo教程
  • app与微网站的区别是什么意思王烨画家简历
  • 威海城市 建设信息网站货车拆车件网上商城
  • 网站关键词快速排名工具福州有网站建设的公司排名
  • 中国哪家网站做仿古做的好网站开发 明细
  • 哪些网站有设计缺点免费建设旅游网站
  • 西安高端网站制作公司做旅行网站多少钱
  • 给企业开发网站北京响应式网站制作公司
  • 北京建设商业网站网站建设工作情况汇报
  • 苏州住房和城乡建设局网站wordpress mip
  • 抖音上做我女朋友网站怎么改wordpress的html5
  • 服装电子商务的网站建设mine-video wordpress
  • 在哪家网站做外贸比较好网时 网站服务器租赁
  • 邯郸做网站询安联网络有机大米网站建设方案
  • 珠宝网站建商台北电商网站推广