冒泡排序并行阵列

时间:2015-07-22 13:50:16

标签: java arrays sorting bubble-sort parallel-arrays

我需要读取数据文件并将其加载到2个数组中(1个并行)。数据由100个整数(ID#)列表组成,它们与双重(Price)相对应,如下所示:

[ID] - [价格]

837 - 14.88

253 - 65.12

931 - 11.96

196 - 20.47

我需要使用bubbleSort()方法按降序排列ID(及其相应的价格)。最后,我需要使用二进制顺序搜索方法来定位我将显示的特定目标。

我的问题 - 当我运行此程序时,我的顺序搜索成功,但我的二进制搜索不成功。我已经粘贴了下面的代码,希望有人来救我。

public class StoreInventory
{
    public int[] storeItem = new int[200];
    public double[] itemPrice = new double[200];
    public int itemCount = 0;

    StoreInventory() {}

    public void loadItems() 
    {       
        try {
            String filename = "MasterStoreInv.dat";
            Scanner infile = new Scanner(new FileInputStream(filename));

            while (infile.hasNext()) {                  
                storeItem[itemCount] = infile.nextInt();
                itemPrice[itemCount] = infile.nextDouble();
                itemCount += 1;             
            }

            infile.close();

        } catch (IOException ex) {
            itemCount = -1;
            ex.printStackTrace();
        }
    }

    public double getItemPrice(int item)
    {
        return itemPrice[item];
    }

    public void bubbleSort() 
    {   
        for (int i = 0; i < itemCount; i++) {
            for (int x = 1; x < itemCount - i; x++) {
                if (storeItem[x - 1] > storeItem[x] && itemPrice[x - 1] > itemPrice[x]) {
                    int temp = storeItem[x - 1];
                    double tempi = itemPrice[x - 1];
                    storeItem[x - 1] = storeItem[x];
                    itemPrice[x - 1] = itemPrice[x];
                    storeItem[x] = temp;
                    itemPrice[x] = tempi;
                } 
            }
        }
    }

    public int binSearch (int target)
    {
     int low = 0;
     int high = itemCount - 1;

     while(high >= low) {
      int mid = (low + high) / 2;
        if(storeItem[mid] == target) {
        return mid;
        }
        if(storeItem[mid] < target) {
          low = mid + 1;
          }
        if(storeItem[mid] > target) {
          high = mid - 1;
          }
       }
         return -1;
    }

    public int seqSearch (int target)
    {
        int ind = 0;
        int found = -1;

        while (ind < itemCount) {
          if(target==storeItem[ind]) {
              found = ind;
              ind = itemCount;
            }else {
              ++ind;
            }
        }
        return found;
    }   

    public static void main(String[] args) 
    {
        StoreInventory inventory = new StoreInventory();
        Scanner myScanner = new Scanner(System.in);
        int target, item;
        double itemPrice;


        inventory.loadItems();      
        inventory.bubbleSort();

        do {
            System.out.println("Which item number do you want to see -->");     
            target = myScanner.nextInt();
        /* Sequential Search */
        item = inventory.seqSearch(target);     
            if (item >= 0) {
                itemPrice = inventory.getItemPrice(item);
                System.out.print("\nSequential search - Successful!\nID number: " + target + "  Price: $" + itemPrice+ "\n");   
            }else {
                System.out.print("\nSequential search - Failed\nID number not found\n\n");
            }
        /* Binary Search */
        item = inventory.binSearch(target);     
            if (item >= 0) {
                itemPrice = inventory.getItemPrice(item);
                System.out.print("\nBinary search - Successful!\nID number: " + target + "  Price: $" + itemPrice+ "\n\n"); 
            }else {         
                System.out.print("\nBinary search - Failed\nID number not found\n\n");
            }

        System.out.print("Enter '1' to make another search\nEnter '0' to quit -->");

        }while (myScanner.nextInt() >= 1);
        myScanner.close();  


    }//END main

}

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

if (storeItem[x - 1] > storeItem[x] && itemPrice[x - 1] > itemPrice[x])

如果商品的价格都更高,那么您只会移动商品 - 这不是一个合适的排序。想象一下这样的数据:

5,10
1,20

这些不会被交换 - 而且这些将是:

1,20
5,10

您需要选择正确的顺序,例如:

storeItem[x - 1] > storeItem[x] || (storeItem[x - 1] == storeItem[x] && itemPrice[x - 1] > itemPrice[x])

这将确保所有条目都有严格的订单。

BTW - 您可以考虑构建class来存储这些对并使其实现Comparable