Develop a NetBeans application that illustrates the applicability of one sorting algorithm on a collection of 10 items. The algorithm should sort the items in descending order. The algorithm must work based on the principle of one of the four sorting algorithms studied during the semester. The collection of 10 items is defined and populated in the application code. You can decide the type and values of items stored in the collection. The algorithm must be implemented as part of a class called MyArrayList that extends the ArrayList class functionality with your sorting algorithm. [20 marks] 5. Extend the application implemented for Question 4 above to be able to measure the efficiency of the implemented sorting algorithm in term of number of comparisons performed by the algorithm. The application should display the total number of comparisons performed by the algorithm for a given collection of items. [10 marks] 6. In an Excel graph, plot the number of comparisons measured by the sorting algorithm when three different sizes of collection are considered. Submit your Excel file on Moodle. [20 marks]

时间: 2023-08-21 21:24:50 浏览: 144
Sure, I understand your question. You want me to develop a NetBeans application that demonstrates the use of one sorting algorithm on a collection of 10 items, sorting them in descending order. The algorithm should be based on one of the four sorting algorithms studied during the semester and should be implemented as part of a class called MyArrayList that extends the ArrayList class functionality with your sorting algorithm. Additionally, you want me to extend the application to measure the efficiency of the implemented sorting algorithm in terms of the number of comparisons performed by the algorithm, and to plot the number of comparisons measured by the sorting algorithm when three different sizes of collection are considered in an Excel graph. To start, let's define and populate the collection of 10 items in the NetBeans application code. We can use integers for the items stored in the collection. Here's an example of how the collection can be defined and populated: ``` import java.util.ArrayList; public class SortingDemo { public static void main(String[] args) { MyArrayList<Integer> list = new MyArrayList<Integer>(); list.add(5); list.add(2); list.add(10); list.add(8); list.add(1); list.add(7); list.add(9); list.add(3); list.add(4); list.add(6); System.out.println("Original list: " + list); list.sortDescending(); System.out.println("Sorted list: " + list); } } class MyArrayList<E extends Comparable<E>> extends ArrayList<E> { public void sortDescending() { // Insertion sort algorithm for (int i = 1; i < size(); i++) { E key = get(i); int j = i - 1; while (j >= 0 && get(j).compareTo(key) < 0) { set(j + 1, get(j)); j--; } set(j + 1, key); } } } ``` In the above code, we define a class called SortingDemo which contains a main method. Inside the main method, we create an instance of MyArrayList and add 10 integers to it. We then print out the original list and call the sortDescending method on the list to sort it in descending order. Finally, we print out the sorted list. The MyArrayList class extends the ArrayList class and defines a sortDescending method that implements the insertion sort algorithm to sort the list in descending order. Next, we need to extend the application to measure the efficiency of the sorting algorithm in terms of the number of comparisons performed by the algorithm. We can modify the MyArrayList class to keep track of the number of comparisons performed during the sorting process, like this: ``` class MyArrayList<E extends Comparable<E>> extends ArrayList<E> { private int comparisons; public void sortDescending() { comparisons = 0; // Insertion sort algorithm for (int i = 1; i < size(); i++) { E key = get(i); int j = i - 1; while (j >= 0 && get(j).compareTo(key) < 0) { set(j + 1, get(j)); j--; comparisons++; } set(j + 1, key); } } public int getComparisons() { return comparisons; } } ``` We added a private variable called comparisons to keep track of the number of comparisons performed during the sorting process. We initialize it to 0 at the start of the sortDescending method and increment it for each comparison performed. We also added a public method called getComparisons to retrieve the number of comparisons performed. Finally, we need to plot the number of comparisons measured by the sorting algorithm when three different sizes of collection are considered in an Excel graph. To do this, we can modify the SortingDemo class to create three different lists of different sizes and sort each of them, recording the number of comparisons performed for each sort. We can then export the data to an Excel file and create a graph with the data. Here's an example of the modified SortingDemo class: ``` import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class SortingDemo { public static void main(String[] args) { MyArrayList<Integer> list1 = new MyArrayList<Integer>(); list1.add(5); list1.add(2); list1.add(10); list1.add(8); list1.add(1); list1.add(7); list1.add(9); list1.add(3); list1.add(4); list1.add(6); System.out.println("Original list 1: " + list1); list1.sortDescending(); System.out.println("Sorted list 1: " + list1); int comparisons1 = list1.getComparisons(); MyArrayList<Integer> list2 = new MyArrayList<Integer>(); for (int i = 0; i < 100; i++) { list2.add((int) (Math.random() * 100)); } System.out.println("Original list 2: " + list2); list2.sortDescending(); System.out.println("Sorted list 2: " + list2); int comparisons2 = list2.getComparisons(); MyArrayList<Integer> list3 = new MyArrayList<Integer>(); for (int i = 0; i < 1000; i++) { list3.add((int) (Math.random() * 100)); } System.out.println("Original list 3: " + list3); list3.sortDescending(); System.out.println("Sorted list 3: " + list3); int comparisons3 = list3.getComparisons(); System.out.println("Comparisons 1: " + comparisons1); System.out.println("Comparisons 2: " + comparisons2); System.out.println("Comparisons 3: " + comparisons3); try (XSSFWorkbook workbook = new XSSFWorkbook()) { FileOutputStream out = new FileOutputStream(new File("sorting.xlsx")); org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet("Sorting"); Row headerRow = sheet.createRow(0); Cell cell0 = headerRow.createCell(0); cell0.setCellValue("Collection Size"); Cell cell1 = headerRow.createCell(1); cell1.setCellValue("Comparisons"); Row row1 = sheet.createRow(1); Cell cell10 = row1.createCell(0); cell10.setCellValue("10"); Cell cell11 = row1.createCell(1); cell11.setCellValue(comparisons1); Row row2 = sheet.createRow(2); Cell cell20 = row2.createCell(0); cell20.setCellValue("100"); Cell cell21 = row2.createCell(1); cell21.setCellValue(comparisons2); Row row3 = sheet.createRow(3); Cell cell30 = row3.createCell(0); cell30.setCellValue("1000"); Cell cell31 = row3.createCell(1); cell31.setCellValue(comparisons3); workbook.write(out); out.close(); System.out.println("Excel file written successfully."); } catch (IOException e) { e.printStackTrace(); } } } ``` In the above code, we create three different lists of sizes 10, 100, and 1000, and sort each of them using the MyArrayList class. We record the number of comparisons performed for each sort and print them to the console. We then create an Excel file and write the data to it, creating a graph with the data. To create the graph in Excel, open the sorting.xlsx file and select the data in cells A1:B3. Click on the Insert tab and select the type of graph you want to create. You can then customize the graph as needed.
阅读全文

