中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

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
洛南县| 阜新| 施甸县| 凌源市| 永定县| 云浮市| 美姑县| 阳朔县| 溧阳市| 阿合奇县| 郁南县| 宁安市| 岗巴县| 宜丰县| 称多县| 辽宁省| 邛崃市| 徐水县| 泰来县| 蓝山县| 南郑县| 商都县| 白城市| 夹江县| 岳普湖县| 白山市| 南通市| 崇明县| 德兴市| 沛县| 丹东市| 宁武县| 五寨县| 成安县| 宜君县| 上林县| 涞源县| 木里| 固始县| 怀柔区| 乳山市|