五种常见的排序算法

包含:插入排序,选择排序,冒泡排序,快速排序,归并排序


class Solution { 
    public static void main(String[] args) { 
        int[] arr = {3,6,7,3,1,6,2,5,5,4}; 
        //insertSort(arr); 
        //bubbleSort(arr); 
        //selectSort(arr); 
        quickSort(arr, 0, arr.length-1); 
        //mergeSort(arr, 0, arr.length-1); 
        for(int i=0; i<arr.length; i++) { 
            System.out.println(arr[i]); 
        } 
    } 

    /*private static void insertSort(int[] arr) { 
        for(int i=1; i<arr.length; i++) { 
            int start = arr[i]; 
            int j; 
            for(j=i-1; j>=0 && arr[j]>start; j--) { 
                arr[j+1] = arr[j]; 
            } 
            arr[j+1] = start; 
        } 
    }*/ 
/* 
    private static void bubbleSort(int[] arr) { 
        for(int i=0; i<arr.length-1; i++) { 
            boolean flag = true; 
            for(int j=0; j<arr.length-i-1; j++) { 
                if(arr[j] > arr[j+1] ) { 
                    int temp = arr[j]; 
                    arr[j] = arr[j+1]; 
                    arr[j+1] = temp; 
                    flag = false; 
                } 
            } 
            if(flag) { 
                break; 
            } 
        } 
    }*/ 

    /*private static void selectSort(int[] arr) { 
        for(int i=0; i<arr.length-1; i++) { 
            int k = i; 
            for(int j=i+1; j<arr.length; j++) { 
                if(arr[j] < arr[k]) { 
                    k = j; 
                } 
            } 
            if(arr[k] != arr[i]) { 
                int temp = arr[k]; 
                arr[k] = arr[i]; 
                arr[i] = temp; 
            } 
        } 
    }*/ 

    /*private static void quickSort(int[] arr, int top, int bottom) { 
        if(top >= bottom) { 
            return; 
        } 
        int comp = arr[top]; 
        int i = top; 
        int j = bottom; 
        int temp; 
        while(i < j) { 
            while(i < j && arr[j] >= comp) { 
                j --; 
            } 
            arr[i] = arr[j]; 
            while(i < j && arr[i] < comp) { 
                i ++; 
            } 
            arr[j] = arr[i]; 
        } 

        arr[i] = comp; 
        quickSort(arr, top, j-1); 
        quickSort(arr, j+1, bottom); 
    }*/ 
     private static void quickSort(int[] arr, int top, int bottom) { 
            if(top >= bottom) { 
                return; 
            } 
            int comp = arr[top]; 
            int i = top; 
            int j = bottom; 
            int temp; 
            while(i < j) { 
                while(i < j && arr[j] >= comp) { 
                    j --; 
                } 
                temp = arr[j]; 
                arr[j] = arr[i]; 
                arr[i] = temp; 

                while(i < j && arr[i] < comp) { 
                    i ++; 
                } 
                temp = arr[j]; 
                arr[j] = arr[i]; 
                arr[i] = temp; 
            } 

            quickSort(arr, top, j-1); 
            quickSort(arr, j+1, bottom); 
        } 
    /*private static void mergeSort(int[] arr, int top, int bottom) { 
        if(top == bottom) { 
            return; 
        } 
        int mid = (top + bottom) / 2; 
        mergeSort(arr, top, mid); 
        mergeSort(arr, mid+1, bottom); 
        merge(arr, top, mid, bottom); 
    } 

    private static void merge(int[] arr, int top, int mid, int bottom) { 
        int len = bottom - top + 1; 
        int[] tempArr = new int[len]; 
        int j = top, k = mid+1; 
        int i = 0; 
        for(;j<=mid && k<=bottom;) { 
            if(arr[j] <= arr[k]) { 
                tempArr[i] = arr[j]; 
                j ++; 
                i ++; 
            } else { 
                tempArr[i] = arr[k]; 
                k ++; 
                i ++; 
            } 
        } 
        if(j == mid + 1) { 
            for(int l=k; l<=bottom; l++) { 
                tempArr[i ++] = arr[l]; 
            } 
        } else { 
            for(int l=j; l<=mid; l++) { 
                tempArr[i ++] = arr[l]; 
            } 
        } 

        for(i=0,j=top; i<len; i++,j++) { 
            arr[j] = tempArr[i]; 
        } 
    }*/ 

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值