• 【算法】—— 排序算法


    文章中算法多来自于牛客网左神算法基础班的示例代码,本文中只贴出关键代码部分。

    如有侵权,请告知。给左神打波广告,https://www.nowcoder.com/courses/semester/algorithm-elementary。

    讲的真的特别好,下面开始进入正题。

    1.冒泡排序

       public static void bubbleSort(int[] arr) {
            if (arr == null || arr.length < 2) {
                return;
            }
            for (int e = arr.length - 1; e > 0; e--) {
                for (int i = 0; i < e; i++) {
                    if (arr[i] > arr[i + 1]) {
                        swap(arr, i, i + 1);
                    }
                }
            }
        }
    
        public static void swap(int[] arr, int i, int j) {
            arr[i] = arr[i] ^ arr[j];
            arr[j] = arr[i] ^ arr[j];
            arr[i] = arr[i] ^ arr[j];
        }            

    2. 插入排序

        public static void insertionSort(int[] arr) {
            if (arr == null || arr.length < 2) {
                return;
            }
            for (int i = 1; i < arr.length; i++) {
                for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
                    swap(arr, j, j + 1);
                }
            }
        }
    
        public static void swap(int[] arr, int i, int j) {
            arr[i] = arr[i] ^ arr[j];
            arr[j] = arr[i] ^ arr[j];
            arr[i] = arr[i] ^ arr[j];
        }

    3. 选择排序

        public static void selectionSort(int[] arr) {
            if (arr == null || arr.length < 2) {
                return;
            }
            for (int i = 0; i < arr.length - 1; i++) {
                int minIndex = i;
                for (int j = i + 1; j < arr.length; j++) {
                    minIndex = arr[j] < arr[minIndex] ? j : minIndex;
                }
                swap(arr, i, minIndex);
            }
        }
    
        public static void swap(int[] arr, int i, int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }

    4. 堆排序

        public static void heapSort(int[] arr) {
            if (arr == null || arr.length < 2) {
                return;
            }
            for (int i = 0; i < arr.length; i++) {
                heapInsert(arr, i);
            }
            int size = arr.length;
            swap(arr, 0, --size);
            while (size > 0) {
                heapify(arr, 0, size);
                swap(arr, 0, --size);
            }
        }
    
        public static void heapInsert(int[] arr, int index) {
            while (arr[index] > arr[(index - 1) / 2]) {
                swap(arr, index, (index - 1) / 2);
                index = (index - 1) / 2;
            }
        }
    
        public static void heapify(int[] arr, int index, int size) {
            int left = index * 2 + 1;
            while (left < size) {
                int largest = left + 1 < size && arr[left + 1] > arr[left] ? left + 1 : left;
                largest = arr[largest] > arr[index] ? largest : index;
                if (largest == index) {
                    break;
                }
                swap(arr, largest, index);
                index = largest;
                left = index * 2 + 1;
            }
        }
    
        public static void swap(int[] arr, int i, int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }

    5. 快速排序

        public static void quickSort(int[] arr) {
            if (arr == null || arr.length < 2) {
                return;
            }
            quickSort(arr, 0, arr.length - 1);
        }
    
        public static void quickSort(int[] arr, int l, int r) {
            if (l < r) {
                swap(arr, l + (int) (Math.random() * (r - l + 1)), r);
                int[] p = partition(arr, l, r);
                quickSort(arr, l, p[0] - 1);
                quickSort(arr, p[1] + 1, r);
            }
        }
    
        public static int[] partition(int[] arr, int l, int r) {
            int less = l - 1;
            int more = r;
            while (l < more) {
                if (arr[l] < arr[r]) {
                    swap(arr, ++less, l++);
                } else if (arr[l] > arr[r]) {
                    swap(arr, --more, l);
                } else {
                    l++;
                }
            }
            swap(arr, more, r);
            return new int[] { less + 1, more };
        }
    
        public static void swap(int[] arr, int i, int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }

    6. 归并排序

        public static void mergeSort(int[] arr) {
            if (arr == null || arr.length < 2) {
                return;
            }
            mergeSort(arr, 0, arr.length - 1);
        }
    
        public static void mergeSort(int[] arr, int l, int r) {
            if (l == r) {
                return;
            }
            int mid = l + ((r - l) >> 1);
            mergeSort(arr, l, mid);
            mergeSort(arr, mid + 1, r);
            merge(arr, l, mid, r);
        }
    
        public static void merge(int[] arr, int l, int m, int r) {
            int[] help = new int[r - l + 1];
            int i = 0;
            int p1 = l;
            int p2 = m + 1;
            while (p1 <= m && p2 <= r) {
                help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
            }
            while (p1 <= m) {
                help[i++] = arr[p1++];
            }
            while (p2 <= r) {
                help[i++] = arr[p2++];
            }
            for (i = 0; i < help.length; i++) {
                arr[l + i] = help[i];
            }
        }

    7. 桶排序

        public static void bucketSort(int[] arr) {
            if (arr == null || arr.length < 2) {
                return;
            }
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < arr.length; i++) {
                max = Math.max(max, arr[i]);
            }
            int[] bucket = new int[max + 1];
            for (int i = 0; i < arr.length; i++) {
                bucket[arr[i]]++;
            }
            int i = 0;
            for (int j = 0; j < bucket.length; j++) {
                while (bucket[j]-- > 0) {
                    arr[i++] = j;
                }
            }
        }

    8. 基数排序

        public static void radixSort(int[] arr) {
            if (arr == null || arr.length < 2) {
                return;
            }
            radixSort(arr, 0, arr.length - 1, maxbits(arr));
        }
    
        public static int maxbits(int[] arr) {
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < arr.length; i++) {
                max = Math.max(max, arr[i]);
            }
            int res = 0;
            while (max != 0) {
                res++;
                max /= 10;
            }
            return res;
        }
    
        public static void radixSort(int[] arr, int begin, int end, int digit) {
            final int radix = 10;
            int i = 0, j = 0;
            int[] count = new int[radix];
            int[] bucket = new int[end - begin + 1];
            for (int d = 1; d <= digit; d++) {
                for (i = 0; i < radix; i++) {
                    count[i] = 0;
                }
                for (i = begin; i <= end; i++) {
                    j = getDigit(arr[i], d);
                    count[j]++;
                }
                for (i = 1; i < radix; i++) {
                    count[i] = count[i] + count[i - 1];
                }
                for (i = end; i >= begin; i--) {
                    j = getDigit(arr[i], d);
                    bucket[count[j] - 1] = arr[i];
                    count[j]--;
                }
                for (i = begin, j = 0; i <= end; i++, j++) {
                    arr[i] = bucket[j];
                }
            }
        }
    
        public static int getDigit(int x, int d) {
            return ((x / ((int) Math.pow(10, d - 1))) % 10);
        }
  • 相关阅读:
    Dubbo(八):timeout超时机制解析
    Visual Studio 2022 开发Python,新建py文件编码是gb2312,怎么样才能变为utf8
    Python 使用ORM框架 SQLAlchemy
    python log.py
    网络传输文件 极连快传 (360.cn)
    python sqlite_helper.py
    Newtonsoft.Json null值不序列化
    python爬虫开发与项目实战 范传辉 2017
    python 常见错误解决方法
    黑白照片转彩色
  • 原文地址:https://www.cnblogs.com/bopo/p/9298048.html
Copyright © 2020-2023  润新知