Public static boolean isSorted(Comparable[] table)判断对象数组是否已按升序排序
时间: 2024-05-05 09:17:07 浏览: 141
以下是Java代码实现:
```
public static boolean isSorted(Comparable[] table) {
for (int i = 1; i < table.length; i++) {
if (table[i].compareTo(table[i-1]) < 0) {
return false;
}
}
return true;
}
```
首先,我们遍历整个数组,从第二个元素开始。
然后,我们比较当前元素和前一个元素的大小关系,如果当前元素比前一个元素小,则说明数组没有按升序排序,返回false。
如果整个数组都遍历完了,没有发现不按升序排序的情况,则返回true。
相关问题
用public static boolean isSorted()Comparable[[ table)判断对象数组是否已经按升序排序]]
这个方法可以使用Arrays.sort()函数,将对象数组按升序排序,然后通过Arrays.equals()函数比较排序前后的数组是否一致,如果一致则说明已经按升序排序,返回true,否则返回false。以下是示例代码:
public static boolean isSorted(Comparable[] table) {
Comparable[] copy = Arrays.copyOf(table, table.length);
Arrays.sort(copy);
return Arrays.equals(table, copy);
}
哈夫曼树以其编码解码\n要求:\n1.从终端读入字符集大小为n(即字符的个数),逐一输入n个字符和相应的n个权值(即字符出现的频度),建立哈夫曼树,进行编码并且输出。\n将它存于文件hfmtree中(选做)。\n2.利用已建好的哈夫曼编码文件hfmtree,对键盘输入的正文进行译码。输出字符正文,再输出该文的二进制码。\n[测试数据]\n用下表中给出的字符集(n=27)和频度的实际统计数据建立哈夫曼树:\n\n 并实现以下报文的译码和输出:“THIS PROGRAM IS MY FAVORITE”。java
以下是Java代码实现哈夫曼树的编解码:
```java
import java.io.*;
import java.util.*;
class HuffmanTree implements Serializable {
private static final long serialVersionUID = 1L;
private Node root;
private Map<Character, String> codeTable;
private class Node implements Comparable<Node>, Serializable {
private static final long serialVersionUID = 1L;
private char ch;
private int freq;
private Node left;
private Node right;
public Node(char ch, int freq, Node left, Node right) {
this.ch = ch;
this.freq = freq;
this.left = left;
this.right = right;
}
public boolean isLeaf() {
return left == null && right == null;
}
@Override
public int compareTo(Node other) {
return freq - other.freq;
}
}
public HuffmanTree(Map<Character, Integer> freqTable) {
PriorityQueue<Node> pq = new PriorityQueue<>();
for (char ch : freqTable.keySet()) {
pq.offer(new Node(ch, freqTable.get(ch), null, null));
}
while (pq.size() > 1) {
Node left = pq.poll();
Node right = pq.poll();
Node parent = new Node('\0', left.freq + right.freq, left, right);
pq.offer(parent);
}
root = pq.poll();
codeTable = new HashMap<>();
buildCodeTable(root, "");
}
private void buildCodeTable(Node node, String code) {
if (node.isLeaf()) {
codeTable.put(node.ch, code);
} else {
buildCodeTable(node.left, code + "0");
buildCodeTable(node.right, code + "1");
}
}
public void saveToFile(String filename) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeObject(this);
}
}
public static HuffmanTree loadFromFile(String filename) throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
return (HuffmanTree) ois.readObject();
}
}
public String encode(String plainText) {
StringBuilder encoded = new StringBuilder();
for (char ch : plainText.toCharArray()) {
encoded.append(codeTable.get(ch));
}
return encoded.toString();
}
public String decode(String encodedText) {
StringBuilder decoded = new StringBuilder();
Node node = root;
for (char c : encodedText.toCharArray()) {
if (c == '0') {
node = node.left;
} else {
node = node.right;
}
if (node.isLeaf()) {
decoded.append(node.ch);
node = root;
}
}
return decoded.toString();
}
}
public class HuffmanCoding {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Map<Character, Integer> freqTable = new HashMap<>();
freqTable.put('T', 2);
freqTable.put('H', 2);
freqTable.put('I', 2);
freqTable.put('S', 2);
freqTable.put('P', 1);
freqTable.put('R', 2);
freqTable.put('O', 1);
freqTable.put('G', 1);
freqTable.put('A', 1);
freqTable.put('M', 1);
freqTable.put('Y', 1);
freqTable.put('F', 1);
HuffmanTree huffmanTree = new HuffmanTree(freqTable);
huffmanTree.saveToFile("hfmtree");
// Test encoding and decoding
String plainText = "THIS PROGRAM IS MY FAVORITE";
String encodedText = huffmanTree.encode(plainText);
System.out.println("Encoded text: " + encodedText);
HuffmanTree huffmanTree2 = HuffmanTree.loadFromFile("hfmtree");
String decodedText = huffmanTree2.decode(encodedText);
System.out.println("Decoded text: " + decodedText);
System.out.println("Encoded binary: " + Arrays.toString(encodedText.chars().mapToObj(c -> (char) c).map(c -> huffmanTree.codeTable.get(c)).toArray()));
}
}
```
阅读全文