java中的Hoare分区正确性 [英] Hoare Partition Correctness in java

查看:62
本文介绍了java中的Hoare分区正确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将 Hoare 分区实现为快速排序.我试图完全理解 hoare 分区,但这本书并没有解释一切.主要是我只是想知道 while TRUE 部分是什么意思?本书摘录如下.如果我将 while 部分放在 java 中,我会使用什么,为什么?

I am working to implement a Hoare partition into a quicksort. I am trying to completely understand the hoare partition but the book doesnt explain everything. Mainly I am just wondering what the while TRUE part means? The excerpt from the book is below. If I were to put the while part in java what would I use and why?

Hoare-Partition(A,p,r)
    x= A[p]
    i = p-1
    j=r+1
    while true
        repeat
            j=j-1
        until A [j] <= x
        repeat
            i = i +1
        until A[i] >= x
        if i < l
           exchange A[i] with A[j]
        else return j

推荐答案

试试上面的代码:

int HoarePartition (int a[],int p, int r) 
{
    int x=a[p],i=p-1,j=r+1;
    while (true) 
    {
        while (a[j] <= x) j--; 
        while (a[i] >= x) i++;
        if  (i < j) swap(a[i],a[j]);
        else return j;
    }
}

这篇关于java中的Hoare分区正确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