Java 常用排序算法总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kimiszc/article/details/83582298

冒泡排序: 

/*冒泡算法*/
public class BubbleSort {
    public static void bubble_sort(int[] arr){
        int temp;
        for(int i = 0; i < arr.length - 1;i++){
            for(int j = arr.length - 1; j > i; j--){
                if(arr[j] < arr[j - 1]){
                    temp = arr[j];
                    arr[j] = arr[j -1];
                    arr[j - 1] = temp;
                }
            }
            show(arr);
        }

    }



    private static void show(int[] arr){
        for(int a:arr){
            System.out.print(a + " ");
        }
        System.out.println();
    }

    public static void main(String[] args){
        int[] data = {42,20,17,13,20,14,23,15};
        show(data);
        System.out.println();
        bubble_sort(data);
        System.out.println();
        show(data);
    }
}

 输出结果:

42 20 17 13 20 14 23 15 

13 42 20 17 14 20 15 23 
13 14 42 20 17 15 20 23 
13 14 15 42 20 17 20 23 
13 14 15 17 42 20 20 23 
13 14 15 17 20 42 20 23 
13 14 15 17 20 20 42 23 
13 14 15 17 20 20 23 42 

13 14 15 17 20 20 23 42 

选择排序: 

/*选择排序算法*/
public class SelectionSort {
    public static void selection_sort(int[] arr){
        int length = arr.length;
        int minIndex;
        for(int i = 0; i < length - 1;i++){
            minIndex = i;
            for(int j = i + 1; j < length; j++){
                if(arr[j] < arr[minIndex])
                    minIndex = j;
            }
            if(minIndex != i){
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
            show(arr);
        }
    }

    private static void show(int[] arr){
        for(int a:arr){
            System.out.print(a + " ");
        }
        System.out.println();
    }

    public static void main(String[] args){
        int[] data = {42,20,17,13,20,14,23,15};
        show(data);
        System.out.println();
        selection_sort(data);
        System.out.println();
        show(data);
    }

}

输出结果:

42 20 17 13 20 14 23 15 

13 20 17 42 20 14 23 15 
13 14 17 42 20 20 23 15 
13 14 15 42 20 20 23 17 
13 14 15 17 20 20 23 42 
13 14 15 17 20 20 23 42 
13 14 15 17 20 20 23 42 
13 14 15 17 20 20 23 42 

13 14 15 17 20 20 23 42 

插入排序: 

/*插入算法*/

public class InsertionSort {
    public static void insertion_sort(int[] arr){
        int temp;
        for(int i = 0; i < arr.length - 1; i++){
            for(int j = i + 1; j > 0;j--){
                if(arr[j] < arr[j-1]){
                    temp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                }
                else break;
            }
            show(arr);
        }
    }
    
    private static void show(int[] arr){
        for(int a:arr){
            System.out.print(a + " ");
        }
        System.out.println();
    }

    public static void main(String[] args){
        int[] data = {42,20,17,13,20,14,23,15};
        show(data);
        System.out.println();
        insertion_sort(data);
        System.out.println();
        show(data);
    }
}

输出结果:

42 20 17 13 20 14 23 15 

20 42 17 13 20 14 23 15 
17 20 42 13 20 14 23 15 
13 17 20 42 20 14 23 15 
13 17 20 20 42 14 23 15 
13 14 17 20 20 42 23 15 
13 14 17 20 20 23 42 15 
13 14 15 17 20 20 23 42 

13 14 15 17 20 20 23 42 

希尔排序: 

/*希尔排序*/
import java.util.Scanner;

public class ShellSort {
    public static void shell_sort(int[] arr, int size){
        int temp = 0;
        if(arr.length < size){
            System.out.println("Sorry, you need to change the size of batch");
        }
        else {
            for(int j = size; j > 0; j--){
                for(int i = 0; i + j < arr.length; i++){
                    if(arr[i + j] < arr[i]) {
                        temp = arr[i];
                        arr[i] = arr[i + j];
                        arr[i + j] = temp;
                    }
                }
                show(arr);
            }
        }
    }

    private static void show(int[] arr){
        for(int a:arr){
            System.out.print(a + " ");
        }
        System.out.println();
    }

    public static void main(String[] args){
        int[] data = {42,20,17,13,20,14,23,15};
        show(data);
        System.out.println("please enter the size of sort batch");
        Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        System.out.println();
        shell_sort(data,size);
        System.out.println();
        show(data);
    }
}

输出结果:

42 20 17 13 20 14 23 15 
please enter the size of sort batch
3//符合数组大小要求

13 20 14 23 15 17 42 20 
13 20 14 17 15 20 42 23 
13 14 17 15 20 20 23 42 

13 14 17 15 20 20 23 42 



42 20 17 13 20 14 23 15 
please enter the size of sort batch
12//超出预定数组大小

Sorry, you need to change the size of batch

42 20 17 13 20 14 23 15 

猜你喜欢

转载自blog.csdn.net/kimiszc/article/details/83582298