如何制作快速排序十进制数C#

时间:2016-04-20 15:24:51

标签: c# sorting

这是一个快速排序程序,它从文件中读取数字,然后将它们从最小到最高排序。我需要它能够完全相同,但对于带小数的数字。我知道有一些方法可以将int转换为double,decimal或float,但是我已经尝试了所有我无能为力的工作,有人可以告诉我他们将如何更改此代码以使其工作。以下是我尝试这样做的方法:

class quickSort
{

private double[] array = new double[1010];
private int len;

public void QuickSort()
{
    sort(0, len - 1);
}

public void sort(double left, double right)
{
    double pivot;
    double leftend, rightend;

    leftend = left;
    rightend = right;
    pivot = array[left];

    while (left < right)
    {
        while ((array[right] >= pivot) && (left < right))
        {
            right--;
        }

        if (left != right)
        {
            array[left] = array[right];
            left++;
        }

        while ((array[left] <= pivot) && (left < right))
        {
            left++;
        }

        if (left != right)
        {
            array[right] = array[left];
            right--;
        }
    }

    array[left] = pivot;
    pivot = left;
    left = leftend;
    right = rightend;

    if (left < pivot)
    {
        sort(left, pivot - 1);
    }

    if (right > pivot)
    {
        sort(pivot + 1, right);
    }
}

public static void Main()
{
    quickSort q_Sort = new quickSort();

    string[] years = System.IO.File.ReadAllLines(@"C:\WS1_Rain.txt");
    var yearArray = years.Select(item => Convert.ToDouble(item));
    double[] array = yearArray.ToArray();

    q_Sort.array = array;
    q_Sort.len = q_Sort.array.Length;
    q_Sort.QuickSort();

    for (int j = 0; j < q_Sort.len; j++)
    {
        Console.WriteLine(q_Sort.array[j]);
    }
    Console.ReadKey();
}
  }
}

1 个答案:

答案 0 :(得分:1)

如果您的最终目标是纯粹对数组进行排序,那么您可以使用Linq!

string[] years = System.IO.File.ReadAllLines(@"C:\WS1_Rain.txt");
            var yearArray = years.Select(item => Convert.ToDouble(item));
            double[] array = yearArray.ToArray();

            var sorted = array.OrderBy(a => a);

但是,如果您希望修改QuickSort类,则只需对原始代码进行最少的更改即可。只有阵列才需要支持双打。用于管理索引的支持变量可以保持整数:

class quickSort
    {
        private double[] array = new double[1010];

        private int len;

        public void QuickSort()
        {
            sort(0, len - 1);
        }

        public void sort(int left, int right)
        {
            double pivot;
            int leftend, rightend;

            leftend = (int)left;
            rightend = (int)right;
            pivot = array[left];

            while (left < right)
            {
                while ((array[right] >= pivot) && (left < right))
                {
                    right--;
                }

                if (left != right)
                {
                    array[left] = array[right];
                    left++;
                }

                while ((array[left] <= pivot) && (left < right))
                {
                    left++;
                }

                if (left != right)
                {
                    array[right] = array[left];
                    right--;
                }
            }

            array[left] = pivot;
            pivot = left;
            left = leftend;
            right = rightend;

            if (left < pivot)
            {
                sort(left, Convert.ToInt32(pivot - 1));
            }

            if (right > pivot)
            {
                sort(Convert.ToInt32(pivot + 1), right);
            }
        }

        static void Main(string[] args)
        {
            quickSort q_Sort = new quickSort();

            string[] years = System.IO.File.ReadAllLines(@"C:\WS1_Rain.txt");
            var yearArray = years.Select(item => Convert.ToDouble(item));
            double[] array = yearArray.ToArray();

            var sorted = array.OrderBy(a => a);

            q_Sort.array = array;
            q_Sort.len = q_Sort.array.Length;
            q_Sort.QuickSort();

            for (int j = 0; j < q_Sort.len; j++)
            {
                Console.WriteLine(q_Sort.array[j]);
            }

            Console.ReadKey();
        }
    }