【面试必看】各种排序算法代码实现

来源:互联网 发布:广电局禁止网络机顶盒 编辑:程序博客网 时间:2024/06/05 10:51
    //冒泡排序bubbleSort,时间复杂度O(n^2),空间复杂度O(1),稳定    public static void bubbleSort(int[] arr){        int len = arr.length;        for(int i = 0; i < len; i++){            for(int j = 1; j < len - i; j++){                if(arr[j] < arr[j - 1]){                    int temp = arr[j];                    arr[j] = arr[j - 1];                    arr[j - 1] = temp;                }            }        }    }    //选择排序selectSort,时间复杂度O(n^2),空间复杂度O(1),不稳定    public static void selectSort(int[] arr){        int len = arr.length;        for(int i = 0; i < len; i++){            int min_index = i;            for(int j = i + 1; j < len; j++){                if(arr[j] < arr[min_index]){                    min_index = j;                }            }            int temp = arr[min_index];            arr[min_index] = arr[i];            arr[i] = temp;        }    }    //插入排序insertSort,时间复杂度O(n^2),空间复杂度O(1),稳定    public static void insertSort(int[] arr){        int len = arr.length;        for(int i = 0; i < len; i++){            int index = i;            int temp = arr[i];            while(index > 0 && arr[index - 1] > temp){                arr[index] = arr[index - 1];                index--;            }            arr[index] = temp;        }    }    //归并排序mergeSort,时间复杂度O(nlog(n)),空间复杂度O(n),稳定    public static void mergeSort(int[] arr){        sort(arr,0,arr.length - 1);    }    public static void sort(int[] arr,int low,int high){        if(low >= high){            return;        }        int mid = low + (high - low) / 2;        sort(arr,low,mid);        sort(arr,mid + 1,high);        merge(arr,low,mid,high);    }    public static void merge(int[] arr, int low,int mid, int high){        int len = arr.length;        int[] helper = new int[len];        for(int k = low; k <= high; k++){            helper[k] = arr[k];        }        int i = low;        int j = mid + 1;        for(int k = low; k <= high; k++){            if(i > mid){                arr[k] = helper[j];                j++;            }            else if(j > high){                arr[k] = helper[i];                i++;            }            else if(helper[i] < helper[j]){                arr[k] = helper[i];                i++;            }            else{                arr[k] = helper[j];                j++;            }        }    }    //快速排序quickSort,时间复杂度O(nlog(n)),空间复杂度O(1),不稳定    public static void quickSort(int[] arr){        quickSortCore(arr,0,arr.length - 1);    }    public static void quickSortCore(int[] arr,int low,int high){        if(low >= high){            return;        }        int pivot = arr[low];        int l = low + 1;        int h = high;        while(l <= h){            while(l <= h && arr[l] < pivot){                l++;            }            while(l <= h && arr[h] >= pivot){                h--;            }            if(l > h){                break;            }            int temp = arr[l];            arr[l] = arr[h];            arr[h] = temp;        }        int temp = arr[h];        arr[h] = arr[low];        arr[low] = temp;        quickSortCore(arr,low,h - 1);        quickSortCore(arr,h + 1,high);    }    //堆排序heapSort,时间复杂度O(nlog(n)),空间复杂度O(n),不稳定    //大根堆为例    public static void heapSort(int[] arr){        int len = arr.length;        buildHeap(arr,len - 1);        for(int i = len - 1; i >= 0; i--){            int temp = arr[0];            arr[0] = arr[i];            arr[i] = temp;            buildHeap(arr, i - 1);        }    }    public static void maxHeap(int[] arr, int index, int size){        if(index > size){            return;        }        int left_index = index * 2 + 1;        int right_index = (index + 1) * 2;        int max_index = index;        if(left_index <= size && arr[left_index] > arr[max_index]){            max_index = left_index;        }        if(right_index <= size && arr[right_index] > arr[max_index]){            max_index = right_index;        }        if(index != max_index) {            int temp = arr[max_index];            arr[max_index] = arr[index];            arr[index] = temp;            maxHeap(arr, max_index, size);        }    }    public static void buildHeap(int[] arr, int size){        for(int i = (size - 1) / 2; i >= 0; i--){            maxHeap(arr,i,size);        }    }}