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