Просмотр исходного кода

add insertion sort function and selection sort function.

171870002 5 лет назад
Родитель
Сommit
d517825a0e

+ 15 - 1
src/main/java/com/example/data_structure/controller/SortingController.java

@@ -4,6 +4,8 @@ import com.example.data_structure.common.Response;
 import com.example.data_structure.domain.Sorting;
 import com.example.data_structure.service.SortingService;
 import com.example.data_structure.vo.BubbleSortVO;
+import com.example.data_structure.vo.InsertionSortVO;
+import com.example.data_structure.vo.SelectionSortVO;
 import com.sun.corba.se.pept.transport.ResponseWaitingRoom;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
@@ -25,6 +27,18 @@ public class SortingController {
         return re;
     }
 
-    
+    @GetMapping(value = "/selection-sort/get-analysis-result")
+    public Response getSelectionSortResult(@RequestParam String rule,@RequestParam ArrayList<Integer> content){
+        SelectionSortVO vo=sortingService.getSelectionSortResult(rule,content);
+        Response re=Response.buildSuccess(vo);
+        return re;
+    }
+
+    @GetMapping(value = "/insertion-sort/get-analysis-result")
+    public Response getInsertionSortResult(@RequestParam String rule,@RequestParam ArrayList<Integer> content){
+        InsertionSortVO vo=sortingService.getInsertionSortResult(rule,content);
+        Response re=Response.buildSuccess(vo);
+        return re;
+    }
 
 }

+ 280 - 3
src/main/java/com/example/data_structure/service/SortingService.java

@@ -1,8 +1,6 @@
 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 com.example.data_structure.vo.*;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Service;
 
@@ -192,4 +190,283 @@ public class SortingService {
     }
 
 
