1-算法与数据结构(Algorithm and Data Structure )
来源:互联网 发布:淘宝花呗怎么开通 编辑:程序博客网 时间:2024/06/01 07:19
1-算法与数据结构(Algorithm and Data Structure )
最近学习一些经典算法,想在算法上有一个初步认识并拥有利用算法解决问题的能力;这个过程打算记录下来,以便日后查看;在任何语言的程序中:算法与数据结构不能完全分开,Algorithm + Data Structure=program(程序)研究算法并不能记住所有的算法,在学习的过程中把一些总结出来的算法进行分析理解,最后在我们自己的大脑中形成一种解决问题的中将从简单到复杂逐渐深入;线性算法-----》树------》图论
本周第一节记录线性算法【O(n^2)】:并不是越复杂的算法就越通用,我们在解决实际问题时,需要根据实际问题取舍;这些算法是基础: --编码简单,易于实现,是一些简单情景的首选 --在一些特殊情况下,简单的排序算法更有效 --简单的排序算法思想衍生出复杂的排序算法 --作为子过程,改进更复杂的排序算法
1、 选择排序
选择排序算法思想非常简单,遍历每一个值,遍历到某个值是查看他后面的所有值,记录最小(大)值的位置最后交换,遍历完成排序也就完成了
//排序算法 public static void sort(Comparable [] arr){ for(int i=0;i<arr.length-1;i++){ int minIndex=i; for(int j=i+1;j<arr.length;j++){ if (arr[minIndex].compareTo(arr[j])>0){ minIndex=j; } } SelectionSortHelp.swap(arr,i,minIndex); }// SelectionSortHelp.printArray(arr); } //SelectionSortHelp的swap就是一个交换值的过程 //交换值 public static void swap(Object [] arr,int i,int minIndex){ Object o=arr[i]; arr[i]=arr[minIndex]; arr[minIndex]=o; }
2、 插入排序:每次插入都会交换,优化与选择排序类似将位置记录,最后将值插入
插入排序有多中写法,但是都大同小异,思想是一样的,都是遍历数组,将此时的值与前面的值比较,直到找到这个值在前面内容中该放入的位置为止
public static void sort(Comparable[] arr){ for(int i=1;i<arr.length;i++){ //用当前值与前面所有值比较,如果后面值小于前面的值,交换 //第一种写法// for(int j=i;j>0 && arr[j].compareTo(arr[j-1])<0;j--){// SelectionSortHelp.swap(arr,j,j-1);// } //第二种写法: ---十万数据50s // ---同样数据选择排序59s// for(int j=i;j>0;j--){// if(arr[j].compareTo(arr[j-1])<0){// SelectionSortHelp.swap(arr,j,j-1);// }else{// break;//跳出循环,不再往前再比较// }// }//第三种写法:上面两种方法内部每比较一次就交换;//效率改进---十万个学生排序12s (略有改进) 100万数据1366s//---同样数据选择排序45s 100万数据 10179s Comparable arrI=arr[i];//记录最后此时i位置的值放入的位置 for(int j=i;j>0;j--){ if(arrI.compareTo(arr[j-1])<0){ arr[j]=arr[j-1]; }else{ arr[j]=arrI; break; } } } }
3、 冒泡排序
SelectionSortHelp.swap方法同样是一个交换方法
//冒泡排序 public static void sort(Comparable [] arr){ int n=arr.length; Boolean cycle=true; while (cycle){ cycle=false;//默认进来后就不再继续循环 for(int i=1;i<n;i++){ if(arr[i-1].compareTo(arr[i])>0){ SelectionSortHelp.swap(arr,i-1,i); cycle=true;//如果此次遍历中有值交换则说明应该继续循环下次遍历; } } n--; } }
4、shell排序:
其实shell排序时间复杂度是比较难计算的,这是刘宇波老师的写法(是O(n^3/2)级别的算法):GitHub网址-https://github.com/liuyubobobo/Play-with-Algorithms/tree/master/02-Sorting-Basic/Course%20Code%20(Java)/Optional-02-Shell-Sort/src/bobo/algo
public static void sort(Comparable[] arr){ int n = arr.length;//10 // 计算 increment sequence: 1, 4, 13, 40, 121, 364, 1093... int h = 1; while (h < n/3) h = 3*h + 1;//h=1 h=4 while (h >= 1) { // h-sort the array for (int i = h; i < n; i++) { // 对 arr[i], arr[i-h], arr[i-2*h], arr[i-3*h]... 使用插入排序 Comparable e = arr[i]; int j = i; for ( ; j >= h && e.compareTo(arr[j-h]) < 0 ; j -= h) arr[j] = arr[j-h]; arr[j] = e; } h /= 3; } }
不喜勿喷,侵权请至邮:liuxiaobo170601@gmail.com
请多指教—待续。。。
阅读全文
0 0
- 1-算法与数据结构(Algorithm and Data Structure )
- algorithm and data structure
- algorithm and data structure
- Data Structure and Algorithm
- android data structure And algorithm
- Algorithm and Data Structure Review
- [Data Structure and Algorithm]--Heap
- Use the Right Algorithm and Data Structure
- 资源向导之 "Data structure and Algorithm"
- Use the Right Algorithm and Data Structure
- [Data Structure & Algorithm] 七大查找算法
- [Data Structure & Algorithm] 七大查找算法
- [Data Structure & Algorithm] 七大查找算法
- [Data Structure & Algorithm] 八大排序算法
- 【Data Structure/Algorithm】LCSS算法实现
- Data Structures and Algorithm Analysis in C (数据结构与算法分析) 读书总结
- [Algorithm]九章八:Data Structure
- 数据结构与算法 Data Structures and Algorithms(作业1)
- new一个对象的时候JVM都做了哪些事情
- 嵌入式程序员应知道的几个基本问题
- python中的break语句
- java io流之拷贝文件
- 单例模式的记录
- 1-算法与数据结构(Algorithm and Data Structure )
- 自定义表情输入键盘
- Logger
- Java 常见报错
- Android hellocharts常见API总结
- ES6学习记录之class, extends, super和箭头函数与this
- 215. Kth Largest Element in an Array
- 怎么查看占用的端口号以及关掉
- Spring发布JAVA最初的远程调用技术---RMI