《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学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二