+    //获取增序选择排序的结果
+    public SelectionSortVO getIncSelectionSortResult(ArrayList<Integer> content){
+        int len=content.size();
+        SelectionSortVO selectionSortVO=new SelectionSortVO();
+        ArrayList<SelectionStep> onSortingStates=new ArrayList<SelectionStep>();
+        ArrayList<Bar> initial =new ArrayList<Bar>();
+        for(int m=0;m<len;m++){
+            initial.add(new Bar(content.get(m),"blue"));
+        }
+        selectionSortVO.setInitial(new SelectionStep("beforeCompare",initial));
+
+        ArrayList<Bar> currentContent=SelectionStep.copyContent(initial);
+        System.out.println("======currentContent==============");
+        System.out.println(currentContent.toString());
+        for(int i=0;i<len-1;i++){
+            int minIndex=i;
+            Bar redBar=currentContent.get(i);
+            redBar.setColor("red");
+            onSortingStates.add(new SelectionStep("onExtreme",currentContent));
+            for(int j=i+1;j<len;j++){
+                currentContent=SelectionStep.copyContent(currentContent);
+                currentContent.get(j).setColor("green");
+                onSortingStates.add(new SelectionStep("onCompare",currentContent));
+                currentContent=SelectionStep.copyContent(currentContent);
+                currentContent.get(j).setColor("blue");
+                //System.out.println("=====compare===="+"minIndexValue: "+content.get(min));
+                if(content.get(minIndex)>content.get(j)){
+                    System.out.println("=============== i:"+i+" j: "+j);
+                    currentContent.get(minIndex).setColor("blue");
+                    currentContent.get(j).setColor("red");
+                    onSortingStates.add(new SelectionStep("onExtreme",currentContent));
+                    currentContent=SelectionStep.copyContent(currentContent);
+                    minIndex=j;
+                }
+            }
+            if(minIndex!=i){
+                currentContent.get(i).setColor("red");
+                onSortingStates.add(new SelectionStep("beforeExchange",currentContent));
+                currentContent=SelectionStep.copyContent(currentContent);
+                int temp=content.get(i);
+                content.set(i,content.get(minIndex));
+                content.set(minIndex,temp);
+                Bar tempBar=currentContent.get(i);
+                currentContent.set(i,currentContent.get(minIndex));
+                currentContent.set(minIndex,tempBar);
+                onSortingStates.add(new SelectionStep("afterExchange",currentContent));
+                currentContent=SelectionStep.copyContent(currentContent);
+            }
+            currentContent.get(i).setColor("orange");
+            if(minIndex>i) {
+                currentContent.get(minIndex).setColor("blue");
+            }
+            onSortingStates.add(new SelectionStep("beforeCompare",currentContent));
+            currentContent=SelectionStep.copyContent(currentContent);
+        }
+        for(int n=0;n<len;n++){
+            currentContent.get(n).setColor("blue");
+        }
+        onSortingStates.add(new SelectionStep("allSorted",currentContent));
+        selectionSortVO.setOnSortingStates(onSortingStates);
+        return selectionSortVO;
+    }
+
+    //获取降序选择排序的结果
+    public SelectionSortVO getDesSelectionSortResult(ArrayList<Integer> content){
+        int len=content.size();
+        SelectionSortVO selectionSortVO=new SelectionSortVO();
+        ArrayList<SelectionStep> onSortingStates=new ArrayList<SelectionStep>();
+        ArrayList<Bar> initial =new ArrayList<Bar>();
+        for(int m=0;m<len;m++){
+            initial.add(new Bar(content.get(m),"blue"));
+        }
+        selectionSortVO.setInitial(new SelectionStep("beforeCompare",initial));
+
+        ArrayList<Bar> currentContent=SelectionStep.copyContent(initial);
+        System.out.println("======currentContent==============");
+        System.out.println(currentContent.toString());
+        for(int i=0;i<len-1;i++){
+            int minIndex=i;
+            Bar redBar=currentContent.get(i);
+            redBar.setColor("red");
+            onSortingStates.add(new SelectionStep("onExtreme",currentContent));
+            for(int j=i+1;j<len;j++){
+                currentContent=SelectionStep.copyContent(currentContent);
+                currentContent.get(j).setColor("green");
+                onSortingStates.add(new SelectionStep("onCompare",currentContent));
+                currentContent=SelectionStep.copyContent(currentContent);
+                currentContent.get(j).setColor("blue");
+                //System.out.println("=====compare===="+"minIndexValue: "+content.get(min));
+                if(content.get(minIndex)<content.get(j)){
+                    System.out.println("=============== i:"+i+" j: "+j);
+                    currentContent.get(minIndex).setColor("blue");
+                    currentContent.get(j).setColor("red");
+                    onSortingStates.add(new SelectionStep("onExtreme",currentContent));
+                    currentContent=SelectionStep.copyContent(currentContent);
+                    minIndex=j;
+                }
+            }
+            if(minIndex!=i){
+                currentContent.get(i).setColor("red");
+                onSortingStates.add(new SelectionStep("beforeExchange",currentContent));
+                currentContent=SelectionStep.copyContent(currentContent);
+                int temp=content.get(i);
+                content.set(i,content.get(minIndex));
+                content.set(minIndex,temp);
+                Bar tempBar=currentContent.get(i);
+                currentContent.set(i,currentContent.get(minIndex));
+                currentContent.set(minIndex,tempBar);
+                onSortingStates.add(new SelectionStep("afterExchange",currentContent));
+                currentContent=SelectionStep.copyContent(currentContent);
+            }
+            currentContent.get(i).setColor("orange");
+            if(minIndex>i) {
+                currentContent.get(minIndex).setColor("blue");
+            }
+            onSortingStates.add(new SelectionStep("beforeCompare",currentContent));
+            currentContent=SelectionStep.copyContent(currentContent);
+        }
+        for(int n=0;n<len;n++){
+            currentContent.get(n).setColor("blue");
+        }
+        onSortingStates.add(new SelectionStep("allSorted",currentContent));
+        selectionSortVO.setOnSortingStates(onSortingStates);
+        return selectionSortVO;
+    }
+
+
+    //获取选择排序的结果
+    public SelectionSortVO getSelectionSortResult(String rule,ArrayList<Integer> content){
+        SelectionSortVO vo =new SelectionSortVO();
+        if(rule.equals("ascending")){
+            vo=getIncSelectionSortResult(content);
+        }else if(rule.equals("descending")){
+            vo=getDesSelectionSortResult(content);
+        }else{
+
+        }
+        return vo;
+
+    }
+
+
+    //获取增序插入排序结果
+    public InsertionSortVO getIncInsertionSortResult(ArrayList<Integer> content){
+        int len=content.size();
+        InsertionSortVO insertionSortVO=new InsertionSortVO();
+        ArrayList<Bar> initial=new ArrayList<Bar>();
+        for(int p=0;p<len;p++){
+            initial.add(new Bar(content.get(p),"blue"));
+        }
+        ArrayList<InsertionStep> onSortingStates=new ArrayList<InsertionStep>();
+        insertionSortVO.setInitial(new InsertionStep("onInsertion",0,initial));
+        ArrayList<Bar> currentContent=InsertionStep.copyContent(initial);
+        currentContent.get(0).setColor("orange");
+        onSortingStates.add(new InsertionStep("onInsertion",0,currentContent));
+        currentContent=InsertionStep.copyContent(currentContent);
+        for(int i=1;i<len;i++){
+            currentContent.get(i).setColor("white");
+            onSortingStates.add(new InsertionStep("onSelect",i,currentContent));
+            currentContent=InsertionStep.copyContent(currentContent);
+
+            int j=i-1;
+            int value=content.get(i);
+            int exchangeIndex=0;
+            while(j>=0){
+                currentContent.get(j).setColor("green");
+                onSortingStates.add(new InsertionStep("onCompare",j+1,currentContent));
+                currentContent=InsertionStep.copyContent(currentContent);
+                if(content.get(j)<=value){
+                    break;
+                }
+                Bar temp=currentContent.get(j);
+                currentContent.set(j,currentContent.get(j+1));
+                currentContent.set(j+1,temp);
+                onSortingStates.add(new InsertionStep("onExchange",j,currentContent));
+                currentContent=InsertionStep.copyContent(currentContent);
+                currentContent.get(j+1).setColor("orange");
+                j--;
+            }
+            if(j<0){
+                exchangeIndex=0;
+                currentContent.get(exchangeIndex).setColor("orange");
+            }else{
+                exchangeIndex=j+1;
+                currentContent.get(exchangeIndex).setColor("orange");
+                currentContent.get(j).setColor("orange");
+            }
+            onSortingStates.add(new InsertionStep("onInsertion",exchangeIndex,currentContent));
+            currentContent=InsertionStep.copyContent(currentContent);
+
+            //移动
+           for(int m=i-1;i<=exchangeIndex;i--){
+               content.set(m+1,content.get(m));
+           }
+           content.set(exchangeIndex,value);
+        }
+        for(int q=0;q<len;q++){
+            currentContent.get(q).setColor("blue");
+        }
+        onSortingStates.add(new InsertionStep("allSorted",0,currentContent));
+        insertionSortVO.setOnSortingStates(onSortingStates);
+        return insertionSortVO;
+    }
+
+    //获取降序插入排序结果
+    public InsertionSortVO getDesInsertionSortResult(ArrayList<Integer> content){
+        int len=content.size();
+        InsertionSortVO insertionSortVO=new InsertionSortVO();
+        ArrayList<Bar> initial=new ArrayList<Bar>();
+        for(int p=0;p<len;p++){
+            initial.add(new Bar(content.get(p),"blue"));
+        }
+        ArrayList<InsertionStep> onSortingStates=new ArrayList<InsertionStep>();
+        insertionSortVO.setInitial(new InsertionStep("onInsertion",0,initial));
+        ArrayList<Bar> currentContent=InsertionStep.copyContent(initial);
+        currentContent.get(0).setColor("orange");
+        onSortingStates.add(new InsertionStep("onInsertion",0,currentContent));
+        currentContent=InsertionStep.copyContent(currentContent);
+        for(int i=1;i<len;i++){
+            currentContent.get(i).setColor("white");
+            onSortingStates.add(new InsertionStep("onSelect",i,currentContent));
+            currentContent=InsertionStep.copyContent(currentContent);
+
+            int j=i-1;
+            int value=content.get(i);
+            int exchangeIndex=0;
+            while(j>=0){
+                currentContent.get(j).setColor("green");
+                onSortingStates.add(new InsertionStep("onCompare",j+1,currentContent));
+                currentContent=InsertionStep.copyContent(currentContent);
+                if(content.get(j)>=value){
+                    break;
+                }
+                Bar temp=currentContent.get(j);
+                currentContent.set(j,currentContent.get(j+1));
+                currentContent.set(j+1,temp);
+                onSortingStates.add(new InsertionStep("onExchange",j,currentContent));
+                currentContent=InsertionStep.copyContent(currentContent);
+                currentContent.get(j+1).setColor("orange");
+                j--;
+            }
+            if(j<0){
+                exchangeIndex=0;
+                currentContent.get(exchangeIndex).setColor("orange");
+            }else{
+                exchangeIndex=j+1;
+                currentContent.get(exchangeIndex).setColor("orange");
+                currentContent.get(j).setColor("orange");
+            }
+            onSortingStates.add(new InsertionStep("onInsertion",exchangeIndex,currentContent));
+            currentContent=InsertionStep.copyContent(currentContent);
+
+            //移动
+            for(int m=i-1;i<=exchangeIndex;i--){
+                content.set(m+1,content.get(m));
+            }
+            content.set(exchangeIndex,value);
+        }
+        for(int q=0;q<len;q++){
+            currentContent.get(q).setColor("blue");
+        }
+        onSortingStates.add(new InsertionStep("allSorted",0,currentContent));
+        insertionSortVO.setOnSortingStates(onSortingStates);
+        return insertionSortVO;
+    }
+
+    //获取插入排序结果
+    public InsertionSortVO getInsertionSortResult(String rule,ArrayList<Integer> content){
+        InsertionSortVO vo =new InsertionSortVO();
+        if(rule.equals("ascending")){
+            vo=getIncInsertionSortResult(content);
+        }else if(rule.equals("descending")){
+            vo=getDesInsertionSortResult(content);
+        }else{
+
+        }
+        return vo;
+    }
+
 }

+ 5 - 2
src/main/java/com/example/data_structure/vo/InsertionStep.java

@@ -7,11 +7,12 @@ import java.util.ArrayList;
 @Component
 public class InsertionStep {
     private String type; //有五个值:onSelect,onCompare,onExchange,onInsertion,allSorted
-    private int insertionIndex;
+    private int insertionIndex; //红色的bar在哪个下标下面
     private ArrayList<Bar> content;
 
-    public InsertionStep(String type, ArrayList<Bar> content) {
+    public InsertionStep(String type, int insertionIndex,ArrayList<Bar> content) {
         this.type = type;
+        this.insertionIndex=insertionIndex;
         this.content = content;
     }
 
@@ -46,10 +47,12 @@ public class InsertionStep {
 
     //toString
 
+
     @Override
     public String toString() {
         return "InsertionStep{" +
                 "type='" + type + '\'' +
+                ", insertionIndex=" + insertionIndex +
                 ", content=" + content +
                 '}';
     }

+ 7 - 0
src/main/java/com/example/data_structure/vo/QuickStep.java

@@ -0,0 +1,7 @@
+package com.example.data_structure.vo;
+
+import org.springframework.stereotype.Component;
+
+@Component
+public class QuickStep {
+}

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

@@ -2,6 +2,9 @@ package com.example.data_structure.service;
 
 
 import com.example.data_structure.vo.BubbleSortVO;
+import com.example.data_structure.vo.InsertionSortVO;
+import com.example.data_structure.vo.InsertionStep;
+import com.example.data_structure.vo.SelectionSortVO;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -39,4 +42,52 @@ public class SortingServiceTest {
         System.out.println(bubbleResult.toString());
     }
 
+    @Test
+    void selectionIncSortResultTest(){
+        ArrayList<Integer> numberList=new ArrayList<Integer>();
+        for(int i=9;i>=0;i--){
+            numberList.add(i);
+        }
+
+        SelectionSortVO selectionResult=sortingService.getIncSelectionSortResult(numberList);
+        System.out.println("======================selectionIncSortResult=======================");
+        System.out.println(selectionResult.toString());
+    }
+
+    @Test
+    void selectionDesSortResultTest(){
+        ArrayList<Integer> numberList=new ArrayList<Integer>();
+        for(int i=0;i<=9;i++){
+            numberList.add(i);
+        }
+
+        SelectionSortVO selectionResult=sortingService.getDesSelectionSortResult(numberList);
+        System.out.println("============selectionDesSortResult======================");
+        System.out.println(selectionResult.toString());
+    }
+
+    @Test
+    void insertionIncSortResultTest(){
+        ArrayList<Integer> numberList=new ArrayList<Integer>();
+        for(int i=9;i>=0;i--){
+            numberList.add(i);
+        }
+
+        InsertionSortVO insertionResult=sortingService.getIncInsertionSortResult(numberList);
+        System.out.println("============insertionIncSortResult================");
+        System.out.println(insertionResult.toString());
+    }
+
+    @Test
+    void insertionDesSortResultTest(){
+        ArrayList<Integer> numberList=new ArrayList<Integer>();
+        for(int i=0;i<=9;i++){
+            numberList.add(i);
+        }
+
+        InsertionSortVO insertionResult=sortingService.getDesInsertionSortResult(numberList);
+        System.out.println("=============insertionDesSortResult====================");
+        System.out.println(insertionResult.toString());
+    }
+
 }