java - 数组 : selection sort doesn't display

标签 java arrays sorting

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

我的问题是,我不希望软件的名称与号码分开!另外,当我编译时,它没有显示排序后的名称!我读了很多关于选择排序的文章,它们基本上都是这样的。这可能有什么问题吗?我的选择排序有错吗?

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

// 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.

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

最佳答案

您应该更改行:

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 

关于java - 数组 : selection sort doesn't display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18472311/

相关文章:

java - 如何从外部jsp调用内部类方法或变量?

java - 对要打印的对象属性的自省(introspection)

用指针对二维数组进行排序的代码

在 Θ(n) 时间内对列表进行排序的算法

c - 如何将两个数组组合成一个交替元素?

java - 在 android 中通过来自 gmaps 的消息获取共享的位置坐标

java - 动态删除 Spring ApplicationListener 的最佳实践?

C++ 可变多类型数组

javascript - p5.j​​s 中对象的 2D 网格

python - 为什么 `for` 在 Python 列表上比在 Numpy 数组上更快?