java 排序方法

import java.util.Arrays;  
public class Sort {  
     
//冒泡排序(从头到尾排)  
public static void bubbleSort( int [] arrays)  
{  
  //第一次循环从第一个元素开始,到倒数第二个元素  
  for ( int i= 0 ;i<arrays.length- 1 ;i++)  
  {  
   for ( int j= 1 ;j<arrays.length-i;j++)  
   {  
    if (arrays[j- 1 ]>arrays[j])  
    {  
     int temp=arrays[j- 1 ];  
     arrays[j- 1 ]=arrays[j];  
     arrays[j]=temp;  
    }  
   }  
  }  
}  
     
//选择排序  
     
public static void selectSort( int [] arrays)  
{  
  //第一次循环从第一个元素开始,到倒数第二个元素  
  for ( int i= 0 ;i<arrays.length- 1 ;i++)  
  {  
   //标识当前的最小元素的位置  
   int ind=i;  
   for ( int j=i+ 1 ;j<arrays.length;j++)  
   {  
    if (arrays[j]<arrays[ind])  
    {  
     ind=j;  
    }  
   }  
   if (ind!=i)  
   {  
    int temp=arrays[ind];  
    arrays[ind]=arrays[i];  
    arrays[i]=temp;  
   }  
  }  
}  
     
//插入排序  
public static void insertSort( int [] arrays)  
{  
  //第一次循环从第二个元素开始,到最后一个元素  
  //arrays[i]是当前需要插入的元素  
  for ( int i= 1 ;i<arrays.length;i++)  
  {  
   //当前已经排好序的数组长度是i  
   int temp=arrays[i];  
   int ind=i;  
   for ( int j=i- 1 ;j>= 0 ;j--)  
   {  
    //如果找到了比arrays[j]小的,那么就将已经排好序的数组的当前位置后的所有元素后移一位,并将需要插入的元素放入该位置j  
    if (temp<arrays[j])  
    {  
     arrays[j+ 1 ]=arrays[j];  
     ind=j;  
    }  
    else
    {  
     break ;  
    }  
   }  
   arrays[ind]=temp;  
  }  
}  
     
//快速排序  
public static void quickSot( int [] arrays, int left, int right)  
{  
  if (left>=right)  
  {  
   return ;  
  }  
  int compInd=left; //选取第一个数作为比较的基石  
  int i=left;  
  int j=right;  
  while (i<j)  
  {  
  for (;j>=i;j--)  
  {  
   if (arrays[j]<arrays[compInd])  
   {  
    int temp=arrays[j];  
    arrays[j]=arrays[compInd];  
    arrays[compInd]=temp;  
    compInd=j;  
    break ;  
   }  
  }  
  for (;i<=j;i++)  
  {  
   if (arrays[i]>arrays[compInd])  
   {  
    int temp=arrays[i];  
    arrays[i]=arrays[compInd];  
    arrays[compInd]=temp;  
    compInd=i;  
    break ;  
   }  
  }  
  }  
  quickSot(arrays,left,compInd- 1 );  
  quickSot(arrays,compInd+ 1 ,right);  
}  
     
//归并排序(两个有序的数组)  
public static int [] merger( int [] a, int [] b)  
{  
  int [] result= new int [a.length+b.length];  
  int ind= 0 ;  
  int ind1= 0 ;  
  int ind2= 0 ;  
  while (ind1<a.length && ind2<b.length)  
  {  
   if (a[ind1]<b[ind2])  
   {  
    result[ind++]=a[ind1++];  
   }  
   else
   {  
    result[ind++]=b[ind2++];  
   }  
  }  
  while (ind1<a.length)  
  {  
   result[ind++]=a[ind1];  
   ind1++;  
       
  }  
  while (ind2<b.length)  
  {  
   result[ind++]=b[ind2];  
   ind2++;  
  }  
  return result;  
}  
     
//希尔排序  steps为增量数组,前面的元素大于后面的元素,并且最后一个元素值为1 ,例如{ 5, 3, 1 }  
public static void shellSort( int [] arrays, int [] steps)  
{  
  for ( int step:steps)  
  {  
   for ( int i= 0 ;i<step;i++)  
   {  
    for ( int j=i;j<arrays.length;j+=step)  
    {  
     //选择排序  
     int ind=j;  
     for ( int k=j+step;k<arrays.length;k+=step)  
     {  
      if (arrays[k]<arrays[ind])  
      {  
       ind=k;  
      }  
     }  
     if (ind!=j)  
     {  
      int temp=arrays[ind];  
      arrays[ind]=arrays[j];  
      arrays[j]=temp;  
     }   
        
    }  
   }  
  }  
}  
     
public static void main(String[] args) {  
  int [] a= new int []{ 6 , 3 , 13 , 8 , 5 , 10 , 12 , 16 , 4 , 6 };  
//  bubbleSort(a);  
//  selectSort(a);  
//  insertSort(a);  
//  quickSot(a,0,a.length-1);  
  shellSort(a, new int []{ 5 , 3 , 1 });  
  System.out.println(Arrays.toString(a));  
}  
     
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值