排序

排序

1. 选择排序

package Ds.Sort.selectSort;

import java.util.Arrays;

/**
 * @Name: 选择排序
 * @Author:ZYJ
 * @Date:2019-08-13-16:09
 * @Description:
 */
public class selectSort {
    public static void main(String[] args) {
        int arr[] = new int[50];
        //获取随机数
        for(int i =0 ;i<50;i++){
            arr[i] = (int)(Math.random()*50);
        }
        System.out.println("排序前:");
        System.out.println(Arrays.toString(arr));

        selectSort(arr);
        System.out.println("排序后:");
        System.out.println(Arrays.toString(arr));

    }

    public static void selectSort(int[] arr){

        for(int i =0 ;i<arr.length;i++){
            int minIndex = i;
            int min = arr[i];

            for(int j =i+1;j<arr.length;j++){
                if(arr[j]<min){//  3ey升序降序只改这里
                    min =arr[j];
                    minIndex = j;
                }
            }

            if(minIndex!=i){
                arr[minIndex] =arr[i];
                arr[i] = min;
            }

        }
    }
}

2. 快速排序

package Ds.Sort.QuickSort;

import java.util.Arrays;

/**
 * @Name:快速排序实现
 * @Author:ZYJ
 * @Date:2019-08-12-16:53
 * @Description:
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {75,67,35,27,35,89,50,98,100};
        System.out.print("排序前:" + Arrays.toString(arr));
        quickSort(arr);
        System.out.println();
        System.out.println("排序后:" + Arrays.toString(arr));
    }

    public static void quickSort(int[] arr) {
        int low = 0;
        int height = arr.length - 1;
        quickSort(arr, low, height);
    }

    private static void quickSort(int[] arr, int low, int height) {
        //分区操作 返回分区边界索引
        //对左右分区进行快排
        if (low < height) {
            int index = partition(arr, low, height);
            quickSort(arr, low, index - 1);
            quickSort(arr, index + 1, height);
        }
    }

    private static int partition(int[] arr, int low, int height) {

        //指定左右指针
        int i = low;
        int j = height;


        //将第一个数作为基准数
        int x = arr[low];
        //使用循环实现分许操作
        while (i < j) {
            while (arr[j] > x && i < j) {
                j--;
            }
            if (i < j) {
                arr[i] = arr[j];
                i++;
            }


            while (arr[i] <= x && i < j) {
                i++;

            }
            if (i < j) {
                arr[j] = arr[i];
                j--;
            }
        }

        //将基准数填入
        arr[i] = x;
        //返回基准值的位置索引
        return i;

    }
}

3. 插入排序

package Ds.Sort.insertSort;

import java.util.Arrays;

/**
 * @Name:  插入排序
 * @Author:ZYJ
 * @Date:2019-08-13-21:15
 * @Description:
 */
public class insetSort {
    public static void main(String[] args) {
        int[] arr = {101,34,119,1};
        System.out.println(Arrays.toString(arr));
        insertSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void insertSort(int[] arr){
         int insertVal=0;
         int insertIndex=0;
        for(int i =1;i<arr.length;i++){
            insertVal = arr[i];
            insertIndex = i-1;
            while (insertIndex>=0 && insertVal<arr[insertIndex]){
                arr[insertIndex+1] = arr[insertIndex];
                insertIndex--;
            }
            if(insertIndex+1!=i)
            arr[insertIndex+1] = insertVal;
        }
    }
}

4. 冒泡排序

package Ds.Sort.bubbleSort;

import java.util.Date;

/**
 * @Name:  冒泡排序
 * @Author:ZYJ
 * @Date:2019-09-15-15:47
 * @Description:
 */
public class bubbleSort {
    public static void main(String[] args) {
        int arr[] = new int[80];
        //获取随机数
        for(int i =0 ;i<80;i++){
            arr[i] = (int)(Math.random()*50);
        }

        Date date = new Date();
        System.out.println(date);
        bubbleSort(arr);
        System.out.println(date);



    }
    public static int[] bubbleSort(int[] arr){
        for (int i=0;i<arr.length-1;i++){
            for (int j=0;j<arr.length-1-i;j++){
                if (arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1]= temp;
                }
            }
            /*System.out.println("第"+ i +"趟");
            System.out.println(Arrays.toString(arr));*/
        }
        return arr;
    }
}

5. 希尔排序

package Ds.Sort.shellSort;

import java.util.Arrays;

/**
 * @Name:希尔排序
 * @Author:ZYJ
 * @Date:2019-08-15-14:51
 * @Description:
 */
public class shellSort {
    public static void main(String[] args) {
        int[] arr = {2, 5, 0, 6, 3, 9, 4, 8, 1, 7};
        System.out.println(Arrays.toString(arr));
        shellSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    //交换法
    public static void shellSort(int[] arr) {
        int tmp =0 ;
        for(int gap =arr.length/2;gap>0;gap/=2){
            for(int i = gap;i<arr.length;i++){
                for(int j = i-gap ;j>=0;j -=gap){
                    if(arr[j]>arr[j+gap]){
                        tmp = arr[j];
                        arr[j] = arr[j+gap];
                        arr[j+gap] = tmp;
                    }
                }
            }
        }
    }

    //移动法
    public static  void shellSort2(int[] arr){
        for(int gap = arr.length/2;gap>0;gap/=2){
            for (int i=gap ;i< arr.length;i++){
                int j =i ;
                int temp = arr[i];
               if(arr[j] <arr[j-gap]){
                   while (j-gap>=0 && temp<arr[j-gap]){
                       arr[j] = arr[j-gap];
                       j=j-gap;
                   }
                   arr[j] = temp;
               }
            }
        }

    }

}

6. 堆排序

package Ds.Sort.heapSort;

import java.util.Arrays;

/**
 * @Name:  堆排序
 * @Author:ZYJ
 * @Date:2019-08-22-15:10
 * @Description:
 */
public class heapSort {
    public static void main(String[] args) {
        int[] arr = {8,2,5,3,4,6,9,7,0,1};
        System.out.println(Arrays.toString(arr));
        heapSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void heapSort(int[] arr){
        for(int i =arr.length/2-1;i>=0;i--){//最后一个非叶子结点
            adjustDown(arr,i,arr.length);
        }
        for (int j=arr.length-1;j>0 ;j--){
            int temp = arr[0];
            arr[0] = arr[j];
            arr[j]= temp;
            adjustDown(arr,0,j);
        }
    }

    private static void adjustDown(int[] arr,int i ,int length){
        int child=0;
        int temp = 0;
        for(temp = arr[i] ;i*2+1<length;i=child){
            child = 2*i+1;
            if (child!=length-1 && arr[child]<arr[child+1]){
                child++;
            }
            if(temp<arr[child]){
                arr[i] = arr[child];
            }else break;
        }
        arr[i] = temp;
    }
}

7.归并排序

package SortTest;

import java.util.Arrays;

/**
 * @Name: 归并排序
 * @Author:ZYJ
 * @Date:2019-09-15-17:52
 * @Description:
 */
public class margetSort {
    public static void main(String[] args) {
        int[] arr = {1, 6, 3, 8, 4, 9, 2, 5, 7};
        int[] temp = new int[arr.length];
        System.out.println(Arrays.toString(arr));
        margetSort(arr, 0, arr.length - 1, temp);
        System.out.println(Arrays.toString(arr));
    }
    public static void margetSort(int[] arr,int left ,int right ,int[] temp){
        if (left<right){
            int mid = (left+right)/2;
            margetSort(arr,left,mid,temp);
            margetSort(arr,mid+1,right,temp);
            marge(arr,left,mid,right,temp);
        }
    }
    private  static  void marge(int[] arr,int left,int mid,int right,int[] temp){
        int i =left;
        int j = mid+1;
        int t =0;
        while (i<=mid && j<=right){
            if (arr[i]>= arr[j]){
                temp[t++] = arr[j++];
            }else {
                temp[t++] = arr[i++];
            }
        }
        while (i<=mid){
            temp[t++] = arr[i++];
        }
        while (j<=right){
            temp[t++] = arr[j++];
        }

         t =0;
        int tempLeft = left;
        while (tempLeft<=right){
            arr[tempLeft++] = temp[t++];
        }

    }
}

你可能感兴趣的:(排序)