【问题标题】:Array: selection sort doesn't display数组:选择排序不显示
【发布时间】:2013-08-27 17:51:21
【问题描述】:

我要做的是编写一个程序,让用户输入软件的名称以及有多少库存。我需要将它们存储在一个数组中,然后使用选择排序按从最少到最多的顺序对其进行排序。

我的问题是,我不想软件的名字和数字分开!另外,当我编译时它没有显示排序的名称!我读过关于选择排序的TONS,它们基本上都是这样的。它有什么问题?我做错了选择排序吗?

这不是我的全部代码,但我认为我没有遗漏任何重要的内容:

// Global variables
static String[] SoftwareArray;
static int[] QuantityArray;

public static void inputInfo() throws IOException
{
    BufferedReader userInput = new BufferedReader  (new InputStreamReader(System.in));  
    System.out.print("How many softwares would you like to input? ");
    String software = userInput.readLine();
    int softwareNum = Integer.parseInt(software);  
    int[] softArray = new int[softwareNum];      

    String [] name = new String [softwareNum];
    int [] quantity = new int[softwareNum];      

    // Initialize global variables
    SoftwareArray = new String[softwareNum];
    QuantityArray = new int[softwareNum];

    //loop through number of softwares  
    for (int i = 0; i < softwareNum; i++)
    {
        System.out.println("Input name of software: ");
        String softwareName = userInput.readLine();

        name[i] = softwareName;

        System.out.println("Input quantity of software: ");
        String quantityString = userInput.readLine();
        int softwareQuantity = Integer.parseInt(quantityString);  

        quantity[i] = softwareQuantity;

        // Copy the software name and quantity to the global variables
        QuantityArray[i] = quantity[i];
        SoftwareArray[i] = name[i];

        System.out.println("There are " + quantity[i] + " of the " + name[i] + " software.");
    }
}

//method to sort and display info
public static void displayInfo(int[] arr, String[] name)
{      
    //sort by quantity
    for(int i=0; i<arr.length; i++)
    {
        for(int j=i+1; j<arr.length; j++)
        {
            if(arr[i] > arr[j] )
            {
                int temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;

                String tempString = name[j];
                name[j] = name[i];
                name[i] = tempString;
            }
        }
        //output
        for(i=0; i < arr.length; i++)
        {
            System.out.println(arr[i] + "  " + name[i]);
        }
    }
}

//main
public static void main(String[] args) throws IOException {
   //input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    inputInfo();

    displayInfo(QuantityArray, SoftwareArray);
}

输出:

How many softwares would you like to input? 2
Input name of software: 
Microsoft
Input quantity of software: 
1000
There are 1000 of the Microsoft software.
Input name of software: 
Linux
Input quantity of software: 
2983
There are 2983 of the Linux software.

然后什么都没有。它根本不显示排序列表。

【问题讨论】:

  • 你能为我们发布一些示例输入/输出吗?

标签: java arrays sorting


【解决方案1】:

你应该换行:

for(int j=i+1; j<arr.length; j++)

到:

for(int j=i; j<arr.length; j++)

完整代码:

public class SelectionSort {

