91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
上栗县| 文成县| 岐山县| 西峡县| 炎陵县| 金乡县| 壤塘县| 连江县| 苏尼特右旗| 板桥市| 新津县| 南投市| 新巴尔虎右旗| 敖汉旗| 儋州市| 镇赉县| 蒲城县| 宁都县| 正宁县| 柳河县| 张家港市| 太白县| 靖远县| 磐石市| 蓬莱市| 三明市| 扎赉特旗| 滕州市| 馆陶县| 汤原县| 北票市| 班玛县| 惠州市| 长沙市| 连云港市| 新河县| 汝城县| 潜山县| 屏边| 永新县| 浪卡子县|