《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

相关文章

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操