排序算法(冒泡,选择,快速)Java 实现

冒泡 排序:

public static void Bubblesort(int [] a)

{
for(int x=0;x<=a.length-1;x++)
{
for(int y=0;y<a.length-1-x;y++)
{
swapArrayAsc(a,y,y+1);
}
}

}

选择排序:

public static void SelectorSort(int [] a)
{
for(int x=0;x<=a.length-1;x++)
{
for(int y=x+1;y<=a.length-1;y++)
{
swapArrayAsc(a,x,y);
}
}

}

快速排序(While):

public static void quickSort(int[] a, int l, int r) {
int i = l, j = r, x = a[l];
if (l < r) {
while (i < j) {
while (i < j && a[j] >= x)//前面虽然while里判断过,但while最后还有个j--,j--之后可能会出现i=j的情况,所以if不能去掉。
j--;
if (i < j) {
a[i] = a[j];
//i++;
}
while (i < j && a[i] < x)
i++;
if (i < j) {
a[j] = a[i];
//j--;
}
}
a[i] = x;
if( i-l >1 )
quickSort(a, l, i - 1);
if( r-i >1 )
quickSort(a, i + 1, r);


}
}

快速排序(for)

 private static void QuickSort(int[] array,int start,int end) 
  { 
      if(start<end) 
      { 
          int key=array[start];//初始化保存基元  
          int i=start,j;//初始化i,j  
          for(j=start+1;j<=end;j++) 
          { 
              if(array[j]<key)//如果此处元素小于基元,则把此元素和i+1处元素交换,并将i加1,如大于或等于基元则继续循环  
              { 
                  int temp=array[j]; 
                  array[j]=array[i+1]; 
                  array[i+1]=temp; 
                  i++; 
              } 
                
          } 
          array[start]=array[i];//交换i处元素和基元  
          array[i]=key; 
          QuickSort(array, start, i-1);//递归调用  
          QuickSort(array, i+1, end); 
            
      } 
        
  } 


//快速排序算法,这个比较复杂一点,如果可以,画图理解比较好,主要采取分治策略
public static void quickSort(int[] array,int low,int high){
if(low>=high){
return; // 如果左边的指针往右移超过或者等于了右边的指针,那么跳出递归
}
int left = low;
int right = high;
int baseValue = array[left];
while(left<right){//开启循环递归
while(left<right&&array[right]>=baseValue){
right--; //如果右边指针左移 一直都是比基准值大或者相等的,则继续左移
}
// 跳出了上面的循环,那么意味着右边指针找到比基准值小的数了,这时候填左边坑
array[left] = array[right];
while(left<right&&array[left]<baseValue){
left++; //同理,左边指针右移 一直都是比基准值小的数字,则继续右移
}
//跳出了上面的循环,那么意味着左边指针找到比基准值大的数字了,这时候填右边坑
array[right] = array[left];
//一次循环结束,则要把基准值放入左边坑
array[left] = baseValue;

//开启递归
quickSort(array,low,left-1);
quickSort(array,left+1,high);

}
}

打印以及交换:

public static void printArray(int [] a)
{
System.out.print("[");
for(int i=0;i<=a.length-1;i++)
{
if(i!=a.length-1)
System.out.print(a[i]+",");
else System.out.print(a[i]+"]");
}
}
public static void swapArrayAsc(int [] a, int x,int y)//,boolean sortFlag)
{
if(a[x]>a[y])
{
int temp=a[x];
a[x]=a[y];
a[y]=temp;
}
}


-----------------------------------------------======================================---------------------------------------------------------------------

取最值

public static int getMaxArray(int [] a)
{
int max=0;
for(int x=0;x<=a.length-1;x++)
{
if (a[x]>a[max])
{
max=x;
}
}
return a[max];
}
public static int getMaxArray2(int [] a)
{
int max=a[0];
for(int x=0;x<=a.length-1;x++)
{
if (a[x]>max)
{
max=a[x];
}
}
return max;
}


二分查找法:

public static int halfSearchGetIndex(int [] a,int key)
{
int  min=0,max=a.length-1,mid=(min+max)/2;
while(a[mid]!=key)
{

if(a[mid]<key)
{
max=mid-1;
}
else if(a[mid]>key)
{
min=mid+1;
}
if(max<min)
return -1;
mid=(min+max)/2;
}
return mid;

}
public static int halfSearchGetIndex2(int [] a,int key)
{
int min=0,max=a.length-1,mid=(min+max)/2;
while(min<=max)
{
mid=(min+max)/2;
if(a[mid]<key)
{
max=mid-1;
}
else if(a[mid]>key)
{
min=mid+1;
}
else return mid;
}
return -1;
}

如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。
原文地址:https://www.cnblogs.com/Frank99/p/5400004.html