    public static void displayInfo(int[] arr, String[] name)
    {
        //sort by quantity
        for(int i=0; i<arr.length; i++)
        {
            for(int j=i; j<arr.length; j++)
            {
                if(arr[i] > arr[j] )
                {
                    int temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;

                    String tempString = name[j];
                    name[j] = name[i];
                    name[i] = tempString;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] QuantityArray = {3,2,4,1,5};
        String[] SoftwareArray = {"3","2","4","1","5"};
        displayInfo(QuantityArray, SoftwareArray);
        for(int i=0; i<QuantityArray.length; i++){
            System.out.print(QuantityArray[i]+" ");
        }
        System.out.println();
        for(int i=0; i<QuantityArray.length; i++){
            System.out.print(SoftwareArray[i]+" ");
        }
    }
}

输出:

1 2 3 4 5 
1 2 3 4 5 

【讨论】:

    【解决方案2】:

    您的 for 循环中有一个遍历 i 的 for 循环,该循环遍历 i

    for(int i=0; i<arr.length; i++)
    {
        for(int j=i+1; j<arr.length; j++)
        {
            if(arr[i] > arr[j] )
            {
                int temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
    
                String tempString = name[j];
                name[j] = name[i];
                name[i] = tempString;
            }
        }
        //output
        for(i=0; i < arr.length; i++)
        {
            System.out.println(arr[i] + "  " + name[i]);
        }
    }
    

    我认为您可能打算将第二个循环放在第一个循环之外

    我想你可能打算把那个循环变成

    for(int i=0; i<arr.length; i++)
    {
        for(int j=i+1; j<arr.length; j++)
        {
            ...
        }
    }
    //output
    for(i=0; i < arr.length; i++)
    {
        System.out.println(arr[i] + "  " + name[i]);
    }
    

    另一方面,JB Nized 是对的。如果你想让你的名字和数量更加耦合,你应该做一个类

    public class Software
    {
        public int quantity;
        public int string;
    }
    

    【讨论】:

    • 没有它,我的 IDE 会给我很多错误和异常。有什么想法吗?
    • 就像您写信给 JB 一样 - 这不是数组未排序问题的根源 - 这只是一个打印问题。我的回答中提到了排序不起作用的原因。
    【解决方案3】:

    我不确定您为什么特别需要选择排序,因为它保证了 O(n^2) 的复杂性。此外,我觉得外循环应该运行到一定长度 - 1. 检查here 以使其正确。

    此外,目前您的代码给出以下输出:

    How many softwares would you like to input? 3
    
    Input name of software: windows
    Input quantity of software: 3
    There are 3 of the windows software.
    
    Input name of software: google
    Input quantity of software: 2
    There are 2 of the google software.
    
    Input name of software: office
    Input quantity of software: 4
    There are 4 of the office software.
    
    2  google
    3  windows
    4  office
    

    您是否期望有所不同,如果是,请在问题中添加。此外,这不是选择排序。在我看来,这更像是一种泡沫。

    编辑 粘贴您在我的机器上运行的代码以产生上述输出。 导入 java.io.BufferedReader; 导入 java.io.IOException; 导入 java.io.InputStreamReader;

    public class Test {
    
        // Global variables
        static String[] SoftwareArray;
        static int[] QuantityArray;
    
        public static void inputInfo() throws IOException {
        BufferedReader userInput = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.print("How many softwares would you like to input? ");
        String software = userInput.readLine();
        int softwareNum = Integer.parseInt(software);
        int[] softArray = new int[softwareNum];
    
        String[] name = new String[softwareNum];
        int[] quantity = new int[softwareNum];
    
        // Initialize global variables
        SoftwareArray = new String[softwareNum];
        QuantityArray = new int[softwareNum];
    
        // loop through number of softwares
        for (int i = 0; i < softwareNum; i++) {
            System.out.println("Input name of software: ");
            String softwareName = userInput.readLine();
    
            name[i] = softwareName;
    
            System.out.println("Input quantity of software: ");
            String quantityString = userInput.readLine();
            int softwareQuantity = Integer.parseInt(quantityString);
    
            quantity[i] = softwareQuantity;
    
            // Copy the software name and quantity to the global variables
            QuantityArray[i] = quantity[i];
            SoftwareArray[i] = name[i];
    
            System.out.println("There are " + quantity[i] + " of the "
                    + name[i] + " software.");
        }
    }
    
    // method to sort and display info
    public static void displayInfo(int[] arr, String[] name) {
        // sort by quantity
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
    
                    String tempString = name[j];
                    name[j] = name[i];
                    name[i] = tempString;
                }
            }
            // output
            for (i = 0; i < arr.length; i++) {
                System.out.println(arr[i] + "  " + name[i]);
            }
        }
    }
    
    // main
    public static void main(String[] args) throws IOException {
        // input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        inputInfo();
        displayInfo(QuantityArray, SoftwareArray);
    }
    }
    

    【讨论】:

    • 我需要使用选择,因为这是我的作业所需要的。该输出不是我在代码中得到的,但它是我想要的!
    • 别忘了把排序改成选择排序。
    • 不,等等,它没有按顺序给我。既不是我写它的顺序,也不是从最小到最大的顺序......
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2016-08-15
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多