1、Java排序源代码一、插入排序 直接插入排序:InsertSort排序方法 最好时间 平均时间 最坏时间 辅助空间 稳定性直接插入排序 O(n) O(n2) O(n2) O(1) 稳定直接插入排序的基本思想是:顺序地把待排序的数据元素按其值的大小插入到已排序数据元素子集合的适当位置。子集合的数据元素个数从只有一个数据元素开始逐次增大。当子集合大小最终和集合大小相同时排序完毕。 public class InsertSort public static void insertSort(int a) int i, j, temp; int n = a.length; for (i = 0; i -
2、1 & temp = aj) aj + 1 = aj; j-; aj + 1 = temp; public static void main(String args) int test = 64, 5, 7, 89, 6, 24 ; int n = test.length; insertSort(test); for (int i = 0; i n; i+) System.out.print(testi + ); 折半插入排序:BinSort这般查找必须是有序的数组public class BinSort /* * param args */ public static void main(S
3、tring args) int arr = new int 1,12, 24, 32, 41, 65, 76, 478, 483, ; /用户输入查找的内容 System.out.println(请输入要查找的数值:); Scanner input = new Scanner(System.in); int target = input.nextInt(); /搜索开始时,查找范围的上下边界、中间元素的索引 int high = arr.length - 1; int low = 0; int mid = (high + low) / 2; /开始查找 while (low arrmid) /
4、目标比中值大,向上收缩搜索范围 low = mid + 1; else /目标比中值小,向下收缩搜索范围 high = mid - 1; /重新设定中间元素的索引 mid = (low + high) / 2; if (low high) System.out.println(目标不在数组中!); else System.out.println(目标在数组中第 + (mid + 1) + 个位置!); 希尔排序:ShellSort排序方法 最好时间 平均时间 最坏时间 辅助空间 稳定性希尔排序 O(n1.3) O(1) 不稳定希尔排序的基本思想是:把待排序的数据元素分成若干个小组,对同一小组内
5、的数据元素用直接插入法排序;小组的个数逐次缩小;当完成了所有数据元素都在一个组内的排序后排序过程结束。希尔排序又称作缩小增量排序。 public class ShellSort public static void shellSort(int a, int d, int numOfD) int i, j, k, m, span; int temp; int n = a.length; for (m = 0; m numOfD; m+) / 共numOfD次循环 span = dm; / 取本次的增量值 for (k = 0; k span; k+) / 共span个小组 for (i = k;
6、 i -1 & temp = aj) aj + span = aj; j = j - span; aj + span = temp; public static void main(String args) int test = 65, 34, 25, 87, 12, 38, 56, 46, 14, 77, 92, 23 ; int n = test.length; int numOfD = 3; int d = 6, 3, 1 ; shellSort(test, d, numOfD); for (int i = 0; i n; i+) System.out.print(testi + );
7、二、选择排序选择排序的基本思想是:每次从待排序的数据元素集合中选取最小(或最大)的数据元素放到数据元素集合的最前(或最后),数据元素集合不断缩小,当数据元素集合为空时排序过程结束。常用的选择排序有直接选择排序和堆排序两种。堆排序是一种基于完全二叉树的排序。 直接选择排序:直接选择排序的基本思想是:从待排序的数据元素集合中选取最小的数据元素并将它与原始数据元素集合中的第一个数据元素交换位置;然后从不包括第一个位置上数据元素的集合中选取最小的数据元素并将它与原始数据元素集合中的第二个数据元素交换位置;如此重复,直到数据元素集合中只剩一个数据元素为止。(1) public class SelectS
8、ort public static void selectSort(int a) int i, j, small; int temp; int n = a.length; for (i = 0; i n - 1; i+) small = i; / 设第i个数据元素最小 for (j = i + 1; j n; j+) / 寻找最小的数据元素 if (aj asmall) small = j; / 记住最小元素的下标 if (small != i) / 当最小元素的下标不为i时交换位置 temp = ai; ai = asmall; asmall = temp; public static vo
9、id main(String args) int test = 64, 5, 7, 89, 6, 24 ; int n = test.length; selectSort(test); for (int i = 0; i n; i+) System.out.print(testi + ); (2)稳定的直接选择排序排序方法 最好时间 平均时间 最坏时间 辅助空间 稳定性直接选择排序 O(n2) O(n2) O(n2) O(1) 稳定直接选择排序的基本思想是:从待排序的数据元素集合中选取最小的数据元素并将它与原始数据元素集合中的第一个数据元素交换位置;然后从不包括第一个位置上数据元素的集合中选取
10、最小的数据元素并将它与原始数据元素集合中的第二个数据元素交换位置;如此重复,直到数据元素集合中只剩一个数据元素为止。 public class SelectSort2 public static void selectSort2(int a) int i, j, small; int temp; int n = a.length; for (i = 0; i n - 1; i+) small = i; for (j = i + 1; j n; j+) / 寻找最小的数据元素 if (aj i; j-) / 把该区段尚未排序元素依次后移 aj = aj - 1; ai = temp; / 插入找
11、出的最小元素 public static void main(String args) int test = 64, 5, 7, 89, 6, 24, 5 ; int n = test.length; selectSort2(test); for (int i = 0; i n; i+) System.out.print(testi + ); 堆排序:HeapSort排序方法 最好时间 平均时间 最坏时间 辅助空间 稳定性堆排序 O(nlog2n) O(nlog2n) O(nlog2n) O(1) 不稳定在直接选择排序中,放在数组中的n个数据元素排成一个线性序列(即线性结构),要从有n个数据元
12、素的数组中选择出一个最小的数据元素需要比较n-1次。如果能把待排序的数据元素集合构成一个完全二叉树结构,则每次选择出一个最大(或最小)的数据元素只需比较完全二叉树的高度次,即log2n次,则排序算法的时间复杂度就是O(nlog2n)。这就是堆排序的基本思想。堆排序的基本思想是:循环执行如下过程直到数组为空:(1)把堆顶a0元素(最大元素)和当前最大堆的最后一个元素交换;(2)最大堆元素个数减1;(3)调整根结点使之满足最大堆的定义。最大堆的定义如下: 设数组a中存放了n个数据元素,数组下标从0开始,如果当数组下标2i+1n时有:aia2i+1;如果当数组下标2i+2n时有:aia2i+2,则这
13、样的数据结构称为最大堆。最小堆的定义如下: 设数组a中存放了n个数据元素,数组下标从0开始,如果当数组下标2i+1n时有:aia2i+1;如果当数组下标2i+2n时有:aia2i+2,则这样的数据结构称为最小堆。 public class HeapSort public static void createHeap(int a, int n, int h) int i, j, flag; int temp; i = h; / i为要建堆的二叉树根结点下标 j = 2 * i + 1; / j为i结点的左孩子结点的下标 temp = ai; flag = 0; / 沿左右孩子中值较大者重复向下筛
14、选 while (j n & flag != 1) / 寻找左右孩子结点中的较大者,j为其下标 if (j n - 1 & aj aj) / aiaj flag = 1; / 标记结束筛选条件 else / 否则把aj上移 ai = aj; i = j; j = 2 * i + 1; ai = temp; / 把最初的ai赋予最后的aj public static void initCreateHeap(int a) int n = a.length; for (int i = (n - 1) / 2; i = 0; i-) createHeap(a, n, i); public static
15、 void heapSort(int a) int temp; int n = a.length; initCreateHeap(a); / 初始化创建最大堆 for (int i = n - 1; i 0; i-) / 当前最大堆个数每次递减1 / 把堆顶a0元素和当前最大堆的最后一个元素交换 temp = a0; a0 = ai; ai = temp; createHeap(a, i, 0); / 调整根结点满足最大堆 public static void main(String args) int test = 10, 50, 32, 5, 76, 9, 40, 88 ; int n =
16、 test.length; heapSort(test); for (int i = 0; i ai+1,则交换两个数据元素,否则不交换,这样数值最大的数据元素将被放置在an-1中。第2趟时,循环次数减1,即数据元素个数为n-1,操作方法和第1趟的类似,这样整个n个数据元素集合中数值次大的数据元素将被放置在an-2中。当第n-1趟结束时,整个n个数据元素集合中次小的数据元素将被放置在a1中,a0中放置了最小的数据元素。public class BubbleSort public static void bubbleSort(int a) int i, j, flag=1; int temp;
17、int n = a.length; for(i = 1; i n & flag = 1; i+) flag = 0; for(j = 0; j aj+1) flag = 1; temp = aj; aj = aj+1; aj+1 = temp; public static void main(String args) int test = 64,5,7,89,6,24; int n = test.length; bubbleSort(test); for(int i = 0; i n; i+) System.out.print(testi + ); 快速排序:QuickSort排序方法 最好时
18、间 平均时间 最坏时间 辅助空间 稳定性快速排序 O(nlog2n) O(nlog2n) O(n2) O(log2n) 不稳定快速排序是一种二叉树结构的交换排序方法。快速排序算法的基本思想是:设数组a中存放了n个数据元素,low为数组的低端下标,high为数组的高端下标,从数组a中任取一个元素(通常取alow)做为标准元素,以该标准元素调整数组a中其他各个元素的位置,使排在标准元素前面的元素均小于标准元素,排在标准元素后面的均大于或等于标准元素。这样一次排序过程结束后,一方面将标准元素放在了未来排好序的数组中该标准元素应位于的位置上,另一方面将数组中的元素以标准元素为中心分成了两个子数组,位于
19、标准元素左边子数组中的元素均小于标准元素,位于标准元素右边子数组中的元素均大于等于或标准元素。对这两个子数组中的元素分别再进行方法类同的递归快速排序。算法的递归出口条件是lowhigh。 public class QuickSort public static void QuickSort(int a, int low, int high) int i, j; int temp; i = low; j = high; temp = alow; / 取第一个元素为标准数据元素 while (i j) / 在数组的右端扫描 while (i j & temp = aj) j-; if (i j)
20、ai = aj; i+; / 在数组的左端扫描 while (i j & ai temp) i+; if (i j) aj = ai; j-; ai = temp; if (low i) QuickSort(a, low, i - 1); / 对左端子集合递归 if (i high) QuickSort(a, j + 1, high); / 对右端子集合递归 public static void main(String args) int test = 60, 55, 48, 37, 10, 90, 84, 36 ; int n = 8; QuickSort(test, 0, 7); for
21、(int i = 0; i n; i+) System.out.print(testi + ); 四、归并排序排序方法 最好时间 平均时间 最坏时间 辅助空间 稳定性归并排序 O(nlog2n) O(nlog2n) O(nlog2n) O(n) 稳定 归并排序:MergeSort归并排序主要是二路归并排序。二路归并排序的基本思想是:设数组a中存放了n个数据元素,初始时我们把它们看成是n个长度为1的有序子数组,然后从第一个子数组开始,把相临的子数组两两合并,得到n/2个(若n/2为小数则上取整)长度为2的新的有序子数组(当n为奇数时最后一个新的有序子数组的长度为1);对这些新的有序子数组再两两归
22、并;如此重复,直到得到一个长度为n的有序数组为止。多于二路的归并排序方法和二路归并排序方法类同。 public class MergeSort public static void merge(int a, int swap, int k) int n = a.length; int m = 0, u1, l2, i, j, u2; int l1 = 0; / 第一个有序子数组下界为0 while (l1 + k = n - 1) l2 = l1 + k; / 计算第二个有序子数组下界 u1 = l2 - 1; / 计算第一个有序子数组上界 u2 = (l2 + k - 1 = n - 1)
23、? l2 + k - 1 : n - 1; / 计算第二个有序子数组上界 for (i = l1, j = l2; i = u1 & j = u2; m+) if (ai = aj) swapm = ai; i+; else swapm = aj; j+; / 子数组2已归并完,将子数组1中剩余的元素存放到数组swap中 while (i = u1) swapm = ai; m+; i+; / 子数组1已归并完,将子数组2中剩余的元素存放到数组swap中 while (j = u2) swapm = aj; m+; j+; l1 = u2 + 1; / 将原始数组中只够一组的数据元素顺序存放到
24、数组swap中 for (i = l1; i n; i+, m+) swapm = ai; public static void mergeSort(int a) int i; int n = a.length; int k = 1; / 归并长度从1开始 int swap = new intn; while (k n) merge(a, swap, k); / 调用函数merge() for (i = 0; i n; i+) ai = swapi; / 将元素从临时数组swap放回数组a中 k = 2 * k; / 归并长度加倍 public static void main(String
25、args) int test = 72, 73, 71, 23, 94, 16, 5, 68, 46 ; ; int n = test.length; mergeSort(test); for (int i = 0; i n; i+) System.out.print(testi + ); 五、基数排序排序方法 最好时间 平均时间 最坏时间 辅助空间 稳定性基数排序 O(mn) O(mn) O(mn) O(n) 稳定基数排序算法的基本思想是:设待排序的数据元素是m位d进制整数(不足m位的在高位补0),设置d个桶,令其编号分别为0,1,2,d-1。首先按最低位(即个位)的数值依次把各数据元素放到相应的桶中,然后按照桶号从小到大和进入桶中数据元素的先后次序收集分配在各桶中的数据元素,这样就形成了数据元素集合的一个新的排列,我们称这样的一次排序过程为一次基数排序;再对一次基数排序得到的数据元素序列按次低位(即十位)的数值依次把各
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1