《Java语言程序设计与数据结构》编程练习答案(第二十三章)(一)

本文主要是介绍《Java语言程序设计与数据结构》编程练习答案(第二十三章)(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《Java语言程序设计与数据结构》编程练习答案(第二十三章)(一)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

23.1

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);}public static <T extends Comparable<T>> void bubbleSort(T[] list){for(int i=list.length-1;i>0;i--){for(int j=0;j<i;j++){if(list[j].compareTo(list[j+1]) > 0){T tmp = list[j];list[j] = list[j+1];list[j+1] = tmp;}}}}public static <T> void bubbleSort (T[] list, Comparator<? super T> comparator){for(int i=list.length-1;i>0;i--){for(int j=0;j<i;j++){if(comparator.compare(list[j],list[j+1]) > 0){T tmp = list[j];list[j] = list[j+1];list[j+1] = tmp;}}}}
}

23.2

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);}public static <T extends Comparable<T>> void mergeSort(T[] list){if(list.length > 1){ArrayList<T> tmpLeft = new ArrayList<>(Arrays.asList(list).subList(0, (int) (list.length / 2)));T[] firstHalf = (T[]) tmpLeft.toArray();mergeSort(firstHalf);ArrayList<T> tmpRight = new ArrayList<>(Arrays.asList(list).subList(list.length / 2, list.length));T[] secondHalf = (T[]) tmpRight.toArray();mergeSort(secondHalf);merge(firstHalf, secondHalf, list);}}public static <T extends Comparable<T>>void merge(T[] list1, T[] list2, T[]tmp){int current1 = 0;int current2 = 0;int current3 = 0;while(current1 < list1.length && current2 < list2.length){if(list1[current1].compareTo(list2[current2]) < 0){tmp[current3++] = list1[current1++];}else{tmp[current3++] = list2[current2++];}}while(current1 < list1.length)tmp[current3++] = list1[current1++];while(current2 < list2.length)tmp[current3++] = list2[current2++];}public static <T> void mergeSort(T[] list, Comparator<T> comparator){if(list.length > 1){ArrayList<T> tmpLeft = new ArrayList<>(Arrays.asList(list).subList(0, (int) (list.length / 2)));T[] firstHalf = (T[]) tmpLeft.toArray();mergeSort(firstHalf, comparator);ArrayList<T> tmpRight = new ArrayList<>(Arrays.asList(list).subList(list.length / 2, list.length));T[] secondHalf = (T[]) tmpRight.toArray();mergeSort(secondHalf, comparator);merge(firstHalf, secondHalf, list, comparator);}}public static <T>void merge(T[] list1, T[] list2, T[]tmp, Comparator<T> comparator){int current1 = 0;int current2 = 0;int current3 = 0;while(current1 < list1.length && current2 < list2.length){if(comparator.compare(list1[current1], list2[current2]) < 0){tmp[current3++] = list1[current1++];}else{tmp[current3++] = list2[current2++];}}while(current1 < list1.length)tmp[current3++] = list1[current1++];while(current2 < list2.length)tmp[current3++] = list2[current2++];}
}

23.3

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);}public static <T extends Comparable<T>> void quickSort(T[] list){quickSort(list, 0, list.length-1);}public static <T extends Comparable<T>> void quickSort(T[] list, int first, int last){if(last > first){int pivotIndex = partition(list,first, last);quickSort(list, first, pivotIndex-1);quickSort(list, pivotIndex+1, last);}}public static <T extends Comparable<T>>int partition(T[] list, int first, int last){T pivot = list[first];int low = first + 1;int high = last;while(high > low){while(low <= high && list[low].compareTo(pivot) <= 0)low++;while(low <= high && list[high].compareTo(pivot) > 0)high--;if(high > low){T tmp = list[high];list[high] = list[low];list[low] = tmp;}}while (high > first && list[high].compareTo(pivot) >= 0)high--;if(pivot.compareTo(list[high]) > 0){list[first] = list[high];list[high] = pivot;return high;}else{return first;}}public static <T> void quickSort(T[] list, Comparator<T> comparator){quickSort(list, 0, list.length-1, comparator);}public static <T> void quickSort(T[] list, int first, int last, Comparator<T> comparator){if(last > first){int pivotIndex = partition(list,first, last, comparator);quickSort(list, first, pivotIndex-1, comparator);quickSort(list, pivotIndex+1, last, comparator);}}public static <T>int partition(T[] list, int first, int last, Comparator<T> comparator){T pivot = list[first];int low = first + 1;int high = last;while(high > low){while(low <= high && comparator.compare(list[low], pivot) <= 0)low++;while(low <= high && comparator.compare(list[high],pivot) > 0)high--;if(high > low){T tmp = list[high];list[high] = list[low];list[low] = tmp;}}while (high > first && comparator.compare(list[high], pivot) >= 0)high--;if(comparator.compare(pivot, list[high]) > 0){list[first] = list[high];list[high] = pivot;return high;}else{return first;}}
}

