Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views3 pages

Invtester 1

The document is a Java program that manages an inventory of products and applies various sorting algorithms (insertion, selection, and merge sort) to organize the products based on different attributes such as product number, name, price, and quantity. It includes methods for printing the product list in a readable format and sorting the products accordingly. The program demonstrates the functionality of sorting algorithms through a sample inventory of products.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Invtester 1

The document is a Java program that manages an inventory of products and applies various sorting algorithms (insertion, selection, and merge sort) to organize the products based on different attributes such as product number, name, price, and quantity. It includes methods for printing the product list in a readable format and sorting the products accordingly. The program demonstrates the functionality of sorting algorithms through a sample inventory of products.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/**

* Array of Product objects, applies multiple sorting algorithms (insertion,


selection, and merge sort), and prints the inventory in a readable table format.
*
* Aksh Ladegaonkar
* APR 12, 2025
*/
public class InventoryTester1 {
public static void main(String[] args) {
Product1[] inventory = {
new Product1("Notebook", 1010, 2.99, 120),
new Product1("Eraser", 1025, 0.99, 200),
new Product1("Binder", 1032, 4.5, 85),
new Product1("Backpack", 1005, 29.99, 45),
new Product1("Pens", 1041, 3.49, 150),
new Product1("Pencil", 1015, 0.89, 300),
new Product1("Highlighter", 1052, 1.29, 175),
new Product1("Calculator", 1060, 12.49, 40),
new Product1("Glue Stick", 1077, 1.99, 90),
new Product1("Ruler", 1080, 1.59, 110)
};

System.out.println("Original Product List:");


printArray(inventory);

System.out.println("\nSorted by Product Number (Insertion Sort


Ascending):");
insertionSortProductNumber(inventory, true);
printArray(inventory);

System.out.println("\nSorted by Name (Selection Sort Descending):");


selectionSortName(inventory, false);
printArray(inventory);

System.out.println("\nSorted by Price (Merge Sort Ascending):");


mergeSortPrice(inventory, 0, inventory.length - 1);
printArray(inventory);

System.out.println("\nSorted by Quantity (Selection Sort Ascending):");


selectionSortQuantity(inventory, true);
printArray(inventory);
}

public static void printArray(Product1[] arr) {


System.out.println("Name Product# Price Quantity");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}

public static void insertionSortProductNumber(Product1[] arr, boolean


ascending) {
for (int i = 1; i < arr.length; i++) {
Product1 key = arr[i];
int j = i - 1;

if (ascending) {
while (j >= 0 && arr[j].getProductNumber() >
key.getProductNumber()) {
arr[j + 1] = arr[j];
j--;
}
} else {
while (j >= 0 && arr[j].getProductNumber() <
key.getProductNumber()) {
arr[j + 1] = arr[j];
j--;
}
}

arr[j + 1] = key;
}
}

public static void selectionSortName(Product1[] arr, boolean ascending) {


for (int i = 0; i < arr.length - 1; i++) {
int selected = i;
for (int j = i + 1; j < arr.length; j++) {
if (ascending) {
if (arr[j].getName().compareTo(arr[selected].getName()) < 0) {
selected = j;
}
} else {
if (arr[j].getName().compareTo(arr[selected].getName()) > 0) {
selected = j;
}
}
}
Product1 temp = arr[i];
arr[i] = arr[selected];
arr[selected] = temp;
}
}

public static void selectionSortQuantity(Product1[] arr, boolean ascending) {


for (int i = 0; i < arr.length - 1; i++) {
int selected = i;
for (int j = i + 1; j < arr.length; j++) {
if (ascending) {
if (arr[j].getQuantity() < arr[selected].getQuantity()) {
selected = j;
}
} else {
if (arr[j].getQuantity() > arr[selected].getQuantity()) {
selected = j;
}
}
}
Product1 temp = arr[i];
arr[i] = arr[selected];
arr[selected] = temp;
}
}

public static void mergeSortPrice(Product1[] arr, int left, int right) {


if (left < right) {
int mid = (left + right) / 2;
mergeSortPrice(arr, left, mid);
mergeSortPrice(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

public static void merge(Product1[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

Product1[] L = new Product1[n1];


Product1[] R = new Product1[n2];

for (int i = 0; i < n1; i++) {


L[i] = arr[left + i];
}

for (int j = 0; j < n2; j++) {


R[j] = arr[mid + 1 + j];
}

int i = 0;
int j = 0;
int k = left;

while (i < n1 && j < n2) {


if (L[i].getPrice() <= R[j].getPrice()) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}
}

You might also like