当前位置: 首页 > 面试题库 >

更改我的quickSort算法Java中的数据透视

梁兴修
2023-03-14
问题内容

我使用数组中的第一个元素作为枢轴实现了一个有效的quickSort算法,如下所示:

public int[] quickSort( int[] a, int start, int end){

    int l = start;
    int r = end;

    int pivotIndex = start; //<---- first element in the array as pivot!

    // must be at least two elements
    if ( end - start >= 1){

        // set pivot
        int pivot = a[pivotIndex];


        while ( r > l ){
            // scan from the left
            while ( a[l] <= pivot && l <= end && r > l  ){
                l++;
            }
            while ( a[r] > pivot && r >= start && r >= l){
                r--;
            }
            if ( r > l ){
                this.swap(a, l, r);
            }
        }
        this.swap(a, pivotIndex, r);

        System.out.println("calling quickSort on " + start + " and " + (r-1));                 
        quickSort(a, pivotIndex, r - 1); // quicksort the left partition
        System.out.println("calling quickSort on " + (r+1) + " and " + end);
        quickSort(a, r + 1, end);   // quicksort the right partition

    } else {
        return a;
    }

    return a;
}

效果很好,但是如果我将更pivotIndex改为可以int pivotIndex = end;得到以下结果:

run:
2, 8, 7, 1, 3, 5, 6, 4, 
2, 8, 7, 1, 3, 5, 6, 4, 
swapping l:8 and r:4
2, 4, 7, 1, 3, 5, 6, 8, 
swapping l:7 and r:3
2, 4, 3, 1, 7, 5, 6, 8, 
swapping l:8 and r:1
calling quickSort on 0 and 2
calling quickSort on 4 and 7
2, 4, 3, 8, 7, 5, 6, 1, 
swapping l:7 and r:1
2, 4, 3, 8, 1, 5, 6, 7, 
swapping l:7 and r:1
calling quickSort on 4 and 3
calling quickSort on 5 and 7
2, 4, 3, 8, 7, 5, 6, 1, 
swapping l:5 and r:1
2, 4, 3, 8, 7, 1, 6, 5, 
swapping l:5 and r:1
calling quickSort on 5 and 4
calling quickSort on 6 and 7
2, 4, 3, 8, 7, 5, 6, 1, 
swapping l:6 and r:1
2, 4, 3, 8, 7, 5, 1, 6, 
swapping l:6 and r:1
calling quickSort on 6 and 5
calling quickSort on 7 and 7
2, 4, 3, 8, 7, 5, 6, 1, 
BUILD SUCCESSFUL (total time: 1 second)

我如何使算法与数据透视表一起用作任何索引 0 to a.length


问题答案:

您可以在开始排序之前简单地将所选的数据透视图与数组中的第一个元素交换,这样它将完全像以前一样工作。

int l = start;
int r = end;

this.swap(a, choosePivot(), start); 
int pivotIndex = start;


 类似资料:
  • 我已经理解了quicksort算法中的分区部分是如何完成的,但是我在理解quicksort递归函数时遇到了麻烦。谁能一步一步地给我解释一下它是怎么工作的吗?这里粘贴的是C++代码。 我的逻辑到目前为止(一步一步)是这样的:

  • 问题内容: 为了刷新一些Java,我尝试实现一种可以对整数数组进行排序的quicksort(inplace)算法。以下是到目前为止的代码。您可以通过拨打电话。 如果两个“指针”均指向与支点值相同的数组条目,则该代码显然会失败(陷入无限循环)。枢轴元素始终是当前分区的最右边(索引最大的分区)。 但是我无法弄清楚如何避免这种情况,有人看到解决方案了吗? 问题答案: 这应该可以工作( 稍后将检查正确性

  • 我想修改QuickSort(在Java中),这样每次调用Partition时,都会以比例数组的中值作为支点。 我在Java中有一个中值选择算法,它返回第k个最小的元素,在本例中是中值。我有大量的java快速排序算法,它们都可以独立工作,对数组进行排序。不幸的是,我不能结合这两者来实现上述目标...每次我尝试它,我通常得到堆栈溢出错误。 任何人都可以向我展示代码,看看它是如何完成的吗? 谢谢 编辑:

  • 我正在做一个拉拉维尔应用程序。在laravel中,我试图更新透视表中的一行。在此应用程序中,我有三个表: 问题:id,标题,描述 类别:id、名称 category_issues:(pivot table)id,issue_id,category_id 在laravel中,我试图更新透视表中的一行。我有以下关系: issue.php public function categories(){ re

  • 问题内容: 我正在使用node.js服务器在Web应用程序和数据库之间创建一个“接近实时”套接字。当前,我使用的是MySQL,它每秒在节点中轮询一次,以检查表是否有任何更改(基于时间戳)。 我想知道是否有使用MySQL进行此类操作的特定技术?目前,我只是在运行SQL查询并在下一次轮询之前使用setTimeout。 我知道在这样的实例中使用NoSQL数据库更为普遍,但我对技术并不满意,我宁愿使用SQ

  • 我对变量的作用域有问题。 我希望输出为,但结果是。为什么我在方法中改变了数组中的值,而原来的数组却改变了?