经典排序算法-java版

class Solution {
    public int[] sortArray(int[] nums) {
        return countingSort(nums);
    }

    void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    // 冒泡排序
    private int[] bubbleSort(int[] nums) {
        for (int i=1; i<nums.length; i++) {
            boolean flag = false;
            for (int j=0;j<nums.length-i;j++) {
                if (nums[j] > nums[j+1]) {
                    swap(nums, j, j+1);
                    flag = true;
                }
            }
            if (!flag) break;
        }
        return nums;
    }
    
    // 选择排序
    private int[] selectionSort(int[] nums) {
        for (int i=0; i<nums.length-1; i++) {
            int minIdx = i;
            for (int j=i+1; j<nums.length; j++) {
                 if (nums[minIdx] > nums[j]) {
                     minIdx = j;
                 }
            }
            if (minIdx != i) {
                swap(nums, i, minIdx);
            }
        }
        return nums;
    }
    
    // 插入排序
    private int[] insertionSort(int[] nums) {
        for (int i=1; i<nums.length; i++) {
            int cur = nums[i];
            int j=i-1;
            while (j>=0 && nums[j] > cur) {
                nums[j+1] = nums[j];
                j--;
            }
            nums[j+1] = cur;
        }
        return nums;
    }
    
    // 希尔排序
    private int[] shellSort(int[] nums) {
        int increment = nums.length;
        
        do {
            increment = increment / 3 + 1;
            for (int i=increment; i < nums.length; i++) {
                int temp = nums[i];
                int j = i - increment;
                while (j >= 0 && nums[j] > temp) {
                    nums[j + increment] = nums[j];
                    j -= increment;
                }
                nums[j + increment] = temp;
            }
        } while (increment > 1);
        
        return nums;
    }
    
    // 归并排序
    private int[] mergeSort(int[] nums, int left, int right) {
        if (left >= right) {
            return nums;
        }
        int mid = (left + right) / 2;
        mergeSort(nums, left, mid);
        mergeSort(nums, mid+1, right);
        return merge (nums, left, mid, right);
    }
    
    private int[] merge(int[] nums, int left, int mid, int right) {

        int i = left;
        int j = mid+1;
        int[] temp = new int[nums.length];
        int idx = 0;
        
        while (i <= mid && j <= right) {
            if (nums[i] < nums [j]) {
                temp[idx++] = nums[i++];
            } else {
                temp[idx++] = nums[j++];
            }
        }
        while (i <= mid) {
            temp[idx++] = nums[i++];
        }
        while (j <= right) {
            temp[idx++] = nums[j++];
        }

        idx = 0;
        while (left <= right) {
            nums[left++] = temp[idx++];

        }
        return nums;
    }
    
    // 快速排序
    private int[] quickSort(int[] nums, int left, int right) {
        if (left < right) {
            int partitionIdx = partition(nums, left, right);
            quickSort(nums, left, partitionIdx - 1);
            quickSort(nums, partitionIdx + 1, right);
        }
        return nums;
    }
    
    private int partition(int[] nums, int left, int right) {
        // int pivot = new Random().nextInt(right - left + 1) + left;
        int pivot = right;
        // swap(nums, pivot, right);
        int idx = left;
        for (int i=idx; i < right; i++) {
            if (nums[i] < nums[right]) {
                swap(nums, idx, i);
                idx++;
            }
        }
        swap(nums, right, idx);
        return idx;
    }
    
    // 堆排序
    private int[] heapSort(int[] nums) {
        int len = nums.length;
        for (int i=nums.length / 2; i >= 0; i--) {
            heapAdjust(nums, i, len);
        }
        
        for (int i=len-1; i > 0; i--) {
            swap(nums, 0, i);
            len--;
            heapAdjust(nums, 0, len);
        }
        return nums;
    }
    
    private void heapAdjust(int[] nums, int root, int len) {
        int left = root * 2 + 1;
        int right = root * 2 + 2;
        int maxIdx = root;
        
        if (left < len && nums[maxIdx] < nums[left]) {
            maxIdx = left;
        }
        if (right < len && nums[maxIdx] < nums[right]) {
            maxIdx = right;
        }
        
        if (root != maxIdx) {
            swap(nums, root, maxIdx);
            heapAdjust(nums, maxIdx, len);
        }
    }
    
    // 计数排序
    private int[] countingSort(int[] nums) {
        int maxValue = 0;
        for (int num : nums) {
            maxValue = Math.max(maxValue, num);
        }
        
        int bucketSize = maxValue + 1;
        int[] bucket = new int[bucketSize];
        for (int num : nums) {
            bucket[num]++;
        }
        
        int sortedIdx = 0;
        for (int i=0; i<bucketSize; i++) {
            while(bucket[i] > 0) {
                nums[sortedIdx++] = i;
                bucket[i]--;
            }
        }
        return nums;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值