|
|
@@ -1,4 +1,95 @@
|
|
|
package com.example.data_structure.service;
|
|
|
|
|
|
+import com.example.data_structure.vo.Bar;
|
|
|
+import com.example.data_structure.vo.BubbleSortVO;
|
|
|
+import com.example.data_structure.vo.BubbleStep;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+@Service
|
|
|
public class SortingService {
|
|
|
+ private String name; //没有实际意义,但是类必须要有一个属性
|
|
|
+
|
|
|
+
|
|
|
+ public BubbleSortVO getIncBubbleSortResult(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=new ArrayList<Bar>(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,new Bar(aBar.getValue(),aBar.getColor()));
|
|
|
+ stepContent.set(j+1,new Bar(bBar.getValue(),bBar.getColor()));
|
|
|
+
|
|
|
+ ArrayList<Bar>copyCompareStepContent=new ArrayList<Bar>(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=new ArrayList<Bar>(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=new ArrayList<Bar>(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=new ArrayList<Bar>(stepContent);
|
|
|
+ onSortingSteps.add(new BubbleStep("allSorted",copyAllSortedStepContent));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bubbleVO.setOnSortingStates(onSortingSteps);
|
|
|
+ return bubbleVO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|