排序算法
1、冒泡排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * 冒泡排序(按从小到大排序) * 最佳情况:T(n) = O(n) 最差情况:T(n) = O(n2) 平均情况:T(n) = O(n2) */ @Test public void bubbleSort() { int [] array = { 3 , 4 , 6 , 1 , 8 , 2 , 1 , 5 , 9 , 0 }; 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; } } } for ( int i : array) { System.out.print(i); } } |
2、选择排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * 选择排序(按从小到大排序) * 最佳情况:T(n) = O(n2) 最差情况:T(n) = O(n2) 平均情况:T(n) = O(n2) */ public void selectionSort() { int [] array = { 3 , 4 , 6 , 1 , 8 , 2 , 1 , 5 , 9 , 0 }; for ( int i = 0 ; i < array.length; i++) { int minIndex = i; for ( int j = i; j < array.length; j++) { if (array[j] < array[minIndex]) minIndex = j; } int temp = array[minIndex]; array[minIndex] = array[i]; array[i] = temp; } for ( int i : array) { System.out.print(i); System.out.print( " " ); } } |
3、插入排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * 插入排序(按从小到大排序) * 最佳情况:T(n) = O(n) 最坏情况:T(n) = O(n2) 平均情况:T(n) = O(n2) */ @Test public void insertionSort() { int [] array = { 3 , 4 , 6 , 1 , 8 , 2 , 1 , 5 , 9 , 0 }; int current; for ( int i = 0 ; i < array.length - 1 ; i++) { current = array[i + 1 ]; int preIndex = i; while (preIndex >= 0 && current < array[preIndex]) { array[preIndex + 1 ] = array[preIndex]; preIndex--; } array[preIndex + 1 ] = current; } for ( int i : array) { System.out.print(i); System.out.print( " " ); } } |
4、希尔排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * 从大到小 */ public static void shellSort1( int [] array) { int number = array.length/ 2 ; int i,j,temp; while (number >= 1 ){ for (i = number; i < array.length; i++) { temp = array[i]; j = i - number; while (j >= 0 && array[j] < temp) { array[j + number] = array[j]; j = j - number; } array[j + number] = temp; } number = number/ 2 ; } } |
5、归并排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** * 从小到大排序 * @param arr */ public static void sort( int []array){ int []temp = new int [array.length]; //在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间 sort(array, 0 ,array.length- 1 ,temp); } private static void sort( int [] array, int left, int right, int []temp){ if (left<right){ int mid = (left+right)/ 2 ; sort(array,left,mid,temp); // 左边归并排序,使得左子序列有序 sort(array,mid+ 1 ,right,temp); // 右边归并排序,使得右子序列有序 merge(array,left,mid,right,temp); // 将两个有序子数组合并操作 } } private static void merge( int [] arr, int left, int mid, int right, int [] temp){ int i = left; // 左序列指针 int j = mid + 1 ; // 右序列指针 int t = 0 ; // 临时数组指针 while (i <= mid && j <= right){ if (arr[i] <= arr[j]){ temp[t++] = arr[i++]; } else { temp[t++] = arr[j++]; } } while (i <= mid){ // 将左边剩余元素填充进temp中 temp[t++] = arr[i++]; } while (j <= right){ // 将右序列剩余元素填充进temp中 temp[t++] = arr[j++]; } t = 0 ; // 将temp中的元素全部拷贝到原数组中 while (left <= right){ arr[left++] = temp[t++]; } } |
6、快速排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | public static void main(String[] args) { int [] array = new int []{ 2 , 3 , 5 , 6 , 9 , 1 , 0 , 3 , 4 , 6 }; quickSort(array); for ( int i= 0 ;i<array.length;i++) { System.out.print(array[i] + " " ); } } /** * 快速排序,使得整数数组 arr 有序 */ public static void quickSort( int [] array) { if (array == null || array.length < 2 ) { return ; } quickSort(array, 0 , array.length - 1 ); } /** * 快速排序,使得整数数组 arr 的 [L, R] 部分有序 */ public static void quickSort( int [] arr, int L, int R) { if (L < R) { // 把数组中随机的一个元素与最后一个元素交换,这样以最后一个元素作为基准值实际上就是以数组中随机的一个元素作为基准值 swap(arr, new Random().nextInt(R - L + 1 ) + L, R); int [] p = partition(arr, L, R); quickSort(arr, L, p[ 0 ] - 1 ); quickSort(arr, p[ 1 ] + 1 , R); } } /** * 分区的过程,整数数组 arr 的[L, R]部分上,使得: * 大于 arr[R] 的元素位于[L, R]部分的右边,但这部分数据不一定有序 * 小于 arr[R] 的元素位于[L, R]部分的左边,但这部分数据不一定有序 * 等于 arr[R] 的元素位于[L, R]部分的中间 * 返回等于部分的第一个元素的下标和最后一个下标组成的整数数组 */ public static int [] partition( int [] arr, int L, int R) { int basic = arr[R]; int less = L - 1 ; int more = R + 1 ; while (L < more) { if (arr[L] < basic) { swap(arr, ++less, L++); } else if (arr[L] > basic) { swap(arr, --more, L); } else { L++; } } return new int [] { less + 1 , more - 1 }; } /* * 交换数组 arr 中下标为 i 和下标为 j 位置的元素 */ public static void swap( int [] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } |
7、堆排序 8、计数排序 9、桶排序 10、基数排序
递归
1、递归算法计算n!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * 递归算法 */ @Test public void recursion() { System.out.println(factorial( 2 )); } /** * 递归算法计算阶乘n! * * @param n * @return */ public static Integer factorial(Integer n) { if (n < 0 ) return 0 ; if (n == 1 ) return 1 ; return n * factorial(n - 1 ); } |
2、递归计算1*2+2*3+3*4+...+n*(n+1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * 递归算法 */ @Test public void recursion() { System.out.println(productSum( 4 )); } /** * 递归算法计算1*2+2*3+3*4+...+n*(n+1) * @param n * @return */ public static Integer productSum(Integer n) { if (n <= 0 ) return 0 ; if (n == 1 ) return 2 ; int result = productSum(n - 1 ) + n * (n + 1 ); return result; } |
now ,fight for future
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程