java实现的各种排序算法代码示例
折半插入排序
折半插入排序是对直接插入排序的简单改进。此处介绍的折半插入,其实就是通过不断地折半来快速确定第i个元素的
插入位置,这实际上是一种查找算法:折半查找。java的arrays类里的binarysearch()方法,就是折半查找的实现,用
于从指定数组中查找指定元素,前提是该数组已经处于有序状态。与直接插入排序的效果相同,只是更快了一些,因
为折半插入排序可以更快地确定第i个元素的插入位置
代码:
package interview; /** * @author administrator * 折半插入排序 */ public class binaryinsertsort { public static void binaryinsertsort(datawrap[] data) { system.out.println("开始排序"); int arraylength = data.length; for (int i = 1; i < arraylength; i++) { datawrap temp = data[i]; int low = 0; int high = i - 1; while (low <= high) { int mid = (low + high) / 2; if (temp.compareto(data[mid]) > 0) { low = mid + 1; } else { high = mid - 1; } } for (int j = i; j > low; j--) { data[j] = data[j - 1]; } data[low] = temp; system.out.println(java.util.arrays.tostring(data)); } } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(-16, ""), new datawrap(21, "*"), new datawrap(23, ""), new datawrap(-30, ""), new datawrap(-49, ""), new datawrap(21, ""), new datawrap(30, "*"), new datawrap(30, "")}; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); binaryinsertsort(data); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
结果:
排序之前: [9, -16, 21*, 23, -30, -49, 21, 30*, 30] 开始排序 [-16, 9, 21*, 23, -30, -49, 21, 30*, 30] [-16, 9, 21*, 23, -30, -49, 21, 30*, 30] [-16, 9, 21*, 23, -30, -49, 21, 30*, 30] [-30, -16, 9, 21*, 23, -49, 21, 30*, 30] [-49, -30, -16, 9, 21*, 23, 21, 30*, 30] [-49, -30, -16, 9, 21, 21*, 23, 30*, 30] [-49, -30, -16, 9, 21, 21*, 23, 30*, 30] [-49, -30, -16, 9, 21, 21*, 23, 30, 30*] 排序之后: [-49, -30, -16, 9, 21, 21*, 23, 30, 30*]
冒泡排序
代码:
package interview; /** * @author administrator * 冒泡排序 */ public class bubblesort { public static void bubblesort(datawrap[] data) { system.out.println("开始排序"); int arraylength = data.length; for (int i = 0; i < arraylength - 1; i++) { boolean flag = false; for (int j = 0; j < arraylength - 1 - i; j++) { if (data[j].compareto(data[j + 1]) > 0) { datawrap temp = data[j + 1]; data[j + 1] = data[j]; data[j] = temp; flag = true; } } system.out.println(java.util.arrays.tostring(data)); if (!flag) break; } } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(-16, ""), new datawrap(21, "*"), new datawrap(23, ""), new datawrap(-30, ""), new datawrap(-49, ""), new datawrap(21, ""), new datawrap(30, "*"), new datawrap(30, "")}; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); bubblesort(data); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
运行结果:
排序之前: [9, -16, 21*, 23, -30, -49, 21, 30*, 30] 开始排序 [-16, 9, 21*, -30, -49, 21, 23, 30*, 30] [-16, 9, -30, -49, 21*, 21, 23, 30*, 30] [-16, -30, -49, 9, 21*, 21, 23, 30*, 30] [-30, -49, -16, 9, 21*, 21, 23, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] 排序之后: [-49, -30, -16, 9, 21*, 21, 23, 30*, 30]
桶式排序
算法的时间效率:时间效率极高,只需经过两轮遍历即可算法的空间效率:空间开销较大,需要两个数组来完成,算
法的稳定性:稳定
代码:
package interview; import java.util.arrays; /** * @author administrator * 桶式排序 */ public class bucketsort { public static void bucketsort(datawrap[] data, int min, int max) { system.out.println("开始排序"); int arraylength = data.length; datawrap[] temp = new datawrap[arraylength]; int[] buckets = new int[max - min]; for (int i = 0; i < arraylength; i++) { buckets[data[i].data - min]++; } system.out.println(arrays.tostring(buckets)); for (int i = 1; i < max - min; i++) { buckets[i] = buckets[i] + buckets[i - 1]; } system.out.println(arrays.tostring(buckets)); system.arraycopy(data, 0, temp, 0, arraylength); for (int k = arraylength - 1; k >= 0; k--) { data[--buckets[temp[k].data - min]] = temp[k]; } } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(5, ""), new datawrap(-1, ""), new datawrap(8, ""), new datawrap(5, "*"), new datawrap(7, ""), new datawrap(3, ""), new datawrap(-3, ""), new datawrap(1, ""),new datawrap(3, "*")}; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); bucketsort(data, -3, 10); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
结果
排序之前: [9, 5, -1, 8, 5*, 7, 3, -3, 1, 3*] 开始排序 [1, 0, 1, 0, 1, 0, 2, 0, 2, 0, 1, 1, 1] [1, 1, 2, 2, 3, 3, 5, 5, 7, 7, 8, 9, 10] 排序之后: [-3, -1, 1, 3, 3*, 5, 5*, 7, 8, 9]
堆排序
代码:
package interview; /** * @author administrator * 堆排序 */ public class heapsort { public static void heapsort(datawrap[] data) { system.out.println("开始排序"); int arraylength = data.length; // 循环建堆 for (int i = 0; i < arraylength - 1; i++) { // 建堆 builmaxdheap(data, arraylength - 1 - i); // 交换堆顶和最后一个元素 swap(data, 0, arraylength - 1 - i); system.out.println(java.util.arrays.tostring(data)); } } // 对data数组从0到lastindex建大顶堆 private static void builmaxdheap(datawrap[] data, int lastindex) { // 从lastindex处节点(最后一个节点)的父节点开始 for (int i = (lastindex - 1) / 2; i >= 0; i--) { // k保存当前正在判断的节点 int k = i; // 如果当前k节点的子节点存在 while (k * 2 + 1 <= lastindex) { // k节点的左子节点的索引 int biggerindex = 2 * k + 1; // 如果biggerindex小于lastindex,即biggerindex +1 // 代表k节点的右子节点存在 if (biggerindex < lastindex) { // 如果右子节点的值较大 if (data[biggerindex].compareto(data[biggerindex + 1]) < 0) { // biggerindex总是记录较大子节点的索引 biggerindex++; } } // 如果k节点的值小于其较大子节点的值 if (data[k].compareto(data[biggerindex]) < 0) { // 交换它们 swap(data, k, biggerindex); // 将biggerindex赋给k,开始while循环的下一次循环 // 重新保证k节点的值大于其左、右节点的值 k = biggerindex; } else { break; } } } } // 交换data数组中i、j两个索引处的元素 private static void swap(datawrap[] data, int i, int j) { datawrap temp = data[i]; data[i] = data[j]; data[j] = temp; } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(-16, ""), new datawrap(21, "*"), new datawrap(23, ""), new datawrap(-30, ""), new datawrap(-49, ""), new datawrap(21, ""), new datawrap(30, "*"), new datawrap(30, "")}; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); heapsort(data); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
结果:
排序之前: [9, -16, 21*, 23, -30, -49, 21, 30*, 30] 开始排序 [-16, 30, 21*, 23, -30, -49, 21, 9, 30*] [-16, 23, 21*, 9, -30, -49, 21, 30, 30*] [21, 9, 21*, -16, -30, -49, 23, 30, 30*] [-49, 9, 21*, -16, -30, 21, 23, 30, 30*] [-30, 9, -49, -16, 21*, 21, 23, 30, 30*] [-30, -16, -49, 9, 21*, 21, 23, 30, 30*] [-49, -30, -16, 9, 21*, 21, 23, 30, 30*] [-49, -30, -16, 9, 21*, 21, 23, 30, 30*] 排序之后: [-49, -30, -16, 9, 21*, 21, 23, 30, 30*]
直接插入排序
package interview; public class insertsort { public static void insertsort(datawrap[] data){ system.out.println("开始排序"); int arraylength = data.length; for(int i = 1;i < arraylength;i++){ datawrap temp = data[i]; if(data[i].compareto(data[i-1]) < 0){ int j = i -1; for(;j >= 0 && data[j].compareto(temp) > 0;j--){ data[j +1] = data[j]; } data[j + 1] = temp; } system.out.println(java.util.arrays.tostring(data)); } } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(-16, ""), new datawrap(21, "*"), new datawrap(23, ""), new datawrap(-30, ""), new datawrap(-49, ""), new datawrap(21, ""), new datawrap(30, "*"), new datawrap(30, "")}; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); insertsort(data); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
结果
排序之前: [9, -16, 21*, 23, -30, -49, 21, 30*, 30] 开始排序 [-16, 9, 21*, 23, -30, -49, 21, 30*, 30] [-16, 9, 21*, 23, -30, -49, 21, 30*, 30] [-16, 9, 21*, 23, -30, -49, 21, 30*, 30] [-30, -16, 9, 21*, 23, -49, 21, 30*, 30] [-49, -30, -16, 9, 21*, 23, 21, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] 排序之后: [-49, -30, -16, 9, 21*, 21, 23, 30*, 30]
归并排序
算法的时间效率:归并算法需要递归地进行分解、合并,每进行一趟归并排序,需要merge()方法一次,每次执行
merge()需要比较n次,较差,需要一个与原始序列同样大小的辅助序列。算法的稳定性:稳定
代码:
package interview; /** * @author administrator * 归并排序 */ public class mergesort { public static void mergesort(datawrap[] data) { // 归并排序 sort(data, 0, data.length - 1); } // 将索引从left到right范围的数组元素进行归并排序 private static void sort(datawrap[] data, int left, int right) { if(left < right){ //找出中间索引 int center = (left + right)/2; sort(data,left,center); sort(data,center+1,right); //合并 merge(data,left,center,right); } } // 将两个数组进行归并,归并前两个数组已经有序,归并后依然有序 private static void merge(datawrap[] data, int left, int center, int right) { datawrap[] temparr = new datawrap[data.length]; int mid = center + 1; int third = left; int temp = left; while (left <= center && mid <= right) { if (data[left].compareto(data[mid]) <= 0) { temparr[third++] = data[left++]; } else { temparr[third++] = data[mid++]; } } while (mid <= right) { temparr[third++] = data[mid++]; } while (left <= center) { temparr[third++] = data[left++]; } while (temp <= right) { data[temp] = temparr[temp++]; } } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(-16, ""), new datawrap(21, "*"), new datawrap(23, ""), new datawrap(-30, ""), new datawrap(-49, ""), new datawrap(21, ""), new datawrap(30, "*"), new datawrap(30, "") }; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); mergesort(data); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
结果:
排序之前: [9, -16, 21*, 23, -30, -49, 21, 30*, 30] 排序之后: [-49, -30, -16, 9, 21*, 21, 23, 30*, 30]
基数排序
基数排序已经不再是一种常规的排序方法,它更多地像是一种排序方法的应用,基数排序必须依赖于另外的排序方法。
基数排序的总体思路就是将待排数据拆分成多个关键字进行排序,也就是说,基数排序的实质是多关键字排序。
多关键字排序的思路是将待排数据里的排序关键字拆分成多个排序关键字:第1个子关键字、第2个子关键字、第3个子
关键字。。。然后,根据子关键字对待排数据进行排序。在进行多关键字排序时有两种解决方案:
最高位优先法msd
最低位优先法lsd
比较msd法和lsd法,一般来讲,lsd法要比msd法来得简单,因为lsd法是从头到尾进行若干次分配和收集,执行
的次数取决于构成关键字值的成分为多少;而msd法则要处理各序列与子序列的独立排序问题,就可能复杂一些。
代码:
package interview; import java.util.arrays; /** * @author administrator * 基数排序 */ public class multikeyradixsort { public static void radixsort(int[] data, int radix, int d) { system.out.println("开始排序:"); int arraylength = data.length; int[] temp = new int[arraylength]; int[] buckets = new int[radix]; for (int i = 0, rate = 1; i < d; i++) { // 重置count数组,开始统计第二个关键字 arrays.fill(buckets, 0); // 当data数组的元素复制到temp数组中进行缓存 system.arraycopy(data, 0, temp, 0, arraylength); for (int j = 0; j < arraylength; j++) { int subkey = (temp[j] / rate) % radix; buckets[subkey]++; } for (int j = 1; j < radix; j++) { buckets[j] = buckets[j] + buckets[j - 1]; } for (int m = arraylength - 1; m >= 0; m--) { int subkey = (temp[m] / rate) % radix; data[--buckets[subkey]] = temp[m]; } system.out.println("对" + rate + "位上子关键字排序:" + java.util.arrays.tostring(data)); rate *= radix; } } public static void main(string[] args) { int[] data = { 1100, 192, 221, 12, 13 }; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); radixsort(data, 10, 4); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
结果
排序之前: [1100, 192, 221, 12, 13] 开始排序: 对1位上子关键字排序:[1100, 221, 192, 12, 13] 对10位上子关键字排序:[1100, 12, 13, 221, 192] 对100位上子关键字排序:[12, 13, 1100, 192, 221] 对1000位上子关键字排序:[12, 13, 192, 221, 1100] 排序之后: [12, 13, 192, 221, 1100]
快速排序
代码:
package interview; /** * @author administrator * 快速排序 */ public class quicksort { private static void swap(datawrap[] data, int i, int j) { datawrap temp = data[i]; data[i] = data[j]; data[j] = temp; } private static void subsort(datawrap[] data, int start, int end) { if (start < end) { datawrap base = data[start]; int i = start; int j = end + 1; while (true) { while (i < end && data[++i].compareto(base) <= 0) ; while (j > start && data[--j].compareto(base) >= 0) ; if (i < j) { swap(data, i, j); } else { break; } } swap(data, start, j); subsort(data, start, j - 1); subsort(data, j + 1, end); } } public static void quicksort(datawrap[] data){ subsort(data,0,data.length-1); } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(-16, ""), new datawrap(21, "*"), new datawrap(23, ""), new datawrap(-30, ""), new datawrap(-49, ""), new datawrap(21, ""), new datawrap(30, "*"), new datawrap(30, "") }; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); quicksort(data); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
结果
排序之前: [9, -16, 21*, 23, -30, -49, 21, 30*, 30] 排序之后: [-49, -30, -16, 9, 21, 21*, 23, 30*, 30]
直接选择排序
代码:
package interview; /** * @author administrator * 直接选择排序 */ public class selectsort { public static void selectsort(datawrap[] data) { system.out.println("开始排序"); int arraylength = data.length; for (int i = 0; i < arraylength - 1; i++) { for (int j = i + 1; j < arraylength; j++) { if (data[i].compareto(data[j]) > 0) { datawrap temp = data[i]; data[i] = data[j]; data[j] = temp; } } system.out.println(java.util.arrays.tostring(data)); } } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(-16, ""), new datawrap(21, "*"), new datawrap(23, ""), new datawrap(-30, ""), new datawrap(-49, ""), new datawrap(21, ""), new datawrap(30, "*"), new datawrap(30, "") }; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); selectsort(data); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
排序之前: [9, -16, 21*, 23, -30, -49, 21, 30*, 30] 开始排序 [-49, 9, 21*, 23, -16, -30, 21, 30*, 30] [-49, -30, 21*, 23, 9, -16, 21, 30*, 30] [-49, -30, -16, 23, 21*, 9, 21, 30*, 30] [-49, -30, -16, 9, 23, 21*, 21, 30*, 30] [-49, -30, -16, 9, 21*, 23, 21, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] 排序之后: [-49, -30, -16, 9, 21*, 21, 23, 30*, 30]
希尔排序
代码:
package interview; /** * @author administrator * shell排序 */ public class shellsort { public static void shellsort(datawrap[] data) { system.out.println("开始排序"); int arraylength = data.length; int h = 1; /** * 将数组分割成若干个子序列 */ while (h <= arraylength / 3) { h = h * 3 + 1; system.out.println("h的结果:" + h); } while (h > 0) { system.out.println("===h的值:" + h + "==="); /** * 将分成的若干子序列进行直接插入排序 */ for (int i = h; i < arraylength; i++) { datawrap temp = data[i]; if (data[i].compareto(data[i - h]) < 0) { int j = i - h; for (; j >= 0 && data[j].compareto(temp) > 0; j -= h) { data[j + h] = data[j]; } data[j + h] = temp; } system.out.println(java.util.arrays.tostring(data)); } h = (h - 1) / 3; } } public static void main(string[] args) { datawrap[] data = { new datawrap(9, ""), new datawrap(-16, ""), new datawrap(21, "*"), new datawrap(23, ""), new datawrap(-30, ""), new datawrap(-49, ""), new datawrap(21, ""), new datawrap(30, "*"), new datawrap(30, "")}; system.out.println("排序之前:\n" + java.util.arrays.tostring(data)); shellsort(data); system.out.println("排序之后:\n" + java.util.arrays.tostring(data)); } }
结果:
排序之前: [9, -16, 21*, 23, -30, -49, 21, 30*, 30] 开始排序 h的结果:4 ===h的值:4=== [-30, -16, 21*, 23, 9, -49, 21, 30*, 30] [-30, -49, 21*, 23, 9, -16, 21, 30*, 30] [-30, -49, 21*, 23, 9, -16, 21, 30*, 30] [-30, -49, 21*, 23, 9, -16, 21, 30*, 30] [-30, -49, 21*, 23, 9, -16, 21, 30*, 30] ===h的值:1=== [-49, -30, 21*, 23, 9, -16, 21, 30*, 30] [-49, -30, 21*, 23, 9, -16, 21, 30*, 30] [-49, -30, 21*, 23, 9, -16, 21, 30*, 30] [-49, -30, 9, 21*, 23, -16, 21, 30*, 30] [-49, -30, -16, 9, 21*, 23, 21, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] [-49, -30, -16, 9, 21*, 21, 23, 30*, 30] 排序之后: [-49, -30, -16, 9, 21*, 21, 23, 30*, 30]
所需要的工具类:
package interview; public class datawrap implements comparable<datawrap>{ int data; string flag; public datawrap(int data, string flag) { this.data = data; this.flag = flag; } public string tostring(){ return data + flag; } @override public int compareto(datawrap dw) { return this.data > dw.data ? 1 : (this.data == dw.data ? 0 : -1); } }
以上代码亲测可用,供大家参考。
关于java实现的各种排序算法代码示例的内容,就到这里,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:java 蒙特卡洛算法求圆周率近似值实例详解、java编程实现递增排序链表的合并、java编程ssh整合常见错误解析等,有什么问题可以随时留言,小编会及时回复大家的。这里推荐本站几本关于java的书籍,免费下载,供广大编程爱好及工作者参考。
java经典实例(第三版) 完整版 ([美]达尔文) 中文pdf扫描版
数据挖掘:实用机器学习技术及java实现(英文第2版)高清pdf
java初级开发工程师面试题汇总.pdf
希望大家能够喜欢。
下一篇: PHP合并数组的2种方法小结