快速排序有问题,求修改

CHENJIN252 2019-06-30 10:27:04
package fistSort;

public class FistSort {
private static void sort(int[] a ,int low ,int high) {
int start = low;
int end = high;
int key = a[low];


while (end > start){
while (end > start&& a[end] >=key) {
end-- ;
if (a[end] <= key) {
int temp = a[end];
a[end] = a[start];
a[start] = temp;
}
while (end > start&&a[start] <=key) {
start++ ;
if (a[start] >= key) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
}
// 递归
if (start > low) sort(a, low, start-1);
if (end < high ) sort(a, end+1, high);

}
}
}


// 主函数

public static void main(String[] args) {
System.out.println("快速排序!!!");
int[] a = { 12, 20, 5, 16, 15, 1, 30, 45, 23,9};
int start = 0;
int end = a.length-1;
sort(a , start, end);
for (int i = 0; i < a.length; i++) {
System.out.println( a[i]);

}
}
}
...全文
121 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2019-07-01
  • 打赏
  • 举报
回复
因为start 和end 2个变量在程序中已经改变了,你debug一下,肯定不是你想要的数值。递归方法需要保存,初始值。
qq_39936465 2019-07-01
  • 打赏
  • 举报
回复
首先你先确定你要用那种排序法,感觉程序很乱。先搞明白自己要用的算法,再开始写排序比较好
cwmlow 2019-07-01
  • 打赏
  • 举报
回复
死循环了吧。。。第二层while循环条件改下

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