9大常见算法

这里写图片描述
这里写图片描述
一篇稳定和非稳定文章
https://blog.csdn.net/yangnianjinxin/article/details/77918882

public class sjms {
    public static void main(String[] args) {
        int[] nums = new int[]{5, 4, 10, 24, 8, 12, 34, 59, 22, 33};
        Sort sort = new InsertSort();
        sort.sort(nums);
        printNums(nums);
        nums = new int[]{5, 4, 10, 24, 8, 12, 18, 59, 22, 33};
        sort = new ShellSort();
        sort.sort(nums);
        printNums(nums);
        nums = new int[]{8, 4, 10, 24, 8, 12, 18, 59, 22, 33};
        sort = new SelectMiddleInsertSort();
        sort.sort(nums);
        printNums(nums);
        nums = new int[]{8, 5, 10, 24, 8, 12, 18, 59, 22, 33};
        sort = new SelectSort();
        sort.sort(nums);
        printNums(nums);
        nums = new int[]{8, 5, 10, 24, 8, 12, 18, 59, 22, 33};
        sort = new HeapSort();
        sort.sort(nums);
        printNums(nums);
        nums = new int[]{122, 5, 10, 24, 8, 12, 18, 59, 22, 33};
        sort = new SwapSort();
        sort.sort(nums);
        printNums(nums);
        nums = new int[]{122, 5, 22222, 24, 8, 12, 18, 59, 22, 33};
        sort = new FastSort();
        sort.sort(nums);
        printNums(nums);
        nums = new int[]{23, 125, 22222, 24, 8, 12, 18, 59, 22, 33};
        sort = new MergeSort();
        sort.sort(nums);
        printNums(nums);
        nums = new int[]{2121, 125, 212, 24, 8, 12, 18, 59, 22, 33};
        sort = new RadixSort();
        sort.sort(nums);
        printNums(nums);
    }

    public static void printNums(int[] nums) {
        for (int num : nums) {
            System.out.print(num + " ");
        }
        System.out.println("");
    }
}

interface Sort {
    public void sort(int[] nums);
}

//插入排序
//直接插入排序
class InsertSort implements Sort {
    @Override
    public void sort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j > 0; j--) {
                if (nums[j] < nums[j - 1]) {
                    swapArray(nums, j, j - 1);
                } else {
                    break;
                }
            }
        }
    }

    private void swapArray(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

//希尔排序
class ShellSort implements Sort {

    @Override
    public void sort(int[] nums) {
        int k = nums.length / 2;
        while (k > 0) {
            for (int i = 0; i < nums.length - 1; i += k) {
                for (int j = i + 1; j - k >= 0; j--) {
                    if (nums[j] < nums[j - k]) {
                        swapArray(nums, j, j - 1);
                    } else {
                        break;
                    }
                }
            }
            k = k / 2;
        }
    }

    private void swapArray(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

//折半查找插入排序
class SelectMiddleInsertSort implements Sort {
    @Override
    public void sort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            int j = i + 1;
            int tmpValue = nums[j];
            int result = selectInsertPo(nums, j, tmpValue);
            while (j > result) {
                nums[j] = nums[j - 1];
                j--;
            }
            nums[result] = tmpValue;
        }
    }

    private int selectInsertPo(int[] nums, int length, int value) {
        int left = 0;
        int right = length;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] == value) {
                return mid;
            } else if (nums[mid] > value) {
                right = mid - 1;
            } else if (nums[mid] < value) {
                left = mid + 1;
            }
        }
        return left;
    }
}

