用java语言设置程序,首先从数据文件中读出各药品的信息记录,存储在顺序表中。各药品的信息包括:药品编号、药名、药品单价、销出数量、销售额。对单价的排序采用冒泡排序法,对销售量的排序采用快速排序法,对销售额的排序采用堆排序法。
时间: 2024-02-12 18:03:11 浏览: 70
java程序写的数据排序法
以下是用Java语言实现从数据文件中读出各药品信息记录并进行排序的示例代码:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class DrugSort {
private static final int MAX_SIZE = 100; // 最大记录数
public static void main(String[] args) {
Drug[] drugs = new Drug[MAX_SIZE]; // 定义Drug类型数组
int n = 0; // 记录数
try {
BufferedReader reader = new BufferedReader(new FileReader("drugs.txt"));
String line;
while ((line = reader.readLine()) != null && n < MAX_SIZE) {
String[] fields = line.split(",");
if (fields.length == 5) {
Drug drug = new Drug(fields[0], fields[1], Double.parseDouble(fields[2]),
Integer.parseInt(fields[3]), Double.parseDouble(fields[4]));
drugs[n++] = drug;
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
// 冒泡排序(按单价升序)
for (int i = 0; i < n; i++) {
boolean flag = false;
for (int j = 0; j < n - i - 1; j++) {
if (drugs[j].getPrice() > drugs[j + 1].getPrice()) {
swap(drugs, j, j + 1);
flag = true;
}
}
if (!flag) {
break;
}
}
// 快速排序(按销售量降序)
quickSort(drugs, 0, n - 1, SortType.SALES_NUM);
// 堆排序(按销售额升序)
heapSort(drugs, n, SortType.SALES_AMOUNT);
// 输出排序结果
System.out.println("按单价升序排序结果:");
for (int i = 0; i < n; i++) {
System.out.println(drugs[i]);
}
System.out.println("按销售量降序排序结果:");
for (int i = 0; i < n; i++) {
System.out.println(drugs[i]);
}
System.out.println("按销售额升序排序结果:");
for (int i = 0; i < n; i++) {
System.out.println(drugs[i]);
}
}
// 快速排序
private static void quickSort(Drug[] drugs, int left, int right, SortType sortType) {
if (left >= right) {
return;
}
int pivotIndex = partition(drugs, left, right, sortType);
quickSort(drugs, left, pivotIndex - 1, sortType);
quickSort(drugs, pivotIndex + 1, right, sortType);
}
private static int partition(Drug[] drugs, int left, int right, SortType sortType) {
Drug pivot = drugs[left];
int i = left + 1, j = right;
while (i <= j) {
while (i <= j && compare(drugs[i], pivot, sortType) > 0) {
i++;
}
while (i <= j && compare(drugs[j], pivot, sortType) < 0) {
j--;
}
if (i <= j) {
swap(drugs, i, j);
i++;
j--;
}
}
swap(drugs, left, j);
return j;
}
// 堆排序
private static void heapSort(Drug[] drugs, int n, SortType sortType) {
if (n <= 1) {
return;
}
// 建堆
buildHeap(drugs, n, sortType);
// 排序
for (int i = n - 1; i >= 1; i--) {
swap(drugs, 0, i);
heapify(drugs, i, 0, sortType);
}
}
// 建堆
private static void buildHeap(Drug[] drugs, int n, SortType sortType) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(drugs, n, i, sortType);
}
}
// 堆化
private static void heapify(Drug[] drugs, int n, int i, SortType sortType) {
int largest = i;
int l = i * 2 + 1;
int r = i * 2 + 2;
if (l < n && compare(drugs[l], drugs[largest], sortType) > 0) {
largest = l;
}
if (r < n && compare(drugs[r], drugs[largest], sortType) > 0) {
largest = r;
}
if (largest != i) {
swap(drugs, i, largest);
heapify(drugs, n, largest, sortType);
}
}
// 比较两个药品的大小
private static int compare(Drug drug1, Drug drug2, SortType sortType) {
switch (sortType) {
case PRICE:
return (int) (drug1.getPrice() - drug2.getPrice());
case SALES_NUM:
return drug2.getSalesNum() - drug1.getSalesNum();
case SALES_AMOUNT:
return (int) (drug1.getSalesAmount() - drug2.getSalesAmount());
default:
return 0;
}
}
// 交换元素
private static void swap(Drug[] drugs, int i, int j) {
Drug temp = drugs[i];
drugs[i] = drugs[j];
drugs[j] = temp;
}
}
enum SortType {
PRICE, SALES_NUM, SALES_AMOUNT
}
class Drug {
private String id;
private String name;
private double price;
private int salesNum;
private double salesAmount;
public Drug(String id, String name, double price, int salesNum, double salesAmount) {
this.id = id;
this.name = name;
this.price = price;
this.salesNum = salesNum;
this.salesAmount = salesAmount;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getSalesNum() {
return salesNum;
}
public double getSalesAmount() {
return salesAmount;
}
@Override
public String toString() {
return id + "," + name + "," + price + "," + salesNum + "," + salesAmount;
}
}
```
以上代码中,`Drug` 类表示药品信息记录,`SortType` 枚举类型表示排序方式,`DrugSort` 类表示主程序,其中 `main` 方法读取数据文件中的药品信息记录,然后按照单价升序、销售量降序、销售额升序分别进行排序,并输出排序结果。其中冒泡排序、快速排序、堆排序分别用于排序。
阅读全文