23.4

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);}public static void quickSort(int[] list){quickSort(list, 0, list.length-1);}public static void quickSort(int[] list, int first, int last){if(last > first){int pivotIndex = partition(list, first, last);quickSort(list, first, pivotIndex-1);quickSort(list, pivotIndex+1, last);}}public static int partition(int[] list, int first, int last){int mid = (first + last) / 2;if((list[first]<list[last] && list[first]>list[mid])||(list[first]<list[mid] && list[first]>list[last])){}else if((list[mid]<list[first] && list[mid]>list[last])||(list[mid]<list[last] && list[mid]>list[first])){int tmp = list[first];list[first] = list[mid];list[mid] = tmp;}else{int tmp = list[first];list[first] = list[last];list[last] = tmp;}int pivot = list[first];int low = first + 1;int high = last;while(high > low){while(low <= high && list[low] <= pivot)low++;while(low <= high && list[high] > pivot)high--;if(high > low){int tmp = list[high];list[high] = list[low];list[low] = tmp;}}while(high > first && list[high] >= pivot)high--;if(pivot > list[high]){list[first] = list[high];list[high] = pivot;return high;}else{return first;}}
}

23.5

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);}}class HeapWithComparator<T> {private ArrayList<T> list = new ArrayList<>();private Comparator<? super T>comparator;public HeapWithComparator(){this.comparator = new Comparator<T>() {@Overridepublic int compare(T o1, T o2) {return o1.toString().compareTo(o2.toString());}};}public HeapWithComparator(Comparator<? super T> comparator){this.comparator = comparator;}public void add(T newObject){list.add(newObject);int currentIndex = list.size()-1;while(currentIndex > 0){int parentIndex = (currentIndex-1) / 2;if(comparator.compare(list.get(currentIndex),list.get(parentIndex)) > 0){T tmp = list.get(currentIndex);list.set(currentIndex, list.get(parentIndex));list.set(parentIndex, tmp);}else{break;}currentIndex = parentIndex;}}public T remove(){if(list.size() == 0)return null;T removeObject = list.get(0);list.set(0, list.get(list.size()-1));list.remove(list.size()-1);int currentIndex = 0;while(currentIndex < list.size()){int leftChildIndex = 2*currentIndex + 1;int rightChildIndex = 2*currentIndex + 2;if(leftChildIndex >= list.size())break;int maxIndex = leftChildIndex;if(rightChildIndex < list.size()){if(comparator.compare(list.get(maxIndex), list.get(rightChildIndex)) < 0){maxIndex = rightChildIndex;}}if(comparator.compare(list.get(currentIndex), list.get(maxIndex)) < 0){T tmp = list.get(maxIndex);list.set(maxIndex, list.get(currentIndex));list.set(currentIndex, tmp);currentIndex = maxIndex;}else{break;}}return removeObject;}public int getSize(){return list.size();}}

23.6

