博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
十大排序算法
阅读量:4946 次
发布时间:2019-06-11

本文共 5583 字,大约阅读时间需要 18 分钟。

1.java代码

public class SortUtil {    //冒泡排序    public static void bubble(int[] a) {        int len = a.length;        int flag = len;        while (flag > 0) {            flag = 0;            for (int i = 1; i < len; i++) {                if (a[i] < a[i - 1]) {                    int temp = a[i];                    a[i] = a[i - 1];                    a[i - 1] = temp;                    flag = i;                }            }            len = flag;        }    }    //简单选择排序    public static void select(int[] a) {        for (int i = 0; i < a.length - 1; i++) {            int min = i;            for (int j = i + 1; j < a.length; j++) {                if (a[j] < a[min]) {                    min = j;                }            }            if (i != min) {                int temp = a[min];                a[min] = a[i];                a[i] = temp;            }        }    }    //插入排序    public static void insert(int[] a) {        for (int i = 1; i < a.length; i++) {            int j = i;            int temp = a[i];            while (j > 0 && a[j - 1] > temp) {                a[j] = a[j - 1];                j--;            }            a[j] = temp;        }    }    //快速排序    public static void quick(int[] a, int start, int end) {        if (start > end) return;        int left = start;        int right = end;        int partition = a[start];        while (left < right) {            while (left < right && a[right] >= partition) {                right--;            }            while (left < right && a[left] <= partition) {                left++;            }            if (left < right) {                int temp = a[right];                a[right] = a[left];                a[left] = temp;            }        }        a[start] = a[left];        a[left] = partition;        quick(a, start, left - 1);        quick(a, right + 1, end);    }    //希尔排序    public static void shell(int[] arr) {        // i表示希尔排序中的第n/2+1个元素(或者n/4+1)        // j表示希尔排序中从0到n/2的元素(n/4)        // r表示希尔排序中n/2+1或者n/4+1的值        // 划组排序        for (int r = arr.length / 2; r >= 1; r = r / 2) {            for (int i = r; i < arr.length; i++) {                int tmp = arr[i];                int j = i - r;                // 一轮排序                while (j >= 0 && tmp < arr[j]) {                    arr[j + r] = arr[j];                    j -= r;                }                arr[j + r] = tmp;            }        }    }    //构建大根堆    public static void buildMaxHeap(int[] a, int lastIndex) {        for (int i = (lastIndex - 1) / 2; i >= 0; i--) {            int k = i;            int bigIndex = 2 * k + 1;            if (bigIndex + 1 <= lastIndex && a[bigIndex] < a[bigIndex + 1]) {                bigIndex++;            }            if (a[k] < a[bigIndex]) {                int temp = a[bigIndex];                a[bigIndex] = a[k];                a[k] = temp;            }        }    }    //堆排序    public static void heap(int[] a) {        for (int i = 0; i < a.length-1; i++) {            buildMaxHeap(a, a.length - 1 - i);            int temp = a[0];            a[0] = a[a.length - 1 - i];            a[a.length - 1 - i] = temp;        }    }    //合并    public static void merge(int[] a, int start, int mid, int end) {        int[] temp = new int[a.length];        int i = start;        int j = mid + 1;        int k = start;        while (i <= mid && j <= end) {            if (a[i] <= a[j]) {                temp[k++] = a[i++];            } else {                temp[k++] = a[j++];            }        }        while (i <= mid) {            temp[k++] = a[i++];        }        while (j <= end) {            temp[k++] = a[j++];        }        for (int n = start; n <= end; n++) {            a[n] = temp[n];        }    }    //归并排序    public static void merge(int[] a, int start, int end) {        if (start < end) {            int mid = (start + end) / 2;            merge(a, start, mid);            merge(a, mid + 1, end);            merge(a, start, mid, end);        }    }    //基数排序    public static void radix(int[] a) {        int disisor = 1;        int[][] bucket = new int[10][a.length];        int[] count = new int[10];        int digit;        for (int i = 1; i <= 3; i++) {            for (int temp : a) {                digit = (temp / disisor) % 10;                bucket[digit][count[digit]++] = temp;            }            int k = 0;            for (int m = 0; m < 10; m++) {                if (count[m] == 0) {                    continue;                }                for (int n = 0; n < count[m]; n++) {                    a[k++] = bucket[m][n];                }                count[m] = 0;            }            disisor *= 10;        }    }    //二分插入排序    public static void advanceInsertSortWithBinarySearch(int[] arr) {        for (int i = 1; i < arr.length; i++) {            int temp = arr[i];            int low = 0, high = i - 1;            int mid = -1;            while (low <= high) {                mid = low + (high - low) / 2;                if (arr[mid] > temp) {                    high = mid - 1;                } else { // 元素相同时,也插入在后面的位置                    low = mid + 1;                }            }            for(int j = i - 1; j >= low; j--) {                arr[j + 1] = arr[j];            }            arr[low] = temp;        }    }    //计数排序    public static void count(int[] a,int maxNum){        int[] b=new int[maxNum+1];//maxNum为数组a中最大的数,+1是因为有0这个数        for (int i=0;i
0){ a[k++]=j; b[j]--; } } }}

  

转载于:https://www.cnblogs.com/xiaobo520/p/10402495.html

你可能感兴趣的文章
bootloader,kernel,initrc
查看>>
Java中jshell脚本
查看>>
配置IIS
查看>>
NSSet和NSArray区别与方法总结
查看>>
Python列表 元组 字典 集合
查看>>
Still unable to dial persistent://blog.csdn.net:80 after 3 attempts
查看>>
基于busybox制作mini2440根文件系统及使用nfs挂载
查看>>
信道容量及信道编码原理学习
查看>>
从随机过程的熵率和马尔科夫稳态过程引出的一些思考 - 人生逃不过一场马尔科夫稳态...
查看>>
《A First Course in Abstract Algebra with Applications》-chaper1-数论-关于素数
查看>>
JS获取农历日期
查看>>
PHP中的HTTP协议
查看>>
Java WebService入门实例
查看>>
css样式之补充
查看>>
结构与联合
查看>>
BUPT复试专题—众数(2014)
查看>>
20145316 《信息安全系统设计基础》第十四周学习总结
查看>>
Liferay7 BPM门户开发之18: 理解ServiceContext
查看>>
Intel Galileo development documentation
查看>>
EV: Workaround to Allow Only One Instance or Window of outlook
查看>>