相关推荐

最新推荐

recommend-type

Create a Java EE 6 Application with JSF 2, EJB 3.1, JPA, and NetBeans IDE 7

在本教程中,我们将深入探讨如何使用Java EE 6中的核心技术——JavaServer Faces 2.0 (JSF),Enterprise Java Beans 3.1 (包括Session Bean和Message-Driven Bean)以及Java Persistence API (JPA),结合NetBeans IDE...
recommend-type

Java MeteoInfo教程-V1.0.docx

【Java MeteoInfo教程-V1.0】是一个针对MeteoInfo Java版的二次开发教程,使用NetBeans IDE 7.3作为开发环境。这个教程适用于已经安装了Java SE开发工具包JDK 6 Update 26或更高版本,或者JDK 7 Update 10或更高版本...
recommend-type

韩顺平java从入门到精通视频教程(全94讲)学习笔记整理(齐全).docx(446页那个)

初学者通常从简单的文本编辑器(如记事本)开始,然后逐渐过渡到高级的集成开发环境(IDE),如Eclipse、NetBeans等。这些IDE提供了丰富的功能,有助于提高开发效率。 **Java程序的生命周期** 1. **编写源文件**:...
recommend-type

韩顺平java从入门到精通视频教程(全94讲)学习笔记整理(齐全).docx

