Answer to Question #274910 in Java | JSP | JSF for Evan

Question #274910

Write a linear search to find an InventoryItem by Description and tell whether that InventoryItem is in the set or not. 

Sort the Array by using a Bubble Sort, Select Sort, or another sort you know of (and your instructor may have shown the Insert Sort in lecture) to sort the array by ItemID. Then loop through the array printing the values of each InventoryItem to show they are now in order.

Then ask the user for a ItemID and write a Binary Search to tell 


1
Expert's answer
2021-12-04T01:27:58-0500
class SortManager {
  void bubbleSort(InventoryItem[] items) {
    int length = items.length;
    boolean swap = true;
    while (swap) {
      InventoryItem item;
      swap = false;
      length--;
      for (int i = 0; i < length; i++) {
        if (items[i+1].getId() < items[i].getId()) {
          item = items[i];
          items[i] = items[i+1];
          items[i+1] = item;
          swap=true;
        }
      }
    }
  }
  void selectSort(InventoryItem[] items) {
    InventoryItem item;
    for (int i = 0; i < items.length-1; i++) {
      for (int j = i+1; j < items.length; j++) {
        if (items[i].getId() < items[j].getId()) {
          item = items[i];
          items[i] = items[j];
          items[j] = item;
        }
      }
    }
  }
  void insertSort(InventoryItem[] items) {
    InventoryItem item;
    int i, j;
    for (int i = 0; i < items.length; i++) {
      item = items[i];
      j = i;
      while (j > 0 && items[j-1].getId() > item.getId()) {
        item[j] = items[j-1];
        j--;
      }
      items[j] = item;
    }
  }
}

class InventoryItem {
  int itemID;
  ...

  public int getId() { return itemID; }
  ...
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS