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