简体   繁体   中英

Quick Sort won't run C#

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

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

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,

  • 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

Try it!


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.

int表示整数一个整数没有分数

I didn't run this, so it might not work without some tweaking but when it does work I hope you try and figure out what was wrong with your version:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sortQuick
{
    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)
        {
            int leftend, rightend;
            double pivot;

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

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

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

UPDATE : Ok, your code was hard to modifyso I got the original code from here and modified it to work with double. So here is a working version (I tested it).

class Program
    {
        static public int Partition(double[] numbers, int left, int right)
        {
            double pivot = numbers[left];
            while (true)
            {
                while (numbers[left] < pivot)
                    left++;

                while (numbers[right] > pivot)
                    right--;

                if (left < right)
                {
                    double temp = numbers[right];
                    numbers[right] = numbers[left];
                    numbers[left] = temp;
                }
                else
                {
                    return right;
                }
            }
        }

        static public void QuickSort_Recursive(double[] arr, int left, int right)
        {
            // For Recusrion
            if (left < right)
            {
                int pivot = Partition(arr, left, right);

                if (pivot > 1)
                    QuickSort_Recursive(arr, left, pivot - 1);

                if (pivot + 1 < right)
                    QuickSort_Recursive(arr, pivot + 1, right);
            }
        }

        static void Main(string[] args)
        {
            double[] numbers = System.IO.File.ReadAllLines(@"C:\WS1_Rain.txt").Select(p => double.Parse(p)).ToArray();
            int len = 9;

            Console.WriteLine("QuickSort By Recursive Method");
            QuickSort_Recursive(numbers, 0, len - 1);
            for (int i = 0; i < 9; i++)
                Console.WriteLine(numbers[i]);

            Console.WriteLine();
            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