java - Bubble Sort Parallel Array -


i need read , load data file 2 arrays (1 parallel). data consists of list of 100 integers (id#) correspond double(price) this:

[id] - [price]

837 - 14.88

253 - 65.12

931 - 11.96

196 - 20.47

i need use bubblesort() method arrange id's(and corresponding price) in descending order. lastly, need use binary and sequential search methods locate specific target display.

my issue - when run program sequential search successful, binary search not. have pasted code below in hopes come rescue.

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()      {            (int = 0; < itemcount; i++) {             (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();          {             system.out.println("which item number want 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' make search\nenter '0' quit -->");          }while (myscanner.nextint() >= 1);         myscanner.close();         }//end main  } 

your problem here:

if (storeitem[x - 1] > storeitem[x] && itemprice[x - 1] > itemprice[x]) 

you moving items if both item and price greater - not proper sort. imagine data such as:

5,10 1,20 

these not swapped - , n'or these:

1,20 5,10 

you need choose proper ordering such perhaps:

storeitem[x - 1] > storeitem[x] || (storeitem[x - 1] == storeitem[x] && itemprice[x - 1] > itemprice[x]) 

this ensure entries have strict order.

btw - may wish consider building class store pairs , making implement comparable.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -