Explorar o código

finish quick sort function.

171870002 %!s(int64=5) %!d(string=hai) anos
pai
achega
44eab5a945

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

@@ -5,6 +5,7 @@ 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.QuickSortVO;
 import com.example.data_structure.vo.SelectionSortVO;
 import com.sun.corba.se.pept.transport.ResponseWaitingRoom;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -41,4 +42,11 @@ public class SortingController {
         return re;
     }
 
+    @GetMapping(value = "/quick-sort/get-analysis-result")
+    public Response getQuickSortResult(@RequestParam String rule,@RequestParam ArrayList<Integer> content){
+        QuickSortVO vo=sortingService.getQuickSortResult(rule,content);
+        Response re=Response.buildSuccess(vo);
+        return re;
+    }
+
 }

+ 188 - 5
src/main/java/com/example/data_structure/service/SortingService.java

@@ -1,6 +1,7 @@
 package com.example.data_structure.service;
 
 import com.example.data_structure.vo.*;
+import com.sun.xml.internal.bind.v2.model.annotation.Quick;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Service;
 
@@ -473,6 +474,14 @@ public class SortingService {
     //获取增序快速排序结果
     public QuickSortVO getIncQuickSortResult(ArrayList<Integer> content){
         int len=content.size();
+        QuickSortVO quickSortVO=new QuickSortVO();
+        ArrayList<QuickStep> onSortingStates=new ArrayList<QuickStep>();
+        ArrayList<Bar> initial=new ArrayList<Bar>();
+        for(int m=0;m<len;m++){
+            initial.add(new Bar(content.get(m),"blue"));
+            quickSortVO.setInitial(new QuickStep("beforeSelectPivot",initial));
+        }
+        ArrayList<Bar> currentContent=QuickStep.copyContent(initial);
         Stack<ArrayList<Integer>> unsortedSegments = new Stack<ArrayList<Integer>>();
         ArrayList<Integer> initialSeg= new ArrayList<Integer>();
         initialSeg.add(0);
@@ -481,19 +490,193 @@ public class SortingService {
         ArrayList<Integer> currentSeg=initialSeg;
         while(!unsortedSegments.isEmpty()){
             currentSeg=unsortedSegments.pop();
-
+            int leftIndex=currentSeg.get(0);
+            int rightIndex=currentSeg.get(1);
+            if(leftIndex==rightIndex){
+                currentContent.get(leftIndex).setColor("orange");
+                onSortingStates.add(new QuickStep("beforeSelectPivot",currentContent));
+                currentContent=QuickStep.copyContent(currentContent);
+                continue;
+            }
+            int pivot=leftIndex;
+            System.out.println("==========currentContent size: "+currentContent.size()+"=================");
+            currentContent.get(pivot).setColor("yellow");
+            onSortingStates.add(new QuickStep("onSelectPivot",currentContent));
+            currentContent=QuickStep.copyContent(currentContent);
+            int storeIndex=pivot+1;
+            for(int i=pivot+1;i<=rightIndex;i++){
+                currentContent.get(i).setColor("red");
+                onSortingStates.add(new QuickStep("onCompare",currentContent));
+                currentContent=QuickStep.copyContent(currentContent);
+                if(content.get(i)<content.get(pivot)){
+                    if(i!=storeIndex){
+                        int temp=content.get(storeIndex);
+                        content.set(storeIndex,content.get(i));
+                        content.set(i,temp);
+                        Bar tempBar=currentContent.get(i);
+                        currentContent.set(i,currentContent.get(storeIndex));
+                        currentContent.set(storeIndex,tempBar);
+                        onSortingStates.add(new QuickStep("onExchange",currentContent));
+                        currentContent=QuickStep.copyContent(currentContent);
+                    }
+                    currentContent.get(storeIndex).setColor("green");
+                    storeIndex++;
+                }else{
+                    currentContent.get(i).setColor("purple");
+                }
+                if(i==rightIndex){
+                    onSortingStates.add(new QuickStep("onCompare",currentContent));
+                    currentContent=QuickStep.copyContent(currentContent);
+                }
+            }
+            if(pivot<storeIndex-1) {
+                int temp = content.get(pivot);
+                content.set(pivot, content.get(storeIndex - 1));
+                content.set(storeIndex - 1, temp);
+                Bar tempBar = currentContent.get(pivot);
+                currentContent.set(pivot, currentContent.get(storeIndex - 1));
+                currentContent.set(storeIndex - 1, tempBar);
+                onSortingStates.add(new QuickStep("onConfirm", currentContent));
+                currentContent = QuickStep.copyContent(currentContent);
+            }
+            currentContent.get(storeIndex-1).setColor("orange");
+            if(rightIndex>storeIndex-1){
+                ArrayList<Integer> rightSeg=new ArrayList<Integer>();
+                rightSeg.add(storeIndex);
+                rightSeg.add(rightIndex);
+                unsortedSegments.push(rightSeg);
+                for(int m=storeIndex;m<=rightIndex;m++){
+                    currentContent.get(m).setColor("blue");
+                }
+            }
+            if(leftIndex<storeIndex-1){
+                ArrayList<Integer> leftSeg=new ArrayList<Integer>();
+                leftSeg.add(leftIndex);
+                leftSeg.add(storeIndex-2);
+                unsortedSegments.push(leftSeg);
+                for(int m=leftIndex;m<storeIndex-1;m++){
+                    currentContent.get(m).setColor("blue");
+                }
+            }
+            onSortingStates.add(new QuickStep("beforeSelectPivot",currentContent));
+            currentContent=QuickStep.copyContent(currentContent);
         }
-        return null;
+        for(int i=0;i<len;i++){
+            currentContent.get(i).setColor("blue");
+        }
+        onSortingStates.add(new QuickStep("allSorted",currentContent));
+        quickSortVO.setOnSortingStates(onSortingStates);
+        return quickSortVO;
     }
 
     //获取降序快速排序结果
     public QuickSortVO getDesQuickSortResult(ArrayList<Integer> content){
-        return null;
+        int len=content.size();
+        QuickSortVO quickSortVO=new QuickSortVO();
+        ArrayList<QuickStep> onSortingStates=new ArrayList<QuickStep>();
+        ArrayList<Bar> initial=new ArrayList<Bar>();
+        for(int m=0;m<len;m++){
+            initial.add(new Bar(content.get(m),"blue"));
+            quickSortVO.setInitial(new QuickStep("beforeSelectPivot",initial));
+        }
+        ArrayList<Bar> currentContent=QuickStep.copyContent(initial);
+        Stack<ArrayList<Integer>> unsortedSegments = new Stack<ArrayList<Integer>>();
+        ArrayList<Integer> initialSeg= new ArrayList<Integer>();
+        initialSeg.add(0);
+        initialSeg.add(len-1);
+        unsortedSegments.push(initialSeg);
+        ArrayList<Integer> currentSeg=initialSeg;
+        while(!unsortedSegments.isEmpty()){
+            currentSeg=unsortedSegments.pop();
+            int leftIndex=currentSeg.get(0);
+            int rightIndex=currentSeg.get(1);
+            if(leftIndex==rightIndex){
+                currentContent.get(leftIndex).setColor("orange");
+                onSortingStates.add(new QuickStep("beforeSelectPivot",currentContent));
+                currentContent=QuickStep.copyContent(currentContent);
+                continue;
+            }
+            int pivot=leftIndex;
+            currentContent.get(pivot).setColor("yellow");
+            onSortingStates.add(new QuickStep("onSelectPivot",currentContent));
+            currentContent=QuickStep.copyContent(currentContent);
+            int storeIndex=pivot+1;
+            for(int i=pivot+1;i<=rightIndex;i++){
+                currentContent.get(i).setColor("red");
+                onSortingStates.add(new QuickStep("onCompare",currentContent));
+                currentContent=QuickStep.copyContent(currentContent);
+                if(content.get(i)>content.get(pivot)){
+                    if(i!=storeIndex){
+                        int temp=content.get(storeIndex);
+                        content.set(storeIndex,content.get(i));
+                        content.set(i,temp);
+                        Bar tempBar=currentContent.get(i);
+                        currentContent.set(i,currentContent.get(storeIndex));
+                        currentContent.set(storeIndex,tempBar);
+                        onSortingStates.add(new QuickStep("onExchange",currentContent));
+                        currentContent=QuickStep.copyContent(currentContent);
+                    }
+                    currentContent.get(storeIndex).setColor("green");
+                    storeIndex++;
+                }else{
+                    currentContent.get(i).setColor("purple");
+                }
+                if(i==rightIndex){
+                    onSortingStates.add(new QuickStep("onCompare",currentContent));
+                    currentContent=QuickStep.copyContent(currentContent);
+                }
+            }
+            if(pivot<storeIndex-1) {
+                int temp = content.get(pivot);
+                content.set(pivot, content.get(storeIndex - 1));
+                content.set(storeIndex - 1, temp);
+                Bar tempBar = currentContent.get(pivot);
+                currentContent.set(pivot, currentContent.get(storeIndex - 1));
+                currentContent.set(storeIndex - 1, tempBar);
+                onSortingStates.add(new QuickStep("onConfirm", currentContent));
+                currentContent = QuickStep.copyContent(currentContent);
+            }
+            currentContent.get(storeIndex-1).setColor("orange");
+            if(rightIndex>storeIndex-1){
+                ArrayList<Integer> rightSeg=new ArrayList<Integer>();
+                rightSeg.add(storeIndex);
+                rightSeg.add(rightIndex);
+                unsortedSegments.push(rightSeg);
+                for(int m=storeIndex;m<=rightIndex;m++){
+                    currentContent.get(m).setColor("blue");
+                }
+            }
+            if(leftIndex<storeIndex-1){
+                ArrayList<Integer> leftSeg=new ArrayList<Integer>();
+                leftSeg.add(leftIndex);
+                leftSeg.add(storeIndex-2);
+                unsortedSegments.push(leftSeg);
+                for(int m=leftIndex;m<storeIndex-1;m++){
+                    currentContent.get(m).setColor("blue");
+                }
+            }
+            onSortingStates.add(new QuickStep("beforeSelectPivot",currentContent));
+            currentContent=QuickStep.copyContent(currentContent);
+        }
+        for(int i=0;i<len;i++){
+            currentContent.get(i).setColor("blue");
+        }
+        onSortingStates.add(new QuickStep("allSorted",currentContent));
+        quickSortVO.setOnSortingStates(onSortingStates);
+        return quickSortVO;
     }
 
     //获取快速排序结果
-    public QuickSortVO getQuickSortResult(ArrayList<Integer> content){
-        return null;
+    public QuickSortVO getQuickSortResult(String rule,ArrayList<Integer> content){
+        QuickSortVO vo =new QuickSortVO();
+        if(rule.equals("ascending")){
+            vo=getIncQuickSortResult(content);
+        }else if(rule.equals("descending")){
+            vo=getDesQuickSortResult(content);
+        }else{
+
+        }
+        return vo;
     }
 
 }

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

@@ -50,4 +50,7 @@ public class Bar {
         return copyBar;
     }
 
+    public String toColorString(){
+        return color+" ";
+    }
 }

+ 13 - 1
src/main/java/com/example/data_structure/vo/QuickSortVO.java

@@ -38,11 +38,23 @@ public class QuickSortVO {
 
     //toString
 
+
     @Override
     public String toString() {
-        return "BubbleSortVO{" +
+        return "QuickSortVO{" +
                 "initial=" + initial +
                 ", onSortingStates=" + onSortingStates +
                 '}';
     }
+
+    public String toColorString() {
+        String re="BubbleSortVO{" +
+                "initial=" + initial.toColorString() +
+                ", onSortingStates=";
+        for(int i=0;i<onSortingStates.size();i++){
+        re=re+" i="+i+" "+onSortingStates.get(i).toColorString();
+        }
+        re=re+'}';
+        return re;
+    }
 }

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

@@ -55,4 +55,13 @@ public class QuickStep {
 
         return copyContent;
     }
+
+    public String toColorString(){
+        String re=type+" ";
+        for(int i=0;i<content.size();i++){
+            re=re+content.get(i).toColorString();
+        }
+        re=re+"\n";
+        return re;
+    }
 }

+ 36 - 4
src/test/java/com/example/data_structure/service/SortingServiceTest.java

@@ -1,10 +1,7 @@
 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 com.example.data_structure.vo.*;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -90,4 +87,39 @@ public class SortingServiceTest {
         System.out.println(insertionResult.toString());
     }
 
+    @Test
+    void QuickSortTest(){
+        ArrayList<Integer> numberList=new ArrayList<Integer>();
+        numberList.add(3); //3,44,38,5,47,15,36,26,27,2,46,4,19,50,48
+        numberList.add(44);
+        numberList.add(38);
+        numberList.add(5);
+        numberList.add(47);
+        numberList.add(15);
+        numberList.add(36);
+        numberList.add(26);
+        numberList.add(27);
+        numberList.add(2);
+        numberList.add(46);
+        numberList.add(4);
+        numberList.add(19);
+        numberList.add(50);
+        numberList.add(48);
+        QuickSortVO quickSortResult=sortingService.getIncQuickSortResult(numberList);
+        System.out.println("=======================quickIncSortResult=====================");
+        System.out.println(quickSortResult.toColorString());
+    }
+
+    @Test
+    void QuickSortTest1(){
+        ArrayList<Integer> numberList=new ArrayList<Integer>();
+        for(int i=0;i<=9;i++){
+            numberList.add(i);
+        }
+
+        QuickSortVO quickSortResult=sortingService.getDesQuickSortResult(numberList);
+        System.out.println("=============quickDesSortResult====================");
+        System.out.println(quickSortResult.toString());
+    }
+
 }