📄 guibingpaixu.java
字号:
}
}
public void drawOneBar(Graphics g, int i)
{
if(i < 0 || i >= aSize)
return;
int j = theArray[i].getHeight();
int k = 35 + i * (barWidth + barSeparation);
int l = 230 - j;
Color color = theArray[i].getColor();
g.setColor(Color.CYAN);
g.fillRect(k, 30, barWidth, 200);
g.setColor(color);
g.fill3DRect(k, l, barWidth, j, true);
if(aSize == 24)
{
g.setColor(Color.black);
int i1 = i <= 9 ? k + 5 : k;
g.drawString(String.valueOf(i), i1, 228);
}
}
public void draw(Graphics g)
{
/*count=(comps+copies+aSize)*0.01;*/
if(drawMode == 2)
{
g.setColor(Color.CYAN);
g.fillRect(0, -200, 1000, 500);
for(int i = 0; i < aSize; i++)
drawOneBar(g, i);
} else
if(drawMode == 1)
drawOneBar(g, (lowerBoundM + jM) - 1);
g.setColor(Color.CYAN);
g.fillRect(0, 0, 120, 32);
g.setColor(Color.black);
g.drawString("Comparisons = " + comps, 10, 28);
g.drawString("Copies = " + copies, 10, 15);
g.setColor(Color.CYAN);
g.fillRect(0, 230, 1000, 78);
if(aSize == 24)
{
arrowText(g, Color.red, "lower", lower, 1, true, true);
arrowText(g, Color.red, "upper", upper, 2, true, true);
int j = (lowerBoundM + jM) - 1;
if(oldCodePart == 9)
arrowText(g, Color.magenta, "ptr", lowerBoundM, 3, true, true);
else
if(oldCodePart == 10)
arrowText(g, Color.magenta, "ptr", j, 3, true, true);
else
arrowText(g, Color.blue, "mid", mid, 3, true, true);
arrowText(g, Color.black, note, 0, 4, false, true);
} else
{
arrowText(g, Color.red, "xxx", lower, 1, true, false);
arrowText(g, Color.red, "xxx", upper, 2, true, false);
arrowText(g, Color.blue, "xxx", mid, 3, true, false);
}
drawMode = 2;
}
public void paixuStep()
{
if(codePart==1)
{
theseParams = new params(0, aSize - 1, 0, 8);
theStack.push(theseParams);
note = "Initial call to mergeSort";
drawMode = 0;
oldCodePart = codePart;
codePart = 2;
return;
}else if(codePart==2){
theseParams = theStack.peek();
lower = theseParams.lower;
upper = theseParams.upper;
note = "Entering mergeSort: " + lower + "-" + upper;
oldCodePart = codePart;
if(lower == upper)
codePart = 7;
else
codePart = 4;
drawMode = 0;
return;
}else if(codePart==3){
return;
}else if(codePart==4){
mid = (lower + upper) / 2;
note = "Will sort lower half: " + lower + "-" + mid;
params params1 = new params(lower, mid, mid, 5);
theStack.push(params1);
drawMode = 0;
oldCodePart = codePart;
codePart = 2;
return;
}else if(codePart==5){
theseParams = theStack.peek();
lower = theseParams.lower;
upper = theseParams.upper;
mid = (lower + upper) / 2;
note = "Will sort upper half: " + (mid + 1) + "-" + upper;
params params2 = new params(mid + 1, upper, mid, 6);
theStack.push(params2);
drawMode = 0;
oldCodePart = codePart;
codePart = 2;
return;
}else if(codePart==6){
theseParams = theStack.peek();
lower = theseParams.lower;
upper = theseParams.upper;
mid = (lower + upper) / 2;
note = "Will merge ranges";
lowPtrM = lower;
highPtrM = mid + 1;
upperBoundM = upper;
drawMode = 0;
oldCodePart = codePart;
codePart = 9;
return;
}else if(codePart==7){
oldCodePart = codePart;
codePart = theseParams.codePart;
theStack.pop();
if(!theStack.isEmpty())
{
theseParams = theStack.peek();
note = "Exiting mergeSort: " + lower + "-" + upper;
} else
{
note = "Exciting mergeSort; sort is complete";
}
drawMode = 0;
return;
}else if(codePart==8){
doneFlag = true;
note = "Sort is complete; Press New or Size";
drawMode = 0;
oldCodePart = codePart;
codePart = 1;
return;
}else if(codePart==9){
jM = 0;
lowerBoundM = lowPtrM;
midM = highPtrM - 1;
nM = (upperBoundM - lowerBoundM) + 1;
note = "Merged " + lowPtrM + "-" + midM + " and " + highPtrM + "-" + upperBoundM + " into workspace";
while(lowPtrM <= midM && highPtrM <= upperBoundM)
{
comps++;
copies++;
if(theArray[lowPtrM].getHeight() < theArray[highPtrM].getHeight())
workSpace[jM++] = theArray[lowPtrM++];
else
workSpace[jM++] = theArray[highPtrM++];
}
while(lowPtrM <= midM)
{
copies++;
workSpace[jM++] = theArray[lowPtrM++];
}
while(highPtrM <= upperBoundM)
{
copies++;
workSpace[jM++] = theArray[highPtrM++];
}
mergingFlag = true;
jM = 0;
oldCodePart = codePart;
codePart = 10;
drawMode = 0;
return;
}else{
oldCodePart = codePart;
if(jM == nM)
{
note = "Merge completed";
codePart = 7;
drawMode = 0;
return;
} else
{
copies++;
theArray[lowerBoundM + jM] = workSpace[jM];
note = "Copied workspace into " + (lowerBoundM + jM);
jM++;
codePart = 10;
drawMode = 1;
return;
}
}
}
}
/* */
class person//单柱类
{
public Color getColor()//色彩的获得
{
return color;
}
public int getHeight()//高度的获得
{
return height;
}
public person(int i, Color color1)//单柱构造器
{
height = i;
color = color1;
}
private int height;
private Color color;
}
/**************************************/
class params//指针类
{
public params(int i, int j, int k, int l)
{
lower = i;
upper = j;
mid = k;
codePart = l;
}
public int lower;
public int upper;
public int mid;
public int codePart;
}
/**********************************************/
class stack//堆栈类
{
public stack(int i)
{
maxSize = i;
stackArray = new params[maxSize];
top = -1;
}
public void push(params params1)
{
stackArray[++top] = params1;
}
public params pop() //弹出
{
return stackArray[top--];
}
public params peek()
{
return stackArray[top];
}
public boolean isEmpty()
{
return top == -1;
}
private int maxSize;
private params stackArray[];//基于数组的指针
private int top;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -