Java排序算法

class sort
{
    private int arr[]= {5,7,6,9,8,1,2,3,4,0};
    
    public boolean InsertSort()
    {
        for(int i=1;i<arr.length;++i)
        {
            for(int j=i;j>0;j--)
            {
                if(arr[j-1]>=arr[j])   //前面的比后面的大就交换
                {
                    int temp=arr[j-1];
                    arr[j-1]=arr[j];
                    arr[j]=temp;
                }
                
            }
        }
        return true;
    }
    
   public boolean shellsort()
   {
       int increment=arr.length/3+1;
       while(increment>1)
       {
           for(int i=increment;i<arr.length;++i)
           {
               int j=i-increment;
               while(j>=0)
               {
                   if(arr[j]>arr[j+increment])
                   {
                       int temp=arr[j];
                       arr[j]=arr[j+increment];
                       arr[j+increment]=temp;
                   }
                   j-=increment;
               }
           }
           increment=increment/3+1;
       }
       return true;
   }
   
   public boolean Quicksort(int array[],int First,int back)
   {
       if(First>back)
       {
           return false;
       }
       int start=First;
       int end=back;
       int flag=array[back];
       
       while(start<end)
       {
           while(start<end&&array[end]>=flag)
               end--;
           while(start<end&&array[start]<=flag)
               start++;
           swap(array,start,end);
       }
       swap(array,start,First);
       Quicksort(array,First,start-1);
       Quicksort(array,start+1,end);
       return true;
   }
    
    public final boolean OutputArr() 
    {
        for(int i=0;i<arr.length;++i)
        {
            System.out.print(arr[i]+" ");
        }
        return true;
    }
    
    public boolean swap(int array[],int i,int j)
    {
        int temp=array[i];
        array[i]=array[j];
        array[j]=temp;
        return true;
    }
}

 

posted @ 2019-10-10 15:45  pycodego  阅读(105)  评论(0编辑  收藏  举报