冒泡排序。我需要double还是int?

时间:2011-03-30 01:59:37

标签: java arrays sorting bubble-sort

好的所以我正在制作一个泡泡排序程序,以确保数组是有序的,我现在已经编译好第一个类,但第二个一直给我带来问题。现在它说它是错误的类型所以我将变量dif更改为int而不是double但是它表示可能会丢失精度。这是第一个文件的代码。

public class BubbleSort
{
    public static void sort(int[] a, int numberUsed)
    {
        int index;
        for ( int i =0; i<numberUsed; i++)
        {
            for (index = 0; index<a.length - 1; index++)
            {
                if (a[index]> a[index + 1])
                {
                    interchange(index, index + 1, a);
                } //end of if ()
            } //end of for ()
        } //end of for ()
    }
        private static void interchange(int i, int j, int[] a)
        {
            int temp1;
            temp1 = a[i];
            a[i] = a[j];
            a[j] = temp1;
        }
    }

这是第二个给我提问题的文件

import java.util.Scanner;
public class GolfScores
{
    public static double[] diff = new double[5];
    public static void printOutArray(double[] a)
    {
        System.out.println("Sorted array values:");
        for (int i=0; i<a.length; i++)
        {
            System.out.printf("%2.2f",a[i]);
        } //end of for loop
        System.out.println();
        double Handicap= (diff[0])/1*0.96;
        System.out.printf("Handicap: %2.2f",Handicap);
        System.out.println();
    }
    public static void main(String[]args)
    {
        //construct and declare three arrays
        Scanner keyboard = new Scanner(System.in);
        double[] rating = new double[5];
        double[] slope = new double[5];
        double[] score = new double[5];
        int numberUsed = 0;
        int index = 0;

        //Print out directions for the user
        System.out.println("Calculate handicap for 5 games of golf.  This program takes Scores, \nCourse Rating and Slope rating for 5 games. \n\nUsing those figures, the program calculates the differential\nfor each round entered using this formula:(Score - Course Rating) x113 / Slope Rating.  \n\nThen uses the lowest differential to calculate handicap");
        System.out.println();
        //A do while loop that runs until index is great than 5
        do
        {
            System.out.print("Enter golf scores for game " +(index+1)+ ": ");
            score[index]= keyboard.nextDouble();
            System.out.print("Course rating for game "+(index+1)+ ": ");
            rating[index] = keyboard.nextDouble();
            System.out.print("Course slope for game "+(index+1)+": ");
            slope[index] = keyboard.nextDouble();
            index++;
            //index is the number of array indexed variables used so far
        } while((index<5));
        //this formula for all 5 arrays (Score- Course Rating) x 113 / Slope Rating
        diff[0]=((score[0]- rating[0]) * 113 / slope [0]);
        diff[1]=((score[1]-rating[1])*113/slope[1]);
        diff[2]=((score[2]-rating[2])*113/slope[2]);
        diff[3]=((score[3]-rating[3])*113/slope[3]);
        diff[4]=((score[4]-rating[4])*113/slope[4]);

        BubbleSort.sort(diff, diff.length);//passes value of diff array to BubbleSort.sort
        printOutArray(diff);//prints out diff array
    }
}

2 个答案:

答案 0 :(得分:0)

您的冒泡排序仅适用于int [],因此diff必须为int []。
我认为“可能的精度损失”来自于

    diff[0]=((score[0]- rating[0]) * 113 / slope [0]);
    diff[1]=((score[1]-rating[1])*113/slope[1]);
    diff[2]=((score[2]-rating[2])*113/slope[2]);
    diff[3]=((score[3]-rating[3])*113/slope[3]);
    diff[4]=((score[4]-rating[4])*113/slope[4]);

由于:

    double[] rating = new double[5];
    double[] slope = new double[5];
    double[] score = new double[5];

这些数组定义为double []。


您需要什么数据类型取决于您的输入内容 如果输入只是int,那么使用int。如果可能出现浮点数,则应考虑加倍。

答案 1 :(得分:0)

您需要确保数据类型匹配,如前所述,您的BubbleSort类仅在int数组上运行,这是修改为使用双数组(diff包含的内容)的代码。

当您将dif更改为int并收到“精度损失”消息时,这是因为您正在进行除法(GolfScores.java中的第43-47行),然后将结果分配给int(丢弃小数值) )。

这是BubbleSort的一个版本,可以编译而不会出现错误或警告。我所做的更改(其中有3个)被注释为注释:

public class BubbleSort
{
    public static void sort(double[] a, int numberUsed) //change here
    {
        int index;
        for ( int i =0; i<numberUsed; i++)
        {
            for (index = 0; index<a.length - 1; index++)
            {
                if (a[index]> a[index + 1])
                {
                    interchange(index, index + 1, a);
                } //end of if ()
            } //end of for ()
        } //end of for ()
    }
        private static void interchange(int i, int j, double[] a) //change here
        {
            double temp1; //change here, important
            temp1 = a[i];
            a[i] = a[j];
            a[j] = temp1;
        }
    }

通过使用对象而不是基本类型(例如,对Object数组进行排序而不是double),可以使代码更加灵活,并且可能使用用户指定的函数来比较元素,这样您就不需要更改类以处理不同的数据类型。也就是说,如果您要使用更多Java的工具和标准库,那么您也可以使用Arrays.sort方法。 This page解释了使用内置排序方法(以及为什么你不应该编写自己的排序例程,除非它是某种学术任务)比我更好。