数据结构--排序
来源:互联网 发布:提升网站流量软件 编辑:程序博客网 时间:2024/05/21 19:35
记性不好,以此文章记录看过的代码。
数据结构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;}}
0 0
- 数据结构-排序
- 数据结构----排序
- 数据结构 - 排序
- 数据结构--排序
- 数据结构排序
- 数据结构 排序
- 数据结构排序
- 数据结构-----排序
- 数据结构排序
- 数据结构--排序
- 数据结构 排序
- 排序 - 数据结构
- 数据结构-排序
- 数据结构 - 排序
- 【数据结构】排序
- 数据结构 - 排序
- 数据结构-排序
- 数据结构排序
- JAVA基础笔记(复习)-单列设计模式 - 概述与体现
- Android中 内存溢出解决方法(OOM)
- 使用kali破解linux登录密码
- 如何学习一门新的知识
- 死锁原因及解决、避免办法
- 数据结构--排序
- Altium Designer Library 介绍 - 3M Card CompactFlash
- Oracle Sequence不设置cache参数的几个潜在问题
- Image Tips for Windows Phone 7 / 8
- Altium Designer Library 介绍 - 3M Footprints
- 设计模式之责任链模式
- 你做人能不能正常点?
- MSP430读写片内FLASH保存数据
- codeforces 380B Sereja and Tree 暴力枚举