原创

Java六种经典排序

1、选择排序

public class ThreeSort {
 public static void main(String[] args) {
  //选择排序
  int[] ary={5,9,1,3,4,7,6,2,0,8,10};
  for(int i=0;i<ary.length-2;i++){
   for(int j=i+1;j<ary.length-1;j++){
    if(ary[i]>ary[j]){
     int temp=ary[i];
     ary[i]=ary[j];
     ary[j]=temp;
    }
   }
  }
  System.out.println(Arrays.toString(ary));
}
}

2、冒泡排序

public class ThreeSort {
 public static void main(String[] args) {
  //冒泡排序(从后往前排,每次都找到最小值,并往前移)
  int[] ary1={5,9,1,3,4,7,6,2,0,8,10};
  for(int i=0;i<ary1.length-1;i++){
   for(int j=ary1.length-1;j>i;j--){
    if(ary1[j]<ary1[j-1]){
     int temp=ary1[j];
     ary1[j]=ary1[j-1];
     ary1[j-1]=temp;
    }
   }
  }
  System.out.println(Arrays.toString(ary1));
  
  //冒泡排序(从前往后派,每一次都找到最大值,并往后移)
  int[] ary2={5,9,1,3,4,7,6,2,0,8,10};
  for(inti=0;i<ary2.length-1;i++){
   for(int j=0;j<ary2.length-1-i;j++){
    if(ary2[j]>ary2[j+1]){
     int temp=ary2[j];
     ary2[j]=ary2[j+1];
     ary2[j+1]=temp;
    }
   }
  }
  System.out.println(Arrays.toString(ary2));
 }
}

3、插入排序(升序)

public class ThreeSort {
 public static void main(String[] args) {
  //插入排序(升序)
  int[] ary3={10,5,9,1,3,4,7,6,2,0,8};
  for(int i=1;i<=ary3.length-1;i++){
   int temp=ary3[i];
   int j;
   for(j=i-1;j>=0&&temp<ary3[j];j--){
    ary3[j+1]=ary3[j];
   }
   ary3[j+1]=temp;
  }
  System.out.println(Arrays.toString(ary3));
 }
}

4、希尔排序

public class ThreeSort {
 public static void main(String[] args) {
   //希尔排序
   int[] ary4={10,5,9,1,3,4,7,6,2,0,8};
   for (int i = ary4.length / 2; i > 0; i /= 2) {
        for (int j = i; j < ary4.length; j++) {
            for (int k = j; k > 0  && k - i >= 0; k -= i) {
                if (ary4[k] < ary4[k - i]) {
                    int temp = ary4[k - i];
                    ary4[k - i] = ary4[k];
                    ary4[k] = temp;
                } else break;
            }
        }
    }
    System.out.println(Arrays.toString(ary4));
 }
}

5、快速排序

    public static int[] quickSort(int arr[],int left,int right) {
        int pivot = arr[left];//轴值
        int i = left;//左下标
        int j = right;//右下标
        while (i < j) {
            //在右边找到一个比中值小或者相等的值
            while (i < j && arr[j] > pivot) {
                j--;
            }
            //在左边找到一个比中值大或者相等的值
            while (i < j && arr[i] < pivot) {
                i++;
            }
            //在i和j没有相遇时,如果 arr[i] == arr[j] 此时让i+1
            //即让arr[i+1] 与arr[j]进行交换 ,使两个相同的数在一起
            if (arr[i] == arr[j] && i < j) {
                i++;
            } else {//交换
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        //左半部递归
        if (i-1 > left) {
            arr=quickSort(arr,left,i-1);
        }
        //右半部递归
        if (j+1 < right) {
            arr=quickSort(arr,j+1,right);
        }

        return arr;
    }

6、二分排序法

public class Test 
{
   public static void main(String[] args) 
   {
	   int[] arr = {1,3,5,7,9,11};
	   int key = 3;
	   int high = arr.length - 1;
	   int low = 0;
	   int middle = 0;
	   while(low <= high)
	   {
		   middle = (high + low)/2;
		   if (key < arr[middle])
		   {
			   high = middle - 1;
		   }
		   else if(key > arr[middle])
		   {
			   low = middle + 1;
		   }
		   else
		   {
			   System.out.println("查找的元素位置:" + middle);
			   return;
		   }
	   }
	   System.out.println("序列表没有找到该" + key);
	   return;
   }
}

本文链接地址:http://www.ysxbohui.com/article/45

正文到此结束