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
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; }
...
}
Comments
Leave a comment