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 08:24:50 浏览: 161

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 &amp;&amp; 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 &amp;&amp; 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.

向AI提问 loading 发送消息图标

相关推荐

大家在看

recommend-type

RPTRead(fname):从 *.rpt 文件读取数据-matlab开发

函数 [d,t,dline,tline]=RPTRead(fname) % 功能: % % 从包含文本和数据的 *.rpt 文件中读取数据% % 信息。内容示例如下: % % **************************************************** ********************** % % * 节点位置报告 * % % **************************************************** ********************** % % 节点位置% % Node ID Coord 1 Value Coord 2 Value Coord 3 Value Reference CID % 0.000000 0.000000 0.000000(全局)矩形% % 4 -0.621540 24.922939
recommend-type

著名的强化学习算法 Proximal Policy Optimization 的另一种自定义实现,也称为 PPO

著名的强化学习算法 Proximal Policy Optimization 的另一种自定义实现,也称为 PPO
recommend-type

基于MATLAB的光栅衍射实验仿真与研究

以光栅衍射为例 , 编写了基于 Matlab 的仿真程序 。 利用 DLL 接口技术 , 结合 Matlab 强大的科学计算功能以及 Visual Basic 的可视化功能 , 通过改变输入参数实现了对光栅衍射 、 单缝衍射 、 杨氏双缝干涉以及多光束干涉的光学实 验进行 生动形象的仿真模拟 。 实验结果的图样细致逼真 , 可为 光 学 的 理 论 分 析 和 实 验 教 学 提 供 新 的 有 效 辅 助 手 段 , 并 为 相 关 课 件 的设计提供了新的途径
recommend-type

ISO 15622 2018 Adaptive cruise control systems (ACC).pdf

自适应巡航系统最新国际标准,适合智能驾驶及ADAS相关研究人员及工程师。
recommend-type

Data Structure and Algorithm in Java (English Version)

数据结构与算法分析:Java语言描述(英文原版)(第二版)

最新推荐

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

Matlab智能算法实践案例集

根据给定文件信息,我们可以得出以下知识点: 1. 智能算法概述: 智能算法是利用计算机模拟人类智能行为的一系列算法。它们在问题解决过程中能够表现出学习、适应、优化和自动化的特点。智能算法广泛应用于数据挖掘、人工智能、模式识别、机器学习、自动化控制等领域。 2. MATLAB简介: MATLAB是一种高性能的数学计算和可视化软件,广泛应用于工程计算、控制设计、信号处理和通信等领域。它提供的强大的工具箱支持,使用户可以方便地进行算法开发、数据分析和可视化工作。 3. MATLAB在智能算法中的应用: 由于MATLAB拥有直观、易用的编程环境和丰富的工具箱,因此它成为了研究和实现智能算法的热门平台。MATLAB中的工具箱,如Fuzzy Logic Toolbox、Neural Network Toolbox、Genetic Algorithm and Direct Search Toolbox等,为智能算法的实现提供了便捷的途径。 4. 智能算法案例研究: 智能算法案例通常是指在某些特定问题领域中应用智能算法解决问题的过程和结果。这些案例可以帮助研究人员和工程师理解算法在实际应用中的效果,并提供解决问题的思路和方法。 5. MATLAB源码的重要性: 在智能算法的学习和研究中,源码是理解算法细节和实现机制的重要途径。阅读和分析源码可以加深对算法工作原理的理解,并可能激发对算法进行改进和创新的想法。 6. 标题“智能算法30个案例”和描述“matlab智能算法30个案例的整本书的源码”暗示了本书可能是一本关于MATLAB环境下智能算法应用的教程或者案例集。它可能按章节组织了30个不同的算法案例,并提供了相应的MATLAB源码。 7. 给定的压缩包子文件的文件名称列表(chapter28、chapter17、chapter05、chapter27、chapter12、chapter15、chapter01、chapter18、chapter21、chapter06)表示这本书被分成了多个章节,每个文件可能包含了对应章节的源码实现。通过这些文件名称,我们可以推测出书中的案例分布情况,例如: - chapter28和chapter27可能探讨了比较接近的主题或问题域,可能是高级智能算法的应用或者是某个具体问题的解决方案。 - chapter01作为第一章,可能是对MATLAB环境或智能算法基础知识的介绍,为后续更复杂的案例学习打基础。 - chapter05、chapter12、chapter15、chapter17、chapter18、chapter21等可能涉及了中间难度级别的智能算法案例,它们可能是对于特定问题的具体解决方法。 8. 标签“matlab算法”进一步强调了本书专注于MATLAB平台上的算法实现,而不仅仅是理论介绍。读者通过这本书能够获得直接在MATLAB环境中实现算法的实际操作经验。 9. 此类书籍对于学生、工程师或者研究人员在研究和应用智能算法时有较高的参考价值。通过学习这些案例,他们不仅能够学习算法的应用,还能够提高自己使用MATLAB进行算法开发的能力。 综上所述,这本书的知识点涉及智能算法的基础和应用,MATLAB工具箱的使用,以及通过实际案例学习算法实现的细节。其内容覆盖了从理论到实践的各个方面,非常适合智能算法的学习者和实践者使用。
recommend-type

