1、冒泡排序
- 算法描述
- 比较相邻的元素。如果第一个比第二个大,就交换它们两个;
- 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素会是最大的数;
- 针对所有的元素重复以上的步骤,除了最后一个;
- 重复步骤1~3,直到排序完成。
package projectwork01; /** * 描述: * 冒泡排序 */ public class BubbleSort { public static void main(String[] args) { int[] array = {11, 3, 45, 28, 29,4,2,15}; bubbleSort(array); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } /** * 冒泡排序 * @param array * @return */ public static int[] bubbleSort(int[] array) { if(array.length == 0){ return array; } for(int i = 0; i < array.length; i++){ for(int j = 0; j < array.length - 1 - i; j++){ if (array[j + 1] < array[j]) { int temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } return array; } }
2、jdk 中的排序算法
-
2.1 Collections.sort()
//Collections.sort()方法 public static <T extends Comparable<? super T>> void sort(List<T> list) { list.sort(null); } //List接口中默认的方法 default void sort(Comparator<? super E> c) { Object[] a = this.toArray(); Arrays.sort(a, (Comparator) c); ListIterator<E> i = this.listIterator(); for (Object e : a) { i.next(); i.set((E) e); } } //List 接口中默认调用的是 Arrays.sort方法 public static <T> void sort(T[] a, Comparator<? super T> c) { if (c == null) { sort(a); } else { if (LegacyMergeSort.userRequested) legacyMergeSort(a, c); else TimSort.sort(a, 0, a.length, c, null, 0, 0); } }
-
2.2 Arrays.sort()
//Arrays.sort 方法 public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); } //Arrays.sort 方法调用的是DualPivotQuicksort.sort 方法 static void sort(int[] a, int left, int right,int[] work, int workBase, int workLen) { // Use Quicksort on small arrays if (right - left < QUICKSORT_THRESHOLD) { sort(a, left, right, true); return; } /* * Index run[i] is the start of i-th run * (ascending or descending sequence). */ int[] run = new int[MAX_RUN_COUNT + 1]; int count = 0; run[0] = left; // Check if the array is nearly sorted for (int k = left; k < right; run[count] = k) { if (a[k] < a[k + 1]) { // ascending while (++k <= right && a[k - 1] <= a[k]); } else if (a[k] > a[k + 1]) { // descending while (++k <= right && a[k - 1] >= a[k]); for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) { int t = a[lo]; a[lo] = a[hi]; a[hi] = t; } } else { // equal for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) { if (--m == 0) { sort(a, left, right, true); return; } } } /* * The array is not highly structured, * use Quicksort instead of merge sort. */ if (++count == MAX_RUN_COUNT) { sort(a, left, right, true); return; } } // Check special cases // Implementation note: variable "right" is increased by 1. if (run[count] == right++) { // The last run contains one element run[++count] = right; } else if (count == 1) { // The array is already sorted return; } // Determine alternation base for merge byte odd = 0; for (int n = 1; (n <<= 1) < count; odd ^= 1); // Use or create temporary array b for merging int[] b; // temp array; alternates with a int ao, bo; // array offsets from 'left' int blen = right - left; // space needed for b if (work == null || workLen < blen || workBase + blen > work.length) { work = new int[blen]; workBase = 0; } if (odd == 0) { System.arraycopy(a, left, work, workBase, blen); b = a; bo = 0; a = work; ao = workBase - left; } else { b = work; ao = 0; bo = workBase - left; } // Merging for (int last; count > 1; count = last) { for (int k = (last = 0) + 2; k <= count; k += 2) { int hi = run[k], mi = run[k - 1]; for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { b[i + bo] = a[p++ + ao]; } else { b[i + bo] = a[q++ + ao]; } } run[++last] = hi; } if ((count & 1) != 0) { for (int i = right, lo = run[count - 1]; --i >= lo; b[i + bo] = a[i + ao] ); run[++last] = right; } int[] t = a; a = b; b = t; int o = ao; ao = bo; bo = o; } }
3、代码评审
- 3.1 什么是代码评审
代码评审也称代码复查,通过阅读代码来检查代码与编码标准符合性以及代码质量的活动 - 3.2 代码评审的好处
- 提高代码质量
- 在项目的早期发现缺陷,将损失降至最低
- 评审的过程也是重新梳理思路的过程,双方都加深了对系统的理解
- 促进团队沟通、促进知识共享、共同提高
参考链接:
https://blog.csdn.net/xlgen157387/article/details/79863301
https://baike.baidu.com/item/%E4%BB%A3%E7%A0%81%E8%AF%84%E5%AE%A1
https://www.cnblogs.com/guoyaohua/p/8600214.html