快速排序无法运行C# [英] Quick Sort won't run C#

查看:111
本文介绍了快速排序无法运行C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的QuickSort排序十进制数字而不仅仅是整数.下面是我读取文件的代码,我认为这是问题所在.我该如何解决?

I need my QuickSort to sort decimal numbers not just whole numbers.Below is my code where the file is read, i think this is the issue. How do i fix this?

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

        string[] years = System.IO.File.ReadAllLines(@"C:\WS1_Rain.txt");
IEnumerable<int> yearArray = years.Select(item => int.Parse(item));
int[] 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();

推荐答案

在C#中,int仅存储整数,而doublefloat可以存储整数和浮点数.

In C#, int stores only integers and double or float can store both integers and floating point numbers.

如果您希望程序既可以读取浮点数又可以读取整数,则应使用double甚至decimal(如果希望数字精确),但我认为不存在需要的.我将使用double

If you want your program to be able to read both floating point numbers and integers, you should use double, or even decimal if you want the numbers to be precise, but I don't think there is a need for that. I'll use double

这基本上是您需要做的,

This is basically what you need to do,

  • 将数组更改为双精度数组
  • sort方法的变量更改为double类型
  • 使用Convert.ToDouble代替int.Parse
  • change the array to an array of doubles
  • change the variables of the sort method to be of type double
  • use Convert.ToDouble instead of int.Parse

尝试一下!

如果您太懒了,请输入以下代码:

If you are too lazy, here's the code:

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

注意:我实际上并不了解快速排序算法.

Note: I don't actually know the quick sort algorithm.

这篇关于快速排序无法运行C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