Optimized Bubble Sort (Java)

前端 未结 10 1215
旧时难觅i
旧时难觅i 2020-12-05 11:29

I would like to know how else I can optimize bubble sort so that it overlooks elements that have already been sorted, even after the first pass.

Eg. [4, 2, 3         


        
10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 12:17

    Optimized bubble sort with just 1 for Loop

    /*Advanced BUBBLE SORT with ONE PASS*/
    /*Authored by :: Brooks Tare  AAU*/
    
    public class Bubble {
    
        public int[] bubble(int b[]){ 
        int temp,temp1; 
    
        for(int i=0;ib[i+1] ){
                    ///swap(b[i],b[i+1]);
    
                    temp=b[i];
                    b[i]=b[i+1];
                    b[i+1]=temp;
    
        /*Checking if there is any number(s) greater than 
          the current number. If there is swap them.*/
                    while(i>0){
    
    
                        if(b[i]b[i-1]){i--;}
                    }
                }
                else{continue;}
    
            }
    
    
    
            return b;
        }
    ///the following is a function to display the Array 
            public void see(int []a){
                for(int j=0;j

提交回复
热议问题