简体   繁体   English

C#quickSort随机支点

[英]C# quickSort with random pivot

I am trying to modify the heapSort algorithm into version with random pivot , and I dont know what to do. 我试图将heapSort算法修改为带有随机数据的版本,我不知道该怎么做。

Could any one help me ? 任何人都可以帮助我吗?

This is the code: 这是代码:

//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();            
        }
    }
}

This is changed code with random pivot , this code crashes at: Swap(ref tab[++m], ref tab[i]); 这是使用随机数据转换的代码,此代码崩溃在: 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();



    }
}

}

Your current pivot is the first element, you select it with 您当前的枢轴是第一个元素,您可以选择

 int m = left;

To use a random pivot, first 首先使用随机数据

  • select a random index p in the range [left, right] , 选择[left,right]范围内的随机索引p,
  • swap tab[left] and tab[p] 交换选项卡[左]和选项卡[p]

Because otherwise you would have to change your split algorithm (which is inline unfortunately) drastically. 因为否则你将不得不彻底改变你的拆分算法(不幸的是内联)。

But

A random pivot is far from ideal. 随意的支点远非理想。 If this is halfway serious, consider a median-of-three pivot 如果这是中途严重,请考虑三个中位数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM