Practical 3
Input:
class Product {
int id;
String name;
String manufacturer;
double price;
double rating;
Product(int id, String name, String manufacturer, double price, double rating) {
[Link] = id;
[Link] = name;
[Link] = manufacturer;
[Link] = price;
[Link] = rating;
}
void display() {
[Link](id + " " + name + " " + manufacturer + " " + price + " " + rating);
}
}
public class sortingPractical {
static void bubbleSort(Product[] arr) {
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j].id > arr[j + 1].id) {
Product temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
static void selectionSort(Product[] arr) {
for (int i = 0; i < [Link] - 1; i++) {
int min = i;
for (int j = i + 1; j < [Link]; j++) {
if (arr[j].price < arr[min].price) {
min = j;
}
}
Product temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
static void insertionSort(Product[] arr) {
for (int i = 1; i < [Link]; i++) {
Product key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j].rating < [Link]) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
static void display(Product[] arr) {
for (Product p : arr) {
[Link]();
}
[Link]();
}
public static void main(String[] args) {
Product[] products = {
new Product(105, "Smartphone", "BrandA", 300, 4.3),
new Product(101, "Laptop", "BrandB", 800, 4.7),
new Product(109, "Tablet", "BrandC", 200, 4.0),
new Product(102, "Headphones", "BrandD", 50, 3.8),
new Product(103, "Smartwatch", "BrandE", 150, 4.5)
};
[Link]("Increasing Order of Product Id:");
bubbleSort(products);
display(products);
[Link]("Increasing order of product price:");
selectionSort(products);
display(products);
[Link]("Decreasing order of Product Quality:");
insertionSort(products);
display(products);
}
}
Output:
Increasing Order of Product Id:
101 Laptop BrandB 800.0 4.7
102 Headphones BrandD 50.0 3.8
103 Smartwatch BrandE 150.0 4.5
105 Smartphone BrandA 300.0 4.3
109 Tablet BrandC 200.0 4.0
Increasing order of product price:
102 Headphones BrandD 50.0 3.8
103 Smartwatch BrandE 150.0 4.5
109 Tablet BrandC 200.0 4.0
105 Smartphone BrandA 300.0 4.3
101 Laptop BrandB 800.0 4.7
Decreasing order of Product Quality:
101 Laptop BrandB 800.0 4.7
103 Smartwatch BrandE 150.0 4.5
105 Smartphone BrandA 300.0 4.3
109 Tablet BrandC 200.0 4.0
102 Headphones BrandD 50.0 3.8