* 记事本、JCreator、JBuilder、NetBeans、Eclipse 等开发工具的介绍 * 如何选择合适的开发工具 Java 语言的历史 * Java 语言的诞生和发展 * James Gosling 和 Sun 公司的贡献 * Java 语言的版本发展历程 Java ...
recommend-type

NetBeans IDE 7.0安装使用说明.docx

NetBeans IDE 7.0 是一个强大的集成开发环境,尤其针对C/C++开发者,它支持在多种操作系统上构建和调试应用程序。NetBeans IDE的核心特性包括模块化的Java编程基础,允许跨平台兼容性,如Windows、Macintosh OS X、...
recommend-type

平尾装配工作平台运输支撑系统设计与应用

资源摘要信息:"该压缩包文件名为‘行业分类-设备装置-用于平尾装配工作平台的运输支撑系统.zip’,虽然没有提供具体的标签信息,但通过文件标题可以推断出其内容涉及的是航空或者相关重工业领域内的设备装置。从标题来看,该文件集中讲述的是有关平尾装配工作平台的运输支撑系统,这是一种专门用于支撑和运输飞机平尾装配的特殊设备。 平尾,即水平尾翼,是飞机尾部的一个关键部件,它对于飞机的稳定性和控制性起到至关重要的作用。平尾的装配工作通常需要在一个特定的平台上进行,这个平台不仅要保证装配过程中平尾的稳定,还需要适应平尾的搬运和运输。因此,设计出一个合适的运输支撑系统对于提高装配效率和保障装配质量至关重要。 从‘用于平尾装配工作平台的运输支撑系统.pdf’这一文件名称可以推断,该PDF文档应该是详细介绍这种支撑系统的构造、工作原理、使用方法以及其在平尾装配工作中的应用。文档可能包括以下内容: 1. 支撑系统的设计理念:介绍支撑系统设计的基本出发点,如便于操作、稳定性高、强度大、适应性强等。可能涉及的工程学原理、材料学选择和整体结构布局等内容。 2. 结构组件介绍:详细介绍支撑系统的各个组成部分,包括支撑框架、稳定装置、传动机构、导向装置、固定装置等。对于每一个部件的功能、材料构成、制造工艺、耐腐蚀性以及与其他部件的连接方式等都会有详细的描述。 3. 工作原理和操作流程:解释运输支撑系统是如何在装配过程中起到支撑作用的,包括如何调整支撑点以适应不同重量和尺寸的平尾,以及如何进行运输和对接。操作流程部分可能会包含操作步骤、安全措施、维护保养等。 4. 应用案例分析:可能包含实际操作中遇到的问题和解决方案,或是对不同机型平尾装配过程的支撑系统应用案例的详细描述,以此展示系统的实用性和适应性。 5. 技术参数和性能指标:列出支撑系统的具体技术参数,如载重能力、尺寸规格、工作范围、可调节范围、耐用性和可靠性指标等,以供参考和评估。 6. 安全和维护指南:对于支撑系统的使用安全提供指导,包括操作安全、应急处理、日常维护、定期检查和故障排除等内容。 该支撑系统作为专门针对平尾装配而设计的设备,对于飞机制造企业来说,掌握其详细信息是提高生产效率和保障产品质量的重要一环。同时,这种支撑系统的设计和应用也体现了现代工业在专用设备制造方面追求高效、安全和精确的趋势。"
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB遗传算法探索:寻找随机性与确定性的平衡艺术

