11package com .algorithm .study .demo .algorithm ;
22
33import com .algorithm .study .demo .model .User ;
4+ import com .alibaba .fastjson .JSON ;
45
56import java .lang .reflect .Method ;
67import java .util .Arrays ;
@@ -24,14 +25,10 @@ public class SortProject {
2425 "heapSort" ,
2526 "binaryTreeSort"
2627 };
27- public static void main (String [] args ) {
28- int [] ls =new int []{1 ,30 ,15 ,11 ,40 };
29- quick (ls );
30- // try {
31- // performanceTest(10000);
32- // } catch (Exception e) {
33- // e.printStackTrace();
34- // }
28+ public static void main (String [] args ) throws Exception {
29+ int [] ls =new int []{30 ,1 ,15 ,11 ,40 };
30+ Method method = SortProject .class .getDeclaredMethod (methodNames [4 ], ls .getClass ());
31+ method .invoke (SortProject .class .newInstance (),ls );
3532 }
3633
3734 /**
@@ -58,10 +55,14 @@ public static void performanceTest(int len) throws Exception{
5855 }
5956 }
6057 /**
61- * 冒泡排序
62- * 两两相邻比较记录的关键字,如果反序就交换,直到没有反序的记录为止。
58+ * 冒泡排序:两两相邻比较记录的关键字,如果反序就交换,直到没有反序的记录为止。
59+ * 原地排序算法
60+ * 稳定排序算法
61+ * 时间复杂度为O(n²)
62+ * 空间复杂度为O(1)
6363 */
6464 private static void maopaoSort (int score []){
65+ System .out .println ("排序前:" + JSON .toJSONString (score ));
6566 boolean flag =true ;//数据发生了交换才继续冒泡
6667 for (int i = 1 ; i < score .length && flag ; i ++){ //最多做n-1趟排序
6768 flag =false ;
@@ -73,12 +74,8 @@ private static void maopaoSort(int score[]){
7374 flag =true ;//发生了数据交换
7475 }
7576 }
76- // System.out.print("第" + (i) + "趟排序结果:");
77- // for(int a = 0; a < score.length; a++){
78- // System.out.print(score[a] + "\t");
79- // }
80- // System.out.println("");
8177 }
78+ System .out .println ("排序后:" + JSON .toJSONString (score ));
8279 }
8380
8481 /**
@@ -93,9 +90,13 @@ private static void listSort(int ls[]){
9390 /**
9491 * 简单选择排序算法
9592 * 每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕
93+ * 空间复杂度为O(1)
94+ * 非稳定排序
95+ * 时间复杂度为O(n²)
9696 * 性能上优于冒泡
9797 */
9898 private static void selectSort (int ls []){
99+ System .out .println ("排序前:" + JSON .toJSONString (ls ));
99100 for (int i =0 ;i <ls .length -1 ;i ++){
100101 int min =i ;//记录数字最小的那个值的索引
101102 for (int j =(i +1 );j <ls .length ;j ++){
@@ -108,35 +109,34 @@ private static void selectSort(int ls[]){
108109 ls [i ]=ls [min ];
109110 ls [min ]=temp ;
110111 }
111- // System.out.print("第" + (i + 1) + "趟排序结果:");
112- // for(int a = 0; a < ls.length; a++){
113- // System.out.print(ls[a] + "\t");
114- // }
115- // System.out.println("");
116112 }
113+ System .out .println ("排序后:" + JSON .toJSONString (ls ));
117114 }
118115
119116 /**
120117 * 插入排序算法
121118 * 插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。
119+ * 将数组中的数据分为两个区间,已排序区间和未排序区间。初始已排序区间只有一个元素,就是数组的第一个元素。
120+ * 空间复杂度为O(1)
121+ * 稳定排序
122+ * 时间复杂度为O(n²)
122123 * 性能优于选择排序、冒泡排序
123- * {69, 70, 2, 87}
124124 */
125125 private static void insertSort ( int ls []){
126+ System .out .println ("排序前:" + JSON .toJSONString (ls ));
126127 for (int i =1 ;i <ls .length ;i ++){
127128 int key = ls [i ];//需要插入的元素
128129 int j = i -1 ;//已经排好序的末索引
129- for (;j >=0 &&key <ls [j ];j --){
130- ls [j +1 ]=ls [j ]; //将大于temp的值整体后移一个单位
130+ for (;j >=0 ;j --){
131+ if (key <ls [j ]) {
132+ ls [j + 1 ] = ls [j ];//将大于temp的值整体后移一个单位
133+ }else {
134+ break ;
135+ }
131136 }
132137 ls [j +1 ] = key ;
133-
134- // System.out.print("第" + (i + 1) + "趟排序结果:");
135- // for(int a = 0; a < ls.length; a++){
136- // System.out.print(ls[a] + "\t");
137- // }
138- // System.out.println("");
139138 }
139+ System .out .println ("排序后:" + JSON .toJSONString (ls ));
140140 }
141141 /**
142142 * 希尔排序(Shell)算法
@@ -229,9 +229,11 @@ public static void quickSortByList(List<User> list, int low, int high) {
229229 quickSortByList (list , hi , high );
230230 }
231231 public static void quick (int [] a2 ) {
232+ System .out .println ("排序前:" +JSON .toJSONString (a2 ));
232233 if (a2 .length > 1 ) { //查看数组是否为空
233234 quickSort (a2 , 0 , a2 .length - 1 );
234235 }
236+ System .out .println ("排序后:" +JSON .toJSONString (a2 ));
235237 }
236238
237239 /**
@@ -272,10 +274,14 @@ public static int[] merge(int[] a, int[] b){
272274 /**
273275 * 归并排序<br>
274276 * 把数组从中间一分为二,并对左右两部分递归调用,直到数组长度为1的时候,开始两两归并;<br>
277+ * 递推公式:
278+ * merge_sort(p…r) = merge(merge_sort(p…q), merge_sort(q+1…r))
279+ * 终止条件:
280+ * p >= r 不用再继续分解
275281 * 时间复杂度: 平均:O(nlogn),最好:O(nlogn);最坏:O(nlogn);
276282 * 空间复杂度: O(n);要为归并的结果分配空间
277- * @param a 待排序数组;
278- * @return 有序数组;
283+ * 稳定排序算法
284+ * 不是原地排序算法
279285 */
280286 public static int [] mergeSort (int [] a ){
281287 if (a .length ==1 ){
@@ -292,6 +298,7 @@ public static int[] mergeSort(int[] a){
292298 }
293299
294300
301+
295302 /**
296303 * 堆排序<br>
297304 * 堆的定义:堆是一个完全,或近似完全的二叉树,堆顶元素的值大于左右孩子的值,左右孩子也需要满足这个条件;<br>
0 commit comments