Array and sorting algorithms

1) bubble sort

public static int[] bubbleSort(int[]a)
    {
        int temp;
        for(int i=0;i<a.length-1;i++)
        {
            for(int j=0;j<a.length-1-i;j++)
            {
                if(a[j]>a[j+1])
                {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
                
            }
            System.out.println(Arrays.toString(a));
        }
        return a;
    }
public  static  int [] bubbleSort2 ( int [] A) 
    { 
        Boolean BOOL = to true ;
         the while (BOOL) 
        { 
            BOOL = to false ; // The following code is not executed when it is described, are lined up to sort, do not recycle the
             for ( int I = 0; I <-a.length. 1; I ++ ) 
            { 
                IF (A [I]> A [I +. 1 ]) 
                { 
                    int TEMP = A [I]; 
                    A [I] = A [I +. 1 ] ; 
                    A [I + 1'd] = TEMP; 
                    BOOL = to true;
                }
            }
            System.out.println(Arrays.toString(a));
        }
        return a;
    }

2) Selection Sort

public static void selectSort(int[] a)
    {
        int length = a.length;
        for(int i=0;i<length-1;i++)
        {
            int min=i;
            for(int j=i+1;j<length;j++)
            {
                if(a[min]>a[j])
                {
                    min=j;
                }
            }
            if(min!=i)
            {
                int temp = a[min];
                a[min]=a[i];
                a[i]=temp;
            }
            System.out.println(Arrays.toString(a));
        }
    }

 3) insertion sort

public static int[] insertSort(int[] a )
    {
        if(a==null||a.length<2) return a;
        int length=a.length;
        for(int i=1;i<length;i++)
        {
            int temp=a[i];
            int index;
            for(int j=i;j>0;j--)
            {
                if(a[j]<a[j-1])
                {
                    a[j]=a[j-1];
                    index=j-1;
                }
                else break;
                a[index]=temp;
            }
            System.out.println(Arrays.toString(a));
        }
        return a;
    }

 

Guess you like

Origin www.cnblogs.com/lhh666/p/11579133.html