1. 程式人生 > >Java 常用排序演算法總結

Java 常用排序演算法總結

氣泡排序: 

/*冒泡演算法*/
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