Kaynağa Gözat

implement functions regaring bubble sort.

171870002 5 yıl önce
ebeveyn
işleme
151110bd63
21 değiştirilmiş dosya ile 345 ekleme ve 0 silme
  1. 6 0
      src/main/java/com/example/data_structure/common/Response.java
  2. 3 0
      src/main/java/com/example/data_structure/controller/GranphController.java
  3. 5 0
      src/main/java/com/example/data_structure/controller/HanoiController.java
  4. 3 0
      src/main/java/com/example/data_structure/controller/SortingController.java
  5. 4 0
      src/main/java/com/example/data_structure/controller/StackController.java
  6. 5 0
      src/main/java/com/example/data_structure/domain/Graph.java
  7. 6 0
      src/main/java/com/example/data_structure/domain/Hanoi.java
  8. 6 0
      src/main/java/com/example/data_structure/domain/Sorting.java
  9. 6 0
      src/main/java/com/example/data_structure/domain/Stack.java
  10. 5 0
      src/main/java/com/example/data_structure/service/GraphService.java
  11. 5 0
      src/main/java/com/example/data_structure/service/HanoiService.java
  12. 91 0
      src/main/java/com/example/data_structure/service/SortingService.java
  13. 5 0
      src/main/java/com/example/data_structure/service/StackService.java
  14. 46 0
      src/main/java/com/example/data_structure/vo/Bar.java
  15. 49 0
      src/main/java/com/example/data_structure/vo/BubbleSortVO.java
  16. 47 0
      src/main/java/com/example/data_structure/vo/BubbleStep.java
  17. 10 0
      src/main/java/com/example/data_structure/vo/GraphVO.java
  18. 4 0
      src/main/java/com/example/data_structure/vo/HanoiVO.java
  19. 5 0
      src/main/java/com/example/data_structure/vo/SortingVO.java
  20. 4 0
      src/main/java/com/example/data_structure/vo/StackVO.java
  21. 30 0
      src/test/java/com/example/data_structure/service/SortingServiceTest.java

+ 6 - 0
src/main/java/com/example/data_structure/common/Response.java

@@ -1,5 +1,8 @@
 package com.example.data_structure.common;
 
