java中quicksort的参数_更改我的quickSort算法Java中的数据透视

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

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

int l = start;

int r = end;

int pivotIndex = start; //

// 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值