//选择排序
//简单选择排序
class SelectSort implements Sort {
    @Override
    public void sort(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            int tmp = i;
            for (int j = i; j < nums.length; j++) {
                if (nums[tmp] > nums[j]) {
                    tmp = j;
                }
            }
            swapArray(nums, tmp, i);
        }
    }

    private void swapArray(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

//堆排序
class HeapSort implements Sort {

    @Override
    public void sort(int[] nums) {
        SortHeap(nums, nums.length);
    }

    private void SortHeap(int[] nums, int length) {
        for (int i = length / 2 - 1; i >= 0; i--) {
            adjustHeap(nums, i, length);
        }
        for (int i = length - 1; i > 0; i--) {
            swapArray(nums, 0, i);
            adjustHeap(nums, 0, i);
        }
    }

    private void adjustHeap(int[] nums, int i, int length) {
        int temp = nums[i];
        for (int k = i * 2 + 1; k < length; k = k * 2 + 1) {
            if (k + 1 < length && nums[k] < nums[k + 1]) {
                k++;
            }
            if (nums[k] > temp) {
                nums[i] = nums[k];
                i = k;
            } else {
                break;
            }
        }
        nums[i] = temp;
    }

    private void swapArray(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

//交换排序
//冒泡排序
class SwapSort implements Sort {
    @Override
    public void sort(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i; j < nums.length - i - 1; j++) {
                if (nums[j] > nums[j + 1]) {
                    swapArray(nums, j, j + 1);
                }
            }
        }
    }

    private void swapArray(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

//快速排序
class FastSort implements Sort {
    @Override
    public void sort(int[] nums) {
        quickSort(nums, 0, nums.length - 1);
    }

    public void quickSort(int[] nums, int left, int right) {
        int i = left;
        int j = right;
        int tmp = nums[left];
        if (left >= right) {
            return;
        }
        while (i != j) {
            while (i < j && nums[j] >= tmp) {
                j--;
            }
            if (j > i) {
                nums[i] = nums[j];
            }
            while (i < j && nums[i] <= tmp) {
                i++;
            }
            if (j > i) {
                nums[j] = nums[i];
            }
        }
        nums[i] = tmp;
        quickSort(nums, left, i - 1);
        quickSort(nums, i + 1, right);
    }
}

//归并排序
class MergeSort implements Sort {

    @Override
    public void sort(int[] nums) {
        mergeSort(nums, 0, nums.length - 1);
    }

    public void mergeSort(int[] nums, int left, int right) {
        if (left < right) {
            int middle = (left + right) / 2;
            mergeSort(nums, left, middle);
            mergeSort(nums, middle + 1, right);
            merge(nums, left, middle, right);
        }
    }

    public void merge(int[] nums, int left, int mid, int right) {
        int[] temp = new int[right - left + 1];
        int i = left;
        int j = mid + 1;
        int t = 0;
        while (i <= mid && j <= right) {
            if (nums[i] <= nums[j]) {
                temp[t] = nums[i];
                i++;
            } else {
                temp[t] = nums[j];
                j++;
            }
            t++;
        }
        while (i <= mid) {
            temp[t++] = nums[i++];
        }
        while (j <= right) {
            temp[t++] = nums[j++];
        }
        t = 0;
        while (left <= right) {
            nums[left++] = temp[t++];
        }
    }
}

//基数排序
class RadixSort implements Sort {

    @Override
    public void sort(int[] nums) {
        int maxNum = getMaxNums(nums);
        radixSort(nums, nums.length, maxNum);
    }

    public void radixSort(int[] nums, int n, int maxNum) {
        int length = n;
        int divisor = 1;
        int[][] bucket = new int[10][length];
        int[] count = new int[10];
        int digit;
        for (int i = 1; i <= maxNum; i++) {
            for (int temp : nums) {
                digit = (temp / divisor) % 10;
                bucket[digit][count[digit]++] = temp;
            }
            int k = 0;
            for (int b = 0; b < 10; b++) {
                if (count[b] == 0) {
                    continue;
                }
                for (int w = 0; w < count[b]; w++) {
                    nums[k++] = bucket[b][w];
                }
                count[b] = 0;
            }
            divisor *= 10;
        }
    }

    public int getMaxNums(int[] nums) {
        int maxNums = 0;
        for (int num : nums) {
            int tmpNums = (num + "").length();
            if (tmpNums > maxNums) {
                maxNums = tmpNums;
            }
        }
        return maxNums;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值