【精准测试】:确保分层数据流图准确性的完整测试方法

# 摘要 分层数据流图(DFD)作为软件工程中描述系统功能和数据流动的重要工具,其测试方法论的完善是确保系统稳定性的关键。本文系统性地介绍了分层DFD的基础知识、测试策略与实践、自动化与优化方法,以及实际案例分析。文章详细阐述了测试的理论基础,包括定义、目的、分类和方法,并深入探讨了静态与动态测试方法以及测试用
recommend-type

洛谷B2084python

<think>嗯,用户需要找到洛谷平台上编号B2084的Python解题思路和代码实现。首先,我得确认这个题目的具体内容,因为用户没有提供题目描述。不过根据现有的引用信息,比如引用[4]提到了B2064斐波那契数列的题解,可能B2084属于类似的算法题,但具体题目未知。 首先,我需要查找洛谷B2084的题目内容。假设用户无法直接访问题目,可能需要通过其他途径获取信息。比如,根据编号推断,B开头的题目通常是《深入浅出》系列的练习题,可能涉及基础算法或数学问题。 由于用户提供的引用中有B2064是关于斐波那契数列的递归实现,B2084可能也是一个递归或递推问题,比如计算斐波那契数列的变种,或者
recommend-type

Laravel8Test: 探索Laravel框架的深度与资源

Laravel是一个流行的PHP Web应用程序框架,它深受开发者喜爱的原因在于其优雅的语法和对开发效率的优化。Laravel框架的设计哲学是使开发过程既愉快又富有创造性,为此它内置了许多功能来简化和加速Web应用程序的开发。 1. 后端的多种数据库支持:Laravel支持多种数据库后端,开发者可以在开发过程中选择MySQL、PostgreSQL、SQLite、SQL Server等数据库系统,这让应用程序的数据持久化更加灵活,便于适应不同的项目需求。 2. 富有表现力和直观的代码:Laravel使用MVC(模型-视图-控制器)架构模式,并提供一套直观的API来帮助开发人员构建复杂的应用程序。它的代码库旨在使应用程序的逻辑更加清晰,并允许开发者专注于业务需求而不是底层的重复性代码。 3. 数据库不可知性:Laravel的Eloquent ORM(对象关系映射)使得数据库操作更加直观和简洁,它允许开发者使用PHP代码代替传统的SQL语句来与数据库进行交互。这提高了代码的可移植性和维护性,因为数据库细节对应用程序逻辑是隐藏的。 4. 易于访问和功能强大的工具集:Laravel提供了大量内置功能,比如认证、路由、会话和缓存处理,这些都大大减少了开发工作量并提高了应用程序的性能和安全性。 5. 学习资源:Laravel拥有庞大的学习资源库,包括官方文档和社区提供的教程。Laracasts是一个专门的平台,提供了超过1500个视频教程,这些教程涵盖了Laravel框架的各个方面,以及现代PHP开发、单元测试和JavaScript。这些资源对于初学者和有经验的开发者都极其宝贵,有助于他们快速掌握框架并提高技能。 尽管在描述中未提及具体的标签,但基于标题和描述中的内容,我们可以推测这个文件是关于Laravel8的介绍、学习资源和社区赞助商的信息。由于标题中存在一些不连贯的文本(如“Laravel8Test:拉蒂汉·拉拉维尔7”),可能暗示文件名与实际内容之间存在不一致或者是一个特定版本的介绍。 从文件名称列表“Laravel8Test-master”来看,可以认为这是一个关于Laravel 8测试的项目或教程的主目录,测试是一个软件开发的关键组成部分,确保代码的质量和功能的正确性。 结合以上信息,我们可以了解到Laravel框架的主要特点,它的学习途径以及社区对它的支持。Laravel不断更新,Laravel 8作为最新版本,引入了各种新特性,包括改进的路由缓存、更好的队列工作流程以及与Laravel Sanctum(用于SPA应用的轻量级认证系统)的整合。因此,对于学习和使用Laravel的开发者来说,了解这些更新和对应的测试方法是非常重要的。
recommend-type

【版本控制】:分层数据流图的高效维护与变更管理

# 摘要 本文系统地探讨了版本控制和分层数据流图设计的重要性和应用实践。第一章强调版本控制的基础知识和其在软件开发生命周期中的关键作用。第二章详细介绍了分层数据流图的设计原理,包括基本概念、设计方法和表示技巧,以及如何通过这些图解高效地管理和沟通软件设计。第三章探讨了版本控制系统的选择与配置,比较了不同类型系统的特点,并提供了配置主流系统的实际案例。第四章重点讨论分层数据流图的变更管理流程,阐述
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部