数组:选择排序不显示

时间:2013-08-27 17:51:21

标签: java arrays sorting

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

我的问题是,我不希望软件的名称与数字分开!另外,在我编写时,它不显示已分类的名称!我读了关于选择排序的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.

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

3 个答案:

答案 0 :(得分:0)

你有一个for循环,它在for循环中迭代i,迭代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;
}

答案 1 :(得分:0)

您应该更改以下行:

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 :(得分:-1)

我不确定为什么你需要选择排序,因为它保证了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

你是否期望有不同的东西,如果是,请你在问题中添加它。此外,这不是选择排序。这对我来说似乎更像是一个泡沫。

编辑压缩在我的机器上运行的代码以产生上述输出。     import java.io.BufferedReader;     import java.io.IOException;     import 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);
}
}