数据结构--排序
2014-01-13 22:11
197 查看
记性不好,以此文章记录看过的代码。
数据结构java版下载地址:http://download.csdn.net/detail/mr__fang/6792625
书中代码下载地址:http://download.csdn.net/detail/mr__fang/6794347
数据结构--排序--冒泡排序
数据结构java版下载地址:http://download.csdn.net/detail/mr__fang/6792625
书中代码下载地址:http://download.csdn.net/detail/mr__fang/6794347
数据结构--排序--冒泡排序
//冒泡排序 //算法复杂度:O(n^2) public class BubbleSort { public static void main(String[] args) { int[] arr = BubbleSort.sort1(new int[] { 50, 20, 6, 90, 1, 80, 75, 65, 74, 25 }); for(int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } } public static int[] sort1(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } public static int[] sort2(int[] arr) { for (int i = arr.length - 1; i > 1; i--) { for (int j = 0; j < i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } }
//选择排序 //时间复杂度:O(n^2) //空间复杂度:O(1)(用于交换和记录索引) public class SelectSort { public static void main(String[] args) { int[] arr = SelectSort.selectSort(new int[] { 50, 20, 6, 90, 1, 80, 75, 65, 74, 25 }); for(int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } } public static int[] selectSort(int arr[]){ for(int i = 0; i < arr.length - 1; i++){ int index = i; for(int j = i + 1; j < arr.length; j++){ if(arr[j] < arr[index]){ index = j; } } if(index != i){ int temp = arr[i]; arr[i] = arr[index]; arr[index] = temp; } } return arr; } }
//插入排序 //平均时间复杂度:O(n^2) //平均空间复杂度:O(1)(用于记录需要插入的数据) public class InsertSort { public static void main(String[] args) { int[] arr = InsertSort.insertSort(new int[] { 50, 20, 6, 90, 1, 80, 75, 65, 74, 25 }); for(int i = 0; i < arr.length; i++){ System.out.print(arr[i] + " "); } } public static int[] insertSort(int[] arr){ for(int i = 1; i < arr.length; i++){ if(arr[i - 1] > arr[i]){ int temp = arr[i]; int j = i; while(j > 0 && arr[j - 1] > temp){ arr[j] = arr[j - 1]; j--; } arr[j] = temp; } } return arr; } }
相关文章推荐
- 数据结构&算法实践—【排序|选择排序】选择排序
- 数据结构 - 2-路插入排序 详解 及 代码(C++)
- 数据结构之排序6--冒泡排序
- 数据结构与算法之排序
- 【数据结构】-快速排序Java实现
- 算法设计和数据结构学习_1(一道堆排序作业题)
- [置顶] 数据结构知识点总结--排序
- 和小强一起学习数据结构java版之直接插入排序
- 11-1-败者树-外部排序-第11章-《数据结构》课本源码-严蔚敏吴伟民版
- [置顶] 【scala 数据结构和算法】Scala实现:快速排序
- 数据结构之排序
- 数据结构 27 排序 排序-选择 插入 冒泡 希尔 快速归并 6种排序
- 数据结构java版四 排序应用
- 浅谈数据结构-交换排序(冒泡、快速)
- 数据结构 排序 基数排序(1)
- 数据结构和算法-002 数组排序 冒泡排序
- 数据结构-排序概述
- 【数据结构】直接插入排序
- 数据结构和算法分析之排序算法--选择排序(堆排序)
- 数据结构的排序用直接插入、折半插入、希尔排序、快速排序