【问题标题】:selection sort algorithm using the array in javajava中使用数组的选择排序算法
【发布时间】:2013-05-04 01:26:46
【问题描述】:

当程序运行时,它会要求您输入产品名称和价格,然后当您按 -1 时,它将停止并显示输入的产品和价格的列表。但是,问题是我编写了一个选择排序算法,以按 PRICE 升序对列表进行排序。输出不是我所期望的。看看这段代码中的“//选择排序”

import java.util.Scanner;

public class ProductPrices {
    private static Scanner keyboard = new Scanner(System.in);
    private static Scanner key = new Scanner(System.in);

    final static int arrayLength = 1000; //maximum array size
    static float[] productPrice = new float[arrayLength]; //stores the prices of products
    static String[] productName = new String[arrayLength]; //stores the names of products

    public static void main(String[] args) {

        System.out.println("SHOPPING. Press -1 to quit anytime.\n");

        for(int i=0; i<arrayLength; i++) {
            System.out.print("Product: ");
            productName[i] = keyboard.nextLine();
            if(productName[i].equals("-1"))
                break;

            System.out.print("Price: $");
            productPrice[i] = key.nextFloat();
            if(productPrice[i] < 0)
                break;
        }

        System.out.println("\nList of the SHOPPING!\n---------------------");
        for(int i=0; i<productPrice.length; i++) {
            if(productName[i] == null || productName[i].equals("-1") || productPrice[i] < 0)
                continue; // null arrays will not be displayed.
            else {
                // Selection sort
                if(productPrice[i] > productPrice[i+1]) {
                    float temp = productPrice[i];
                    productPrice[i] = productPrice[i+1];
                    productPrice[i+1] = temp;
                }
                System.out.printf("Item: %s %.2f\n", productName[i],  productPrice[i]);
            }
        }
    }
}
    For example
    :::Input:::
    Product: apple
    Price: $2.35
    Product: pie
    Price: $1.36
    Product: cereal
    Price: $7.4
    Product: -1

    :::Output:::
    Item: apple 1.36
    Item: pie 2.35
    Item: cereal 0.00

    That is incorrect, it should be
    Item: pie 1.36
    Item: apple 2.35
    Item: cereal 7.40

【问题讨论】:

  • The output is not what I expected. 那么目前情况如何,您有什么期望?
  • 我在那里的代码中添加了输出示例。
  • 好吧,我敢打赌,因为这会简单地抛出一个 ArrayIndexOutOfBoundsException,因为您在访问 [i+1] 索引时迭代到 i
  • 我没有学过 MergeSort,我可能不应该使用它,因为我只学会了选择排序。
  • 我认为这是一个家庭作业,所以您可能也无法使用 HashMap。

标签: java arrays algorithm sorting selection


【解决方案1】:

基本上你已经错误地实现了选择排序。这是修复它的代码,包括一个计数器来计算输入了多少产品(使实现更清晰)。同样正如另一篇文章所说,您不交换示例中的名称,只交换价格。我认为以下应该有效:

public static void main(String[] args) {
    System.out.println("SHOPPING. Press -1 to quit anytime.\n");
    int prodCount = 0;

    for(int i=0; i<arrayLength; i++) {
        System.out.print("Product: ");
        productName[i] = keyboard.nextLine();
        if(productName[i].equals("-1"))
            break;

        System.out.print("Price: $");
        productPrice[i] = key.nextFloat();
        if(productPrice[i] < 0)
            break;

        prodCount++;
    }

    System.out.println("\nList of the SHOPPING!\n---------------------");
    for (int j = 0; j < prodCount-1; j++) {

       int iMin = j;
       for (int i = j+1; i < prodCount; i++) {
           if (productPrice[i] < productPrice[iMin]) {
               iMin = i;
           }
       }
       if ( iMin != j ) {
           float temp = productPrice[j];
           productPrice[j] = productPrice[iMin];
           productPrice[iMin] = temp;
           String tempn = productName[j];
           productName[j] = productName[iMin];
           productName[iMin] = tempn;
       }
    }
    for(int i=0; i<prodCount; i++) {
        System.out.printf("Item: %s %.2f\n", productName[i],  productPrice[i]);
    }
}

【讨论】:

  • 不好给出完整的答案。 OP仍处于学习阶段。这样,除了复制和粘贴之外,OP 将无法学到很多东西。
  • 感谢您的回答,但我必须同意 Smit。你不应该直接回答。我对 prodCount 之类的东西有点困惑,但无论如何我都会尝试了解它是如何工作的。
  • 是的,经过反思,我明白你的意思。无论如何,希望您可以使用此代码来帮助您研究选择排序的工作原理。 ProdCount 计算输入了多少产品,然后用作第二个循环的限制。因此,您不必遍历数组的所有 1000 个元素并测试空值等。
猜你喜欢
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
  • 2021-02-06
  • 1970-01-01
  • 2021-03-20
相关资源
最近更新 更多