![MATLAB多种群遗传算法优化](https://img-blog.csdnimg.cn/39452a76c45b4193b4d88d1be16b01f1.png) # 1. 遗传算法的基本概念与起源 遗传算法(Genetic Algorithm, GA)是一种模拟自然选择和遗传学机制的搜索优化算法。起源于20世纪60年代末至70年代初,由John Holland及其学生和同事们在研究自适应系统时首次提出,其理论基础受到生物进化论的启发。遗传算法通过编码一个潜在解决方案的“基因”,构造初始种群,并通过选择、交叉(杂交)和变异等操作模拟生物进化过程,以迭代的方式不断优化和筛选出最适应环境的
recommend-type

如何在S7-200 SMART PLC中使用MB_Client指令实现Modbus TCP通信?请详细解释从连接建立到数据交换的完整步骤。

为了有效地掌握S7-200 SMART PLC中的MB_Client指令,以便实现Modbus TCP通信,建议参考《S7-200 SMART Modbus TCP教程:MB_Client指令与功能码详解》。本教程将引导您了解从连接建立到数据交换的整个过程,并详细解释每个步骤中的关键点。 参考资源链接:[S7-200 SMART Modbus TCP教程:MB_Client指令与功能码详解](https://wenku.csdn.net/doc/119yes2jcm?spm=1055.2569.3001.10343) 首先,确保您的S7-200 SMART CPU支持开放式用户通
recommend-type

MAX-MIN Ant System:用MATLAB解决旅行商问题

资源摘要信息:"Solve TSP by MMAS: Using MAX-MIN Ant System to solve Traveling Salesman Problem - matlab开发" 本资源为解决经典的旅行商问题(Traveling Salesman Problem, TSP)提供了一种基于蚁群算法(Ant Colony Optimization, ACO)的MAX-MIN蚁群系统(MAX-MIN Ant System, MMAS)的Matlab实现。旅行商问题是一个典型的优化问题,要求找到一条最短的路径,让旅行商访问每一个城市一次并返回起点。这个问题属于NP-hard问题,随着城市数量的增加,寻找最优解的难度急剧增加。 MAX-MIN Ant System是一种改进的蚁群优化算法,它在基本的蚁群算法的基础上,对信息素的更新规则进行了改进,以期避免过早收敛和局部最优的问题。MMAS算法通过限制信息素的上下界来确保算法的探索能力和避免过早收敛,它在某些情况下比经典的蚁群系统(Ant System, AS)和带有局部搜索的蚁群系统(Ant Colony System, ACS)更为有效。 在本Matlab实现中,用户可以通过调用ACO函数并传入一个TSP问题文件(例如"filename.tsp")来运行MMAS算法。该问题文件可以是任意的对称或非对称TSP实例,用户可以从特定的网站下载多种标准TSP问题实例,以供测试和研究使用。 使用此资源的用户需要注意,虽然该Matlab代码可以免费用于个人学习和研究目的,但若要用于商业用途,则需要联系作者获取相应的许可。作者的电子邮件地址为***。 此外,压缩包文件名为"MAX-MIN%20Ant%20System.zip",该压缩包包含Matlab代码文件和可能的示例数据文件。用户在使用之前需要将压缩包解压,并将文件放置在Matlab的适当工作目录中。 为了更好地理解和应用该资源,用户应当对蚁群优化算法有初步了解,尤其是对MAX-MIN蚁群系统的基本原理和运行机制有所掌握。此外,熟悉Matlab编程环境和拥有一定的编程经验将有助于用户根据个人需求修改和扩展算法。 在实际应用中,用户可以根据问题规模调整MMAS算法的参数,如蚂蚁数量、信息素蒸发率、信息素增量等,以获得最优的求解效果。此外,也可以结合其他启发式或元启发式算法,如遗传算法、模拟退火等,来进一步提高算法的性能。 总之,本资源为TSP问题的求解提供了一种有效的算法框架,且Matlab作为编程工具的易用性和强大的计算能力,使得该资源成为算法研究人员和工程技术人员的有力工具。通过本资源的应用,用户将能够深入探索并实现蚁群优化算法在实际问题中的应用,为解决复杂的优化问题提供一种新的思路和方法。