public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);}public static boolean ordered(int[] list){boolean isOrdered = true;for(int i=0;i<list.length-1;i++){if(list[i] > list[i+1]){isOrdered = false;break;}}return isOrdered;}public static boolean ordered(int[] list, boolean ascending){boolean isOrdered = true;if(ascending){for(int i=0;i<list.length-1;i++){if(list[i] > list[i+1]){isOrdered = false;break;}}}else{for(int i=0;i<list.length-1;i++){if(list[i] < list[i+1]){isOrdered = false;break;}}}return isOrdered;}public static boolean ordered(double[] list){boolean isOrdered = true;for(int i=0;i<list.length-1;i++){if(list[i] > list[i+1]){isOrdered = false;break;}}return isOrdered;}public static boolean ordered(double[] list, boolean ascending){boolean isOrdered = true;if(ascending){for(int i=0;i<list.length-1;i++){if(list[i] > list[i+1]){isOrdered = false;break;}}}else{for(int i=0;i<list.length-1;i++){if(list[i] < list[i+1]){isOrdered = false;break;}}}return isOrdered;}public static <T extends Comparable<T>> boolean ordered(T[] list){boolean isOrdered = true;for(int i=0;i<list.length-1;i++){if(list[i].compareTo(list[i+1]) > 0){isOrdered = false;break;}}return isOrdered;}public static <T extends Comparable<T>> boolean ordered(T[] list, boolean ascending){boolean isOrdered = true;if(ascending){for(int i=0;i<list.length-1;i++){if(list[i].compareTo(list[i+1]) > 0){isOrdered = false;break;}}}else{for(int i=0;i<list.length-1;i++){if(list[i].compareTo(list[i+1]) < 0){isOrdered = false;break;}}}return isOrdered;}public static <T> boolean ordered(T[] list, Comparator<? super T> comparator){boolean isOrdered = true;for(int i=0;i<list.length-1;i++){if(comparator.compare(list[i], list[i+1]) > 0){isOrdered = false;break;}}return isOrdered;}public static <T> boolean ordered(T[] list, Comparator<? super T> comparator, boolean ascending){boolean isOrdered = true;if(ascending){for(int i=0;i<list.length-1;i++){if(comparator.compare(list[i], list[i+1]) > 0){isOrdered = false;break;}}}else {for(int i=0;i<list.length-1;i++){if(comparator.compare(list[i], list[i+1]) < 0){isOrdered = false;break;}}}return isOrdered;}
}

这篇关于《Java语言程序设计与数据结构》编程练习答案(第二十三章)(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/766358

相关文章

Java实现Excel与HTML互转

《Java实现Excel与HTML互转》Excel是一种电子表格格式,而HTM则是一种用于创建网页的标记语言,虽然两者在用途上存在差异,但有时我们需要将数据从一种格式转换为另一种格式,下面我们就来看看... Excel是一种电子表格格式,广泛用于数据处理和分析,而HTM则是一种用于创建网页的标记语言。虽然两

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

Java访问修饰符public、private、protected及默认访问权限详解

《Java访问修饰符public、private、protected及默认访问权限详解》:本文主要介绍Java访问修饰符public、private、protected及默认访问权限的相关资料,每... 目录前言1. public 访问修饰符特点:示例:适用场景:2. private 访问修饰符特点:示例:

详解Java如何向http/https接口发出请求

《详解Java如何向http/https接口发出请求》这篇文章主要为大家详细介绍了Java如何实现向http/https接口发出请求,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用Java发送web请求所用到的包都在java.net下,在具体使用时可以用如下代码,你可以把它封装成一

SpringBoot使用Apache Tika检测敏感信息

《SpringBoot使用ApacheTika检测敏感信息》ApacheTika是一个功能强大的内容分析工具,它能够从多种文件格式中提取文本、元数据以及其他结构化信息,下面我们来看看如何使用Ap... 目录Tika 主要特性1. 多格式支持2. 自动文件类型检测3. 文本和元数据提取4. 支持 OCR(光学

Java内存泄漏问题的排查、优化与最佳实践

《Java内存泄漏问题的排查、优化与最佳实践》在Java开发中,内存泄漏是一个常见且令人头疼的问题,内存泄漏指的是程序在运行过程中,已经不再使用的对象没有被及时释放,从而导致内存占用不断增加,最终... 目录引言1. 什么是内存泄漏?常见的内存泄漏情况2. 如何排查 Java 中的内存泄漏?2.1 使用 J

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

使用SQL语言查询多个Excel表格的操作方法

《使用SQL语言查询多个Excel表格的操作方法》本文介绍了如何使用SQL语言查询多个Excel表格,通过将所有Excel表格放入一个.xlsx文件中,并使用pandas和pandasql库进行读取和... 目录如何用SQL语言查询多个Excel表格如何使用sql查询excel内容1. 简介2. 实现思路3