How to sort an array in a single loop?

后端 未结 22 2900
面向向阳花
面向向阳花 2020-12-19 09:14

So I was going through different sorting algorithms. But almost all the sorting algorithms require 2 loops to sort the array. The time complexity of Bubble sort & Insert

22条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 09:37

    public void sortArrayUsingSingleLoop(int[] intArray) {
            int temp;
            boolean swap = false;
            for(int i=0;iintArray[i+1]){
                 temp=intArray[i];
                 intArray[i]=intArray[i+1];
                 intArray[i+1]=temp;
                 swap=true;
             }
    
             if(swap &&i==intArray.length-2){
                i=-1;swap=false;
             }
            }
    
        }
    

提交回复
热议问题