C#quickSort随机支点

时间:2010-10-30 22:16:22

标签: c# random quicksort

我正在尝试将heapSort算法修改为带有随机数据的版本,我不知道该怎么做。

有人可以帮助我吗?

这是代码:

//QuickSort w C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuickSort
{
    class Program
    {
        //Zamienia miejscami , zwykły swap
        static void Swap<T>(ref T lhs, ref T rhs)
        {
            T temp;
            temp = lhs;
            lhs = rhs;
            rhs = temp;
        }

        //Procedura qiucksort
        static void qsort(int[] tab, int left, int right)
        {
            if (left < right)
            {    
                int m = left;

                for (int i = left + 1; i <= right; i++)
                    if (tab[i] < tab[left])
                        Swap(ref tab[++m], ref tab[i]);

                Swap(ref tab[left], ref tab[m]);

                qsort(tab, left, m - 1);
                qsort(tab, m + 1, right);
            }       
        }


        static void Main(string[] args)
        {
            int[] a = { 0, 12, 34, 9, 54, 12, 77, -3, -20 };
            int i;
            int left = 0;
            int right = 8;

            Console.WriteLine("Data before sort ");
            for (i = 0; i < a.Length; i++)
                Console.Write(" {0} ", a[i]);
            Console.WriteLine();
            //Wywołanie procedury qsort
            qsort(a, left, right);

            Console.WriteLine("Data after sort");
            for (i = 0; i < a.Length; i++)
                Console.Write(" {0} ", a[i]);
            Console.WriteLine();
            Console.ReadLine();            
        }
    }
}

这是使用随机数据透视的更改代码,此代码崩溃在: Swap(ref tab[++m], ref tab[i]);


//QuickSort in C# with random pivot
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuickSort { class Program { //common Swap function static void Swap(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; }

    //qiucksort procedure
    static void qsort(int[] tab, int left, int right)
    {
        if (left < right)
        {

            System.Random myRandom = new System.Random(); //Creating instance of random variable

            int m = myRandom.Next(left, right); //pivot = random number between left a right
           Swap(ref tab[left],ref tab[m]);

            for (int i = left + 1; i <= right; i++)
                if (tab[i] < tab[left])
                    Swap(ref tab[++m], ref tab[i]);

            Swap(ref tab[left], ref tab[m]);

            qsort(tab, left, m - 1);
            qsort(tab, m + 1, right);
        }


    }






    static void Main(string[] args)
    {
        Console.Title = "QuickSort";
        int[] a = { 0, 12, 34, 9, 54, 12, 77, -3};
        int i;
        int left = 0;
        int right = 7;


        Console.WriteLine("Data before sort ");
        for (i = 0; i < a.Length; i++)
            Console.Write(" {0} ", a[i]);
        Console.WriteLine();
        //call quicksort procedure
        qsort(a, left, right);

        Console.WriteLine("Data after sort");
        for (i = 0; i < a.Length; i++)
            Console.Write(" {0} ", a[i]);
        Console.WriteLine();
        Console.ReadLine();



    }
}

}

1 个答案:

答案 0 :(得分:2)

您当前的轴是第一个元素,您可以使用

选择它
 int m = left;

首先使用随机数据透视

  • 选择[左,右],
  • 范围内的随机索引p
  • 交换标签[左]和标签[p]

因为否则你必须彻底改变你的分割算法(不幸地是内联的)。

但是

随机枢轴远非理想。如果这是中途严重,请考虑三个中位数