# 求助 大神帮忙看看 诡异的循环终止条件

qq_30676355 2015-10-06 09:24:51
``````package Algorithms.Sort;

import com.algs4.stdlib.StdDraw;

import java.awt.*;
import java.util.Arrays;

/**
* Created by surfacepc on 2015/10/6.
*/
public class Qucik {

private static int N = 15;
private static int left = -1;
private static int right = -1;

public static void sort(int[] a,int lo, int hi) {
if (hi <= lo) {
return;
}
int j = patition(a, lo, hi);
sort(a, lo, j - 1);
sort(a, j + 1, hi);
}
private static int patition(int []a ,int lo, int hi){
int i=lo;
int j=hi+1;
int v=a[lo];

while(true){
while(a[++i]<v){
if (i==hi){
left=i;
break;
}
}
while(a[--j]>v){
if(j==lo){
right=j;
break;
}
}
if (i>=j){
break;
}

int temp=a[i];
a[i]=a[j];
a[j]=temp;

StdDraw.show(1000);
StdDraw.clear();
for (int m = 0; m < N; m++) {
if (m == left || m == right) {
StdDraw.setPenColor(Color.GREEN);
StdDraw.line(m, 0, m, a[m]);
} else {
StdDraw.setPenColor(Color.red);
StdDraw.line(m, 0, m, a[m]);
}

}

StdDraw.show(0);

}
left=lo;
right=j;

int temp=a[lo];
a[lo]=a[j];
a[j]=temp;

StdDraw.show(1000);
StdDraw.clear();
for (int m = 0; m < N; m++) {
if (m == left || m == right) {
StdDraw.setPenColor(Color.BLUE);
StdDraw.line(m, 0, m, a[m]);
} else {
StdDraw.setPenColor(Color.red);
StdDraw.line(m, 0, m, a[m]);
}

}

StdDraw.show(0);
return j;

}

public static void main(String[] args) {
StdDraw.setCanvasSize(2000, 1200);
StdDraw.setXscale(0, N);
StdDraw.setYscale(0, N);

StdDraw.line(0, 0, N, 0);

int [] array=new int [N];
for (int i = 0; i <N ; i++) {
array[i]=(int)(Math.random()*N);
}
sort(array,0,N-1);
System.out.println(Arrays.toString(array));
}

}

``````

...全文
147 3 打赏 收藏 转发到动态 举报

3 条回复

a327369238 2015-10-07
• 打赏
• 举报

qq_30676355 2015-10-06
• 打赏
• 举报

qq_30676355 2015-10-06
• 打赏
• 举报

35行到40行，看不明白。求讲解

50,451

javaspring bootspring cloud 技术论坛（原bbs）

• 近7日
• 近30日
• 至今