+import org.springframework.stereotype.Component;
+
+@Component
 public class Response {
 
     private Object content;
@@ -30,6 +33,9 @@ public class Response {
         this.content=null;
     }
 
+    public Response() {
+    }
+
     public static Response buildSuccess(){
         return null;
     }

+ 3 - 0
src/main/java/com/example/data_structure/controller/GranphController.java

@@ -1,4 +1,7 @@
 package com.example.data_structure.controller;
 
+import org.springframework.stereotype.Controller;
+
+@Controller
 public class GranphController {
 }

+ 5 - 0
src/main/java/com/example/data_structure/controller/HanoiController.java

@@ -1,4 +1,9 @@
 package com.example.data_structure.controller;
 
+
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Controller;
+
+@Controller
 public class HanoiController {
 }

+ 3 - 0
src/main/java/com/example/data_structure/controller/SortingController.java

@@ -1,4 +1,7 @@
 package com.example.data_structure.controller;
 
+import org.springframework.stereotype.Controller;
+
+@Controller
 public class SortingController {
 }

+ 4 - 0
src/main/java/com/example/data_structure/controller/StackController.java

@@ -1,4 +1,8 @@
 package com.example.data_structure.controller;
 
+
+import org.springframework.stereotype.Controller;
+
+@Controller
 public class StackController {
 }

+ 5 - 0
src/main/java/com/example/data_structure/domain/Graph.java

@@ -1,7 +1,10 @@
 package com.example.data_structure.domain;
 
+import org.springframework.stereotype.Component;
+
 import java.util.ArrayList;
 
+@Component
 public class Graph {
     private boolean isDirected; //是否有向
     private boolean isWeighted; //是否带权
@@ -50,6 +53,8 @@ public class Graph {
         this.weight = weight;
     }
 
+    public Graph() {
+    }
 
     /**
      * 获取计算最小生成树过程中每一步的结果,返回一个结果列表

+ 6 - 0
src/main/java/com/example/data_structure/domain/Hanoi.java

@@ -1,5 +1,8 @@
 package com.example.data_structure.domain;
 
+import org.springframework.stereotype.Component;
+
+@Component
 public class Hanoi {
     private int n; //圆盘个数
     private String a; //需移动位置
@@ -21,6 +24,9 @@ public class Hanoi {
         this.c = c;
     }
 
+    public Hanoi() {
+    }
+
     /*getter*/
 
     public int getN() {

+ 6 - 0
src/main/java/com/example/data_structure/domain/Sorting.java

@@ -1,7 +1,10 @@
 package com.example.data_structure.domain;
 
+import org.springframework.stereotype.Component;
+
 import java.util.ArrayList;
 
+@Component
 public class Sorting {
     private int size; //元素个数
     private String elementType; //可以有 String,int,double,char 四种值
@@ -20,6 +23,9 @@ public class Sorting {
         this.isDesc = isDesc;
     }
 
+    public Sorting() {
+    }
+
     /**
      *判断是否需要交换顺序(结合规则是否为降序和比较结果判断)
      * @param compareResult a b比较的结果

+ 6 - 0
src/main/java/com/example/data_structure/domain/Stack.java

@@ -1,7 +1,10 @@
 package com.example.data_structure.domain;
 
+import org.springframework.stereotype.Component;
+
 import java.util.ArrayList;
 
+@Component
 public class Stack {
 
     public static final int MAX_SIZE =20;
@@ -32,6 +35,9 @@ public class Stack {
         this.content = content;
     }
 
+    public Stack() {
+    }
+
     private boolean isFull(){
         return false;
     }

+ 5 - 0
src/main/java/com/example/data_structure/service/GraphService.java

@@ -1,4 +1,9 @@
 package com.example.data_structure.service;
 
+
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+@Service
 public class GraphService {
 }

+ 5 - 0
src/main/java/com/example/data_structure/service/HanoiService.java

@@ -1,4 +1,9 @@
 package com.example.data_structure.service;
 
+
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+@Service
 public class HanoiService {
 }

+ 91 - 0
src/main/java/com/example/data_structure/service/SortingService.java

@@ -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;
+    }
+
+
 }

+ 5 - 0
src/main/java/com/example/data_structure/service/StackService.java

@@ -1,4 +1,9 @@
 package com.example.data_structure.service;
 
+
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+@Service
 public class StackService {
 }

+ 46 - 0
src/main/java/com/example/data_structure/vo/Bar.java

@@ -0,0 +1,46 @@
+package com.example.data_structure.vo;
+
+
+import org.springframework.stereotype.Component;
+
+@Component
+public class Bar {
+    private int value;
+    private String color;
+
+    public Bar(int value, String color) {
+        this.value = value;
+        this.color = color;
+    }
+
+    public Bar() {
+    }
+
+    //getter
+    public int getValue() {
+        return value;
+    }
+
+    public String getColor() {
+        return color;
+    }
+
+    //setter
+    public void setValue(int value) {
+        this.value = value;
+    }
+
+    public void setColor(String color) {
+        this.color = color;
+    }
+
+    //toString
+
+    @Override
+    public String toString() {
+        return "Bar{" +
+                "value=" + value +
+                ", color='" + color + '\'' +
+                '}';
+    }
+}

+ 49 - 0
src/main/java/com/example/data_structure/vo/BubbleSortVO.java

@@ -0,0 +1,49 @@
+package com.example.data_structure.vo;
+
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+
+
+@Component
+public class BubbleSortVO {
+    private BubbleStep initial;
+    private ArrayList<BubbleStep> onSortingStates;
+
+    public BubbleSortVO(BubbleStep initial, ArrayList<BubbleStep> onSortingStates) {
+        this.initial = initial;
+        this.onSortingStates = onSortingStates;
+    }
+
+    public BubbleSortVO() {
+    }
+
+    //getter
+    public BubbleStep getInitial() {
+        return initial;
+    }
+
+    public ArrayList<BubbleStep> getOnSortingStates() {
+        return onSortingStates;
+    }
+
+
+    //setter
+    public void setInitial(BubbleStep initial) {
+        this.initial = initial;
+    }
+
+    public void setOnSortingStates(ArrayList<BubbleStep> onSortingStates) {
+        this.onSortingStates = onSortingStates;
+    }
+
+    //toString
+
+    @Override
+    public String toString() {
+        return "BubbleSortVO{" +
+                "initial=" + initial +
+                ", onSortingStates=" + onSortingStates +
+                '}';
+    }
+}

+ 47 - 0
src/main/java/com/example/data_structure/vo/BubbleStep.java

@@ -0,0 +1,47 @@
+package com.example.data_structure.vo;
+
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+
+@Component
+public class BubbleStep {
+    private String type; //有四个值:onCompare,onExchange,beforeCompare,allSorted
+    private ArrayList<Bar> content;
+
+    public BubbleStep(String type, ArrayList<Bar> content) {
+        this.type = type;
+        this.content = content;
+    }
+
+    public BubbleStep() {
+    }
+
+    //getter
+    public String getType() {
+        return type;
+    }
+
+    public ArrayList<Bar> getContent() {
+        return content;
+    }
+
+    //setter
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public void setContent(ArrayList<Bar> content) {
+        this.content = content;
+    }
+
+    //toString
+
+    @Override
+    public String toString() {
+        return "BubbleStep{" +
+                "type='" + type + '\'' +
+                ", content=" + content +
+                '}';
+    }
+}

+ 10 - 0
src/main/java/com/example/data_structure/vo/GraphVO.java

@@ -1,4 +1,14 @@
 package com.example.data_structure.vo;
 
+
+import org.springframework.stereotype.Component;
+
+@Component
 public class GraphVO {
+
+    private int id;
+
+
+
+
 }

+ 4 - 0
src/main/java/com/example/data_structure/vo/HanoiVO.java

@@ -1,4 +1,8 @@
 package com.example.data_structure.vo;
 
+import org.springframework.stereotype.Component;
+
+@Component
 public class HanoiVO {
+    private int id;
 }

+ 5 - 0
src/main/java/com/example/data_structure/vo/SortingVO.java

@@ -1,4 +1,9 @@
 package com.example.data_structure.vo;
 
+import org.springframework.stereotype.Component;
+
+@Component
 public class SortingVO {
+    private int id;
+
 }

+ 4 - 0
src/main/java/com/example/data_structure/vo/StackVO.java

@@ -1,4 +1,8 @@
 package com.example.data_structure.vo;
 
+import org.springframework.stereotype.Component;
+
+@Component
 public class StackVO {
+    private int id;
 }

+ 30 - 0
src/test/java/com/example/data_structure/service/SortingServiceTest.java

@@ -0,0 +1,30 @@
+package com.example.data_structure.service;
+
+
+import com.example.data_structure.vo.BubbleSortVO;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import java.util.ArrayList;
+
+@SpringBootTest
+public class SortingServiceTest {
+
+    @Autowired
+    private SortingService sortingService;
+
+    @Test
+    void bubbleIncSortResultTest(){
+        ArrayList<Integer> numberList=new ArrayList<Integer>();
+        for(int i=9;i>=0;i--){
+            numberList.add(i);
+        }
+
+        BubbleSortVO bubbleResult=sortingService.getIncBubbleSortResult(numberList);
+        System.out.println("==============bubbleSortingResult==============");
+        System.out.println(bubbleResult.toString());
+
+    }
+
+}