对字符串数组使用快速排序

2024-03-29

我是一名编程学生,我不会发布整个作业,而是请求帮助解决我已经尝试了几个小时才能理解的问题。我的任务是使用快速排序方法对字符串数组进行排序。作为这个问题的一部分,我承担的其他所有任务都很好,但是当我通过打印字符串数组来测试排序方法时,它完全混乱了,没有任何看起来的押韵或理由。请帮助我找出代码中的错误,或者我忽略的几个明显的错误。提供的字符串数组是包含 65 个名称的列表:http://pastebin.com/jRrgeV1E http://pastebin.com/jRrgeV1E该方法的代码如下:

private static void quickSort(String[] a, int start, int end)
{
        // index for the "left-to-right scan"
        int i = start;
        // index for the "right-to-left scan"
        int j = end;

        // only examine arrays of 2 or more elements.
        if (j - i >= 1)
        {
            // The pivot point of the sort method is arbitrarily set to the first element int the array.
            String pivot = a[i];
            // only scan between the two indexes, until they meet.
            while (j > i)
            {
                // from the left, if the current element is lexicographically less than the (original)
                // first element in the String array, move on. Stop advancing the counter when we reach
                // the right or an element that is lexicographically greater than the pivot String.
                while (a[i].compareTo(pivot) < 0 && i <= end && j > i){
                    i++;
                }
                // from the right, if the current element is lexicographically greater than the (original)
                // first element in the String array, move on. Stop advancing the counter when we reach
                // the left or an element that is lexicographically less than the pivot String.
                while (a[j].compareTo(pivot) > 0 && j >= start && j >= i){
                    j--;
                }
                // check the two elements in the center, the last comparison before the scans cross.
                if (j > i)
                    swap(a, i, j);
            }
            // At this point, the two scans have crossed each other in the center of the array and stop.
            // The left partition and right partition contain the right groups of numbers but are not
            // sorted themselves. The following recursive code sorts the left and right partitions.

            // Swap the pivot point with the last element of the left partition.
            swap(a, start, j);
            // sort left partition
            quickSort(a, start, j - 1);
            // sort right partition
            quickSort(a, j + 1, end);
        }
    }
    /**
     * This method facilitates the quickSort method's need to swap two elements, Towers of Hanoi style.
     */
    private static void swap(String[] a, int i, int j)
    {
    String temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }

好吧,我误认为它会起作用并发现了你的小错误。

看一眼维基百科伪代码 http://de.wikipedia.org/wiki/Quicksort

您会注意到 while 循环中的条件导致了错误

如果你改变(a[i].compareTo(pivot) < 0 && i <= end && j > i) and (a[j].compareTo(pivot) > 0 && j >= start && j >= i) to

