用java编写一个程序,输入药品编号、药名、药品单价、销售量、销售额存储在顺序表中,药品编号共4位,采用字母和数字混合编号,如:A125,前一位为大写字母,后三位为数字,在本系统中,对药品单价的排序采用冒泡排序法,对
时间: 2023-09-06 12:10:36 浏览: 82
销售量的排序采用快速排序法,对销售额的排序采用堆排序法,对药品编号进行排序时,可以采用以下代码实现(假设药品信息存储在一个List中):
```java
import java.util.*;
public class DrugSort {
public static void main(String[] args) {
List<Drug> drugs = new ArrayList<>();
// 输入药品信息
// ...
// 对药品单价进行冒泡排序
for (int i = 0; i < drugs.size() - 1; i++) {
for (int j = 0; j < drugs.size() - i - 1; j++) {
if (drugs.get(j).getPrice() > drugs.get(j+1).getPrice()) {
Collections.swap(drugs, j, j+1);
}
}
}
// 对销售量进行快速排序
quickSort(drugs, 0, drugs.size() - 1, "Sales");
// 对销售额进行堆排序
heapSort(drugs, "Revenue");
// 对药品编号进行选择排序
for (int i = 0; i < drugs.size() - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < drugs.size(); j++) {
if (drugs.get(j).getId().compareTo(drugs.get(minIndex).getId()) < 0) {
minIndex = j;
}
}
if (minIndex != i) {
Collections.swap(drugs, i, minIndex);
}
}
// 输出排序后的药品信息
// ...
}
// 快速排序
private static void quickSort(List<Drug> drugs, int left, int right, String fieldName) {
if (left < right) {
int partitionIndex = partition(drugs, left, right, fieldName);
quickSort(drugs, left, partitionIndex - 1, fieldName);
quickSort(drugs, partitionIndex + 1, right, fieldName);
}
}
private static int partition(List<Drug> drugs, int left, int right, String fieldName) {
Drug pivot = drugs.get(right);
int i = left - 1;
for (int j = left; j < right; j++) {
if (fieldName.equals("Sales") && drugs.get(j).getSales() < pivot.getSales()
|| fieldName.equals("Revenue") && drugs.get(j).getRevenue() < pivot.getRevenue()) {
i++;
Collections.swap(drugs, i, j);
}
}
Collections.swap(drugs, i+1, right);
return i+1;
}
// 堆排序
private static void heapSort(List<Drug> drugs, String fieldName) {
// 构建最大堆
for (int i = (drugs.size() - 2) / 2; i >= 0; i--) {
maxHeapify(drugs, i, drugs.size(), fieldName);
}
// 堆排序
for (int i = drugs.size() - 1; i > 0; i--) {
Collections.swap(drugs, 0, i);
maxHeapify(drugs, 0, i, fieldName);
}
}
private static void maxHeapify(List<Drug> drugs, int index, int heapSize, String fieldName) {
int left = index * 2 + 1;
int right = index * 2 + 2;
int largest = index;
if (left < heapSize && (fieldName.equals("Sales") && drugs.get(left).getSales() > drugs.get(largest).getSales()
|| fieldName.equals("Revenue") && drugs.get(left).getRevenue() > drugs.get(largest).getRevenue())) {
largest = left;
}
if (right < heapSize && (fieldName.equals("Sales") && drugs.get(right).getSales() > drugs.get(largest).getSales()
|| fieldName.equals("Revenue") && drugs.get(right).getRevenue() > drugs.get(largest).getRevenue())) {
largest = right;
}
if (largest != index) {
Collections.swap(drugs, index, largest);
maxHeapify(drugs, largest, heapSize, fieldName);
}
}
}
class Drug {
private String id; // 药品编号
private String name; // 药品名称
private double price; // 药品单价
private int sales; // 销售量
private double revenue; // 销售额
public Drug(String id, String name, double price, int sales) {
this.id = id;
this.name = name;
this.price = price;
this.sales = sales;
this.revenue = price * sales;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
public double getRevenue() {
return revenue;
}
public void setRevenue(double revenue) {
this.revenue = revenue;
}
}
```
其中,我们将药品信息封装在一个Drug类中,再利用List来存储所有药品信息。在排序过程中,我们分别使用冒泡排序、快速排序、堆排序和选择排序来对药品单价、销售量、销售额和药品编号进行排序。
阅读全文