|
|
@@ -13,6 +13,8 @@ public class SortingService {
|
|
|
private String name; //没有实际意义,但是类必须要有一个属性
|
|
|
|
|
|
|
|
|
+
|
|
|
+ //获取增序冒泡排序结果
|
|
|
public BubbleSortVO getIncBubbleSortResult(ArrayList<Integer> content){
|
|
|
|
|
|
int len=content.size();
|
|
|
@@ -35,7 +37,7 @@ public class SortingService {
|
|
|
|
|
|
//设置每一步的状态
|
|
|
ArrayList<BubbleStep> onSortingSteps=new ArrayList<BubbleStep>();
|
|
|
- ArrayList<Bar>stepContent=new ArrayList<Bar>(initialStepContent);
|
|
|
+ ArrayList<Bar>stepContent=BubbleStep.copyContent(initialStepContent);
|
|
|
//int len=content.size();
|
|
|
for(int i=len-1;i>0;i--){
|
|
|
for(int j=0;j<i;j++){
|
|
|
@@ -44,10 +46,10 @@ public class SortingService {
|
|
|
Bar bBar=stepContent.get(j+1);
|
|
|
aBar.setColor("green");
|
|
|
bBar.setColor("green");
|
|
|
- stepContent.set(j,new Bar(aBar.getValue(),aBar.getColor()));
|
|
|
- stepContent.set(j+1,new Bar(bBar.getValue(),bBar.getColor()));
|
|
|
+ stepContent.set(j,aBar);
|
|
|
+ stepContent.set(j+1,bBar);
|
|
|
|
|
|
- ArrayList<Bar>copyCompareStepContent=new ArrayList<Bar>(stepContent);
|
|
|
+ ArrayList<Bar>copyCompareStepContent=BubbleStep.copyContent(stepContent);
|
|
|
onSortingSteps.add(new BubbleStep("onCompare",copyCompareStepContent));
|
|
|
|
|
|
//若逆序
|
|
|
@@ -56,7 +58,7 @@ public class SortingService {
|
|
|
Bar tempBar=aBar;
|
|
|
stepContent.set(j,bBar);
|
|
|
stepContent.set(j+1,tempBar);
|
|
|
- ArrayList<Bar> copyExchangeStepContent=new ArrayList<Bar>(stepContent);
|
|
|
+ ArrayList<Bar> copyExchangeStepContent=BubbleStep.copyContent(stepContent);
|
|
|
onSortingSteps.add(new BubbleStep("onExchange",copyExchangeStepContent));
|
|
|
}
|
|
|
|
|
|
@@ -74,7 +76,7 @@ public class SortingService {
|
|
|
readyBar.setColor("orange");
|
|
|
stepContent.set(i,readyBar);
|
|
|
if(i!=1) {
|
|
|
- ArrayList<Bar> copyBeforeCompareStepContent=new ArrayList<Bar>(stepContent);
|
|
|
+ ArrayList<Bar> copyBeforeCompareStepContent=BubbleStep.copyContent(stepContent);
|
|
|
onSortingSteps.add(new BubbleStep("beforeCompare", copyBeforeCompareStepContent));
|
|
|
}else{
|
|
|
for(int n=0;n<stepContent.size();n++){
|
|
|
@@ -82,7 +84,7 @@ public class SortingService {
|
|
|
curBar.setColor("blue");
|
|
|
stepContent.set(n,curBar);
|
|
|
}
|
|
|
- ArrayList<Bar> copyAllSortedStepContent=new ArrayList<Bar>(stepContent);
|
|
|
+ ArrayList<Bar> copyAllSortedStepContent=BubbleStep.copyContent(stepContent);
|
|
|
onSortingSteps.add(new BubbleStep("allSorted",copyAllSortedStepContent));
|
|
|
}
|
|
|
}
|
|
|
@@ -92,4 +94,83 @@ public class SortingService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ //获取降序冒泡排序结果
|
|
|
+ public BubbleSortVO getDesBubbleSortResult(ArrayList<Integer> content){
|
|
|
+
|
|
|
+ int len=content.size();
|
|
|
+
|
|
|
+ //设置初始化的状态
|
|
|
+ ArrayList<Bar>initialStepContent=new ArrayList<Bar>();
|
|
|
+ BubbleSortVO bubbleVO=new BubbleSortVO();
|
|
|
+
|
|
|
+ for(int m=0;m<len;m++){
|
|
|
+ Bar bar=new Bar(content.get(m),"blue");
|
|
|
+ initialStepContent.add(bar);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("========initialStepContent========="+initialStepContent.toString());
|
|
|
+
|
|
|
+ BubbleStep currentStep=new BubbleStep("beforeCompare",initialStepContent);
|
|
|
+ bubbleVO.setInitial(currentStep);
|
|
|
+ System.out.println("=========getInitialAttribute=========="+bubbleVO.getInitial());
|
|
|
+
|
|
|
+ //设置每一步的状态
|
|
|
+ ArrayList<BubbleStep> onSortingSteps=new ArrayList<BubbleStep>();
|
|
|
+ ArrayList<Bar>stepContent=BubbleStep.copyContent(initialStepContent);
|
|
|
+ //int len=content.size();
|
|
|
+ for(int i=len-1;i>0;i--){
|
|
|
+ for(int j=0;j<i;j++){
|
|
|
+ //比较相邻两个值
|
|
|
+ Bar aBar=stepContent.get(j);
|
|
|
+ Bar bBar=stepContent.get(j+1);
|
|
|
+ aBar.setColor("green");
|
|
|
+ bBar.setColor("green");
|
|
|
+ stepContent.set(j,aBar);
|
|
|
+ stepContent.set(j+1,bBar);
|
|
|
+
|
|
|
+ ArrayList<Bar>copyCompareStepContent=BubbleStep.copyContent(stepContent);
|
|
|
+ onSortingSteps.add(new BubbleStep("onCompare",copyCompareStepContent));
|
|
|
+
|
|
|
+ //若逆序
|
|
|
+ if(content.get(j)<content.get(j+1)){
|
|
|
+ //交换位置
|
|
|
+ Bar tempBar=aBar;
|
|
|
+ stepContent.set(j,bBar);
|
|
|
+ stepContent.set(j+1,tempBar);
|
|
|
+ ArrayList<Bar> copyExchangeStepContent=BubbleStep.copyContent(stepContent);
|
|
|
+ onSortingSteps.add(new BubbleStep("onExchange",copyExchangeStepContent));
|
|
|
+ }
|
|
|
+
|
|
|
+ //比较和交换后,恢复到未选中状态
|
|
|
+ aBar=stepContent.get(j);
|
|
|
+ bBar=stepContent.get(j+1);
|
|
|
+ aBar.setColor("blue");
|
|
|
+ bBar.setColor("blue");
|
|
|
+ stepContent.set(j,aBar);
|
|
|
+ stepContent.set(j+1,bBar);
|
|
|
+
|
|
|
+ }
|
|
|
+ //一趟比较和交换后多了一个已排序好的Bar
|
|
|
+ Bar readyBar=stepContent.get(i);
|
|
|
+ readyBar.setColor("orange");
|
|
|
+ stepContent.set(i,readyBar);
|
|
|
+ if(i!=1) {
|
|
|
+ ArrayList<Bar> copyBeforeCompareStepContent=BubbleStep.copyContent(stepContent);
|
|
|
+ onSortingSteps.add(new BubbleStep("beforeCompare", copyBeforeCompareStepContent));
|
|
|
+ }else{
|
|
|
+ for(int n=0;n<stepContent.size();n++){
|
|
|
+ Bar curBar=stepContent.get(n);
|
|
|
+ curBar.setColor("blue");
|
|
|
+ stepContent.set(n,curBar);
|
|
|
+ }
|
|
|
+ ArrayList<Bar> copyAllSortedStepContent=BubbleStep.copyContent(stepContent);
|
|
|
+ onSortingSteps.add(new BubbleStep("allSorted",copyAllSortedStepContent));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bubbleVO.setOnSortingStates(onSortingSteps);
|
|
|
+ return bubbleVO;
|
|
|
+ }
|
|
|
+
|
|
|
}
|