亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

c#快速排序的遞歸與非遞歸形式

c#
小樊
82
2024-06-25 23:41:54
欄目: 編程語言

遞歸形式的C#快速排序算法:

using System;

class QuickSort
{
    public static void Sort(int[] arr, int low, int high)
    {
        if (low < high)
        {
            int pivot = Partition(arr, low, high);
            Sort(arr, low, pivot - 1);
            Sort(arr, pivot + 1, high);
        }
    }

    public 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++;
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        int temp1 = arr[i + 1];
        arr[i + 1] = arr[high];
        arr[high] = temp1;

        return i + 1;
    }

    public static void PrintArray(int[] arr)
    {
        foreach (int num in arr)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }

    public static void Main()
    {
        int[] arr = { 12, 11, 13, 5, 6, 7 };
        int n = arr.Length;

        Console.WriteLine("Original array:");
        PrintArray(arr);

        Sort(arr, 0, n - 1);

        Console.WriteLine("Sorted array:");
        PrintArray(arr);
    }
}

非遞歸形式的C#快速排序算法:

using System;
using System.Collections.Generic;

class QuickSort
{
    public static void Sort(int[] arr, int low, int high)
    {
        Stack<int> stack = new Stack<int>();
        stack.Push(low);
        stack.Push(high);

        while (stack.Count > 0)
        {
            high = stack.Pop();
            low = stack.Pop();

            int pivot = Partition(arr, low, high);

            if (pivot - 1 > low)
            {
                stack.Push(low);
                stack.Push(pivot - 1);
            }

            if (pivot + 1 < high)
            {
                stack.Push(pivot + 1);
                stack.Push(high);
            }
        }
    }

    public 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++;
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        int temp1 = arr[i + 1];
        arr[i + 1] = arr[high];
        arr[high] = temp1;

        return i + 1;
    }

    public static void PrintArray(int[] arr)
    {
        foreach (int num in arr)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }

    public static void Main()
    {
        int[] arr = { 12, 11, 13, 5, 6, 7 };
        int n = arr.Length;

        Console.WriteLine("Original array:");
        PrintArray(arr);

        Sort(arr, 0, n - 1);

        Console.WriteLine("Sorted array:");
        PrintArray(arr);
    }
}

0
沁源县| 阿拉善左旗| 高州市| 玛纳斯县| 犍为县| 新邵县| 大厂| 连云港市| 阜康市| 镇赉县| 朝阳县| 洛扎县| 漾濞| 嘉荫县| 桂阳县| 宜良县| 广宗县| 土默特左旗| 双牌县| 炉霍县| 吐鲁番市| 奇台县| 曲靖市| 兴义市| 兴山县| 怀化市| 贵溪市| 突泉县| 定日县| 化州市| 鄂托克旗| 兰州市| 南溪县| 东至县| 吉隆县| 封丘县| 阳城县| 尉氏县| 英吉沙县| 抚顺县| 修武县|