简体   繁体   English

Java中使用数组的选择排序算法

[英]selection sort algorithm using the array in java

When the program runs, it asks you to enter a product name and price, and then whenever you press -1 it will be stopped and display a list of the entered product and price. 程序运行时,要求您输入产品名称和价格,然后每按-1它将停止,并显示输入的产品和价格的列表。 However, the problem is I wrote a selection sort algorithm to sort the list by PRICE in ascending order. 但是,问题是我编写了一个选择排序算法,以按PRICE升序对列表进行排序。 The output is not what I expected. 输出不是我期望的。 Have a look at "//Selection Sort" in this code 看看这段代码中的“ //选择排序”

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 

Basically you have implemented selection sort incorrectly. 基本上,您没有正确实现选择排序。 Here is the code to fix it, including a counter to count how many products have been entered (makes the implementation clearer). 这是修复此问题的代码,包括一个计数器,用于计算已输入的产品数量(使实现更清晰)。 Also as the other post says, you do not swap the names in your example, only the prices. 同样,正如另一篇文章所说,在示例中,您不交换名称,仅交换价格。 I think the following should work: 我认为以下应该起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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