(a[i].compareTo(pivot) <= 0 && i < end && j > i) and (a[j].compareTo(pivot) >= 0 && j > start && j >= i).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对字符串数组使用快速排序 的相关文章

  • Wicket setResponsePage() 方法如何工作?

    在学习 JSP 和 servlet 时 我听说了重定向和调度 他们中的哪一个做 Wicket 的setResponsePage 履行 What setResponsePage确实取决于几个因素 您调用 setResponsePage 的次数
  • Hibernate - 一对多关系和孤儿删除级联

    我有一个基本的一对多关系父 子关系 就像 Hibernate 参考书第 21 章中一样 级联仅从子级到父级 保留级联只是因为我不想在删除子级时删除父级 当我向父级添加一个子级并保存该子级时 出现 TransientObjectExcepti
  • 按任意顺序对 SQL 行输出进行排序?

    因此 在我的数据库中 我存储乐器名称 以及各种其他属性 比方说id是主键 并且name是唯一的密钥 在 PHP 脚本中 我按仪器类别选择项目 如下所示 name mysql real escape string POST name row
  • 如何处理过时的连接?

    我们的应用程序是一个 J2EE 应用程序 在 Websphere 6 1 上通过 Mainframe DB2 后端使用 Struts EJB Hibernate 最近已投入生产 我们收到过时的连接异常当用户第一次或有时登录应用程序时 此异常
  • 在 IntelliJ IDEA 中哪里添加像 -ea 这样的编译器选项?

    我想添加 ea选项 我把它设置在Project Settings gt Compiler gt Java Compiler Additional command line parameters 但它导致了 make 错误 invalid f
  • Java HTTPS客户端证书认证

    我对HTTPS SSL TLS我对客户在使用证书进行身份验证时到底应该提供什么内容感到有点困惑 我正在编写一个 Java 客户端 需要执行一个简单的操作POST数据到特定的URL 这部分工作正常 唯一的问题是它应该重新完成HTTPS The
  • JUnit 5 中的参数化 beforeEach/beforeAll

    我想为一个小型的类似数据库的应用程序编写一个测试 此应用程序使用查询 查询应返回正确的结果 这在 JUnit 5 中很容易实现 比如 BeforeEach void before database prepareDatabase Test
  • 为什么实现接口的类与 Java 中的接口不属于同一类型?

    I have out load output transactions columnHeaders dataFormat Where load定义为 public boolean load String outputfile List
  • java中调用父构造函数

    我有两节课Parent and Child 而Parent有一个需要 3 个参数的构造函数 class Parent public Parent String host String path int port 现在我想要Child构造函数
  • 我们如何找到 C# 整数数组中的项目计数?

    我需要在 C 数组中查找类型为整数的项目计数 我的意思是 int intArray new int 10 int 0 34 int 1 65 int 2 98 intArray 的项目计数为 3 我在下面找到了 strArray 的代码 但
  • 为什么枚举可以有包私有构造函数?

    既然枚举构造函数只能由其常量调用 为什么允许它是包私有的呢 构造函数实际上不是包私有的 它是隐式的private接口方法的隐式方式public即使您不添加关键字 JLS 的相关部分 8 8 3 http docs oracle com ja
  • Array.filter 与 $filter('filter')

    我应该在 Angular 应用程序中使用哪一个 为什么 array filter o gt o name myName or filter filter array name myName true 关键的区别是快捷方式或语法糖由提供 fi
  • Perl - 将数组元素句子与变量进行比较

    我使用 grep 返回临时F 文件和 arrayWarning 之间不匹配的数组 my c grep map 1 temporaryF arrayWarning c 里面有很多行 例如 Sun Sep 30 00 05 55 fibre c
  • Ruby 将不可打印的字符转换为数字

    我有一个包含不可打印字符的字符串 我目前正在做的是将它们替换为波浪号 使用 string gsub print 但是 我实际上想将它们转换为整数值 我尝试了这个 但它总是输出0 string gsub print 1 to i 想法 字符串
  • 事务 TransactionImple ActionStatus.ABORTED 已回滚

    我实现了DTO业务从TomEE到JBoss的迁移 我有这个实体 NamedQueries NamedQuery name common plagebusiness plage getAllPlages query SELECT p FROM
  • 连接空字符串来进行字符串转换真的那么糟糕吗?

    假设我有两个char变量 稍后我想将它们连接成一个字符串 我就是这样做的 char c1 c2 String s c1 c2 我见过有人说 trick 是 丑陋的 等等 你应该使用String valueOf or Character to
  • 使用 libGDX 写入 Json

    我是 Json 和 libGDX 的新手 但我创建了一个简单的游戏 我想将玩家姓名及其分数存储在 Json 文件中 有没有办法做到这一点 我想创建一个 Json 文件Gdx files localStorage如果它不存在 如果存在 则向其
  • 有什么方法可以使 jQuery.inArray() 不区分大小写吗?

    标题概括了这一点 如果有人想要使用更集成的方法jquery questions tagged jquery function extend Case insensative inArray http api jquery com jquer
  • 为什么不建议将常量存储在单独的类中?

    有人告诉我 我在其他一些地方也看到过这种说法 不建议将常量存储在 Java 中的单独类中 以便在其他类中使用它们 但我没有看到任何地方为什么会这样 我不应该将它们存储在自己的接口 类中的原因是什么 我从 C 转到 Java 在 C 中我只想
  • 在 MATLAB 中一次为元胞数组分配不同的值

    我需要有关在 MATLAB 中创建元胞数组的帮助 其中每个元胞都是不同大小的数组 例如 假设我有这个简单的数组和值 A 5 3 8 7 0 4 1 B 10 元胞数组C必须创建为 C 10 20 30 40 50 10 20 30 10 2

随机推荐

  • 在python中将进度值发送到进度条

    在我的游戏中我有两个模块 岛 py它将岛屿加载到我的游戏中 第二个模块是gui py它在游戏开始之前处理 GUI 小部件 我的问题是如何将 island py 模块中的进度值发送到中创建的进度栏gui py module 编辑 还可以使用加
  • Google Appengine 游标

    我正在使用两者ndb and search api我的 python appengine 项目中的查询 我能找到的关于游标的唯一官方文档 https cloud google com appengine docs python datast
  • Java:CopyOnWriteArrayList 与 SynchronizedList

    有什么区别CopyOnWritearraylist and Collections synchronizedList 什么时候应该优先选择其中一个 CopyOnWriteArrayList当读取次数远远超过写入次数时 应使用列表 这是因为您
  • 特殊字符问题

    如何更改 Android 上的字体以允许显示 或 等特殊字符 实际上包含这些字符的字符串存储在sqlite数据库中 当您将文本加载到您的TextView 这对你有用吗 textView setText new String textFrom
  • 在字符串 Objective-c 中连接字符串

    我想将一个字符串放在一个字符串中 基本上是伪代码 first part of string varying string third part of string 我怎样才能在 Objective C 中做到这一点 有没有办法在 obj c
  • Erlang:如何从体内引用匿名函数?

    In Erlang http en wikipedia org wiki Erlang programming language 有没有办法引用当前正在执行的函数 这对于产生无限循环很有用 spawn fun gt do something
  • Azure 搜索和破折号

    我正在使用 Azure 搜索并尝试对文档执行搜索 看起来好像是这样做的 indexes blah docs api version 2015 02 28 search abc 1003 返回与此相同的结果 indexes blah docs
  • 在 OpenSceneGraph 中创建球体(使用 osg::Geometry)

    我花了相当长的时间才使其正常工作 但我的球体无法显示 使用以下代码来实现我的功能 使用 Visual C 在 Opengl 中创建 3D 球体 https stackoverflow com questions 5988686 creati
  • 禁用 jQuery 移动按钮

    我正在尝试禁用此按钮 a Next a 单击事件不应触发 并且按钮 UI 还应反映按钮禁用状态 我尝试过以下方法 next attr disabled true next attr disabled disabled next button
  • 如何处理管道中的$null

    我的 PowerShell 代码中经常遇到以下情况 我有一个返回对象集合的函数或属性 或者 null 如果将结果推入管道 则还可以处理管道中的元素 如果 null是唯一的元素 例子 Project Features Foreach Obje
  • Python Xpath:lxml.etree.XPathEvalError:谓词无效

    我正在尝试学习如何抓取网页 在教程中我使用下面的代码抛出此错误 lxml etree XPathEvalError Invalid predicate 我正在查询的网站是 不要评判我 它是训练视频中使用的网站 https itunes ap
  • Visual Studio 2015 的 Git 问题

    我在使用 TortoiseGit 版本 1 8 16 0 git 版本 2 6 2 windows 1 创建的 git 存储库中有一个 Visual Studio 解决方案 我刚刚将 Visual Studio 从 2015 年更新到 20
  • 将解密的文件读入 ZipInputStream 有时会截断第一个文件

    我正在开发一个电子阅读器应用程序 使用skyepub https skyepub net 基本上将加密的书籍下载到文件系统中 并将解密密钥保存在数据库中 当用户尝试阅读它时 它将书籍加载到内存中并解密 问题是有些书的第一章被截断了 epub
  • 带有预检请求的 CORS 帖子

    我正在尝试使用 CORS 将文件上传到不同域上的服务 但由于来源被拒绝 它们一直失败 据我所知 正在使用正确的标头来允许此操作 JavaScript 请求 var xhr new XMLHttpRequest xhr open POST h
  • 使用 Microsoft Graph API 授予管理员同意 - Java

    我已经使用图形 API 创建了一个应用程序 并为它们分配了权限 委托和应用程序 ServicePrincipal servicePrincipal graphClient servicePrincipals resSerPrinId bui
  • 如何将 Google 地图置于纬度和经度位置的中心?

    考虑以下代码 stores click function console log this data latitude 1754 26265626 console log this data longitude 65 262518 cons
  • C# 4.0 的新酷功能 [已关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 你们正在寻
  • Laravel - 如何在 AppServiceProvider 中获取当前用户

    所以我通常让当前用户使用Auth user 当我确定用户是否实际登录时Auth check 但这似乎不适用于我的AppServiceProvider 我用它来跨所有视图共享数据 我var dump both Auth user and Au
  • 无法在 Mac OS X 10.8 上使用 Homebrew FreeTds 捆绑安装tiny_tds

    我的问题我可以采取哪些万无一失的步骤来 100 使其正常工作 我需要真正的指示 而不是简单的答案或对该过程的模糊概念描述 让我们来一探究竟 似乎某些地方存在冲突 并且我从 GitHub 上的 gem 开发人员那里获得了与我使用 Ruby R
  • 对字符串数组使用快速排序

    我是一名编程学生 我不会发布整个作业 而是请求帮助解决我已经尝试了几个小时才能理解的问题 我的任务是使用快速排序方法对字符串数组进行排序 作为这个问题的一部分 我承担的其他所有任务都很好 但是当我通过打印字符串数组来测试排序方法时 它完全混