简体   繁体   中英

How to make Quick Sort sort decimal numbers C#

Here is a Quick Sort program that reads the numbers from the file and then sorts them smallest to highest. I need it to be able to do the exact same but for numbers with decimals. I understand there is ways of converting int to double, decimal or float, but I've tried everything i can and nothing is working, can someone show me how they would change this code in order to make it work. Below is how i've attempted to do it:

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

If your end goal is to purely sort the array, you could just use 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);

However, if you wish to amend your QuickSort class, you could do it like this with minimal changes to your original code. It is only the array that needs to support doubles. Your supporting variables used to manage indexing can stay as integers:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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