java各大排序算法总结
来源:互联网 发布:文网文添加域名 编辑:程序博客网 时间:2024/06/05 16:34
java各大排序算法总结
import java.util.Arrays;/** * @author su: 排序算法 * @date time:2016年9月12日 下午9:11:49 * @return */public class Sorted { public void swap(int a[],int x,int y) { int t = a[x]; a[x] = a[y]; a[y] = t; } /** * 时间复杂度是o(n^2),稳定的算法 * @param a */ public void bubble(int a[]) { int n = a.length; for (int i = 0 ; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (a[j + 1] < a[j]) { swap(a,j,j + 1); } } } } /** * * 选择排序,时间复杂度是o(n^2),不是稳定的算法 * @param a */ public void selection(int a[]) { for (int i = 0; i < a.length; i++) { int min = i; for (int j = i + 1 ;j < a.length; j++) { if (a[j] < a[min]) { min = j; } } swap(a,i,min); } } /* * 插入排序:如果有序时候,时间复杂度是o(n) * * 稳定性,是稳定的 */ public void insertion(int a[]) { for (int i = 0 ; i < a.length; i++) { for (int j = i ;j > 0; j--) { if (a[j] < a[j - 1]) { swap(a,j,j - 1); } else { break; } } } } /** * 二分查找插入排序 * @param a * @return */ public void binaryInsertion(int a[]) { for (int i = 0 ; i < a.length; i++) { int t = a[i]; int index = binnarySearch(a, a[i], i);//找到 for (int j = i; j > index; j--) { a[j] = a[j - 1]; } a[index] = t; } } /** * 二分查找 * @param a * @param target */ public int binnarySearch(int a[],int target ,int len) { int lo = 0; int hi = len - 1; while (lo <= hi) { int mid = (lo + hi) >> 1; if (a[mid] == target) { return mid; } else if (a[mid] < target) { lo = mid + 1; } else { hi = mid - 1; } } return lo; } public boolean isSorted(int a[]) { for (int i = 0 ;i < a.length - 1; i++) { if (a[i + 1] < a[i]) { return false; } } return true; } /** * 归并排序,时间复杂度是o(nlogn),是稳定的 * @param a */ public void mergeSort(int a[]) { mergeSort(a,0,a.length - 1); } public void mergeSort(int a[],int lo,int hi) { if (lo >= hi) { return; } int mid = (lo + hi) >> 1; mergeSort(a, lo, mid); mergeSort(a, mid + 1, hi); //merge的过程 int temp[] = new int[hi - lo + 1];//用于记录更新的值 for (int i = lo,j = mid + 1, k = 0; k < temp.length;) { if (i > mid) { temp[k++] = a[j++]; } else if (j > hi) { temp[k++] = a[i++]; } else if(a[i] < a[j]) { temp[k++] = a[i++]; } else { temp[k++] = a[j++]; } } //替换我更新的值 for (int i = 0; i < temp.length; i++) { a[i + lo] = temp[i]; } } /** * 平均复杂度是o(nlogn)最差是o(n^2),不稳定 * @param a */ public void quickSort(int a[]) { quickSort(a,0, a.length - 1); } public void quickSort(int a[],int lo,int hi) { if (lo < hi) { int pivot = partion(a,lo,hi); quickSort(a,lo,pivot - 1); quickSort(a,pivot + 1,hi); } } //partion的一种方法 /* public int partion(int a[],int lo,int hi) { int pvoit = a[hi]; int begin = lo - 1; for (int i = lo; i < hi;i++) { if (a[i] < pvoit) { begin++; if (begin != i) { swap(a, begin, i); } } } begin++; swap(a,begin,hi); return begin; } */ //partion的另一种方法 public int partion(int a[],int lo,int hi) { int pivot = a[hi]; while (lo < hi) { while (lo < hi && a[lo] < pivot) lo++; while (lo < hi && a[hi] > pivot) hi--; if (lo < hi) swap(a,lo,hi); } return lo; } //堆排序的精髓 public void sink(int a[],int k,int N) { while (2 * k <= N) { int j = 2 * k;//孩子结点(2k,2k+1)左右孩子 //找到最大的孩子 if ((j + 1) <= N && less(a,j,j + 1)) { j++; } if (a[k - 1] > a[j - 1]) {//说明已经是堆了 break; } exch(a,k,j); k = j; } } public boolean less(int a[],int x,int y) { if (a[x - 1] < a[y - 1])//注意我们的index是1,但是数组下标是0 return true; else return false; } public void exch(int a[],int x,int y) { int t = a[x - 1]; a[x - 1] = a[y - 1]; a[y - 1] = t; } /** * 堆排序,时间复杂度(nlogn) * @param a */ public void heapSort(int a[]) { int N = a.length; //构建堆 for (int k = N / 2; k >= 1; k--) { sink(a,k,N); } for (int i = 1;i < N;) { exch(a,1,N--);//每次都和根结点交换位置 sink(a,1,N);//重新构建堆 } } //正常情况 public void test() { int a[] = {2,3,1,5,4};// bubble(a);// selection(a);// insertion(a);// binaryInsertion(a);// mergeSort(a);// quickSort(a); heapSort(a); System.out.println(isSorted(a)); System.out.println(Arrays.toString(a)); } //就是递增的 public void test1() { int a[] = {1,2,3,4,5,6};// bubble(a);// selection(a);// insertion(a);// binaryInsertion(a);// mergeSort(a);// quickSort(a); heapSort(a); System.out.println(isSorted(a)); System.out.println(Arrays.toString(a)); } //只有一个值 public void test2() { int a[] = {1};// bubble(a);// selection(a);// insertion(a);// binaryInsertion(a);// mergeSort(a);// quickSort(a); heapSort(a); System.out.println(isSorted(a)); System.out.println(Arrays.toString(a)); } //两个值 public void test3() { int a[] = {1,2};// bubble(a);// selection(a);// insertion(a);// binaryInsertion(a);// mergeSort(a);// quickSort(a); heapSort(a); System.out.println(isSorted(a)); System.out.println(Arrays.toString(a)); } /* public void testBinary1() { int a [] = {1,2,3,4,5}; int index = binnarySearch(a,5); System.out.println(index); } public void testBinary2() { int a [] = {1}; int index = binnarySearch(a,1); System.out.println(index); } */ public static void main(String[] args) { // TODO Auto-generated method stub Sorted s = new Sorted(); s.test(); s.test1(); s.test2(); s.test3();// s.testBinary1();// s.testBinary2(); }}
0 0
- java各大排序算法总结
- 各大排序算法总结
- 排序算法大总结
- 排序算法大总结
- 排序算法大总结
- 排序算法大总结
- 10大排序算法大总结--Java实现
- 各大排序算法的总结
- 各大排序算法自己总结
- 各大排序算法优缺点总结
- 【排序算法】:九大排序算法总结
- 九大排序算法总结
- 十大排序算法总结
- 九大排序算法总结
- 九大排序算法总结
- 九大排序算法总结
- 九大排序算法总结
- 九大排序算法总结
- ArrayAdapter,SimpleAdapter的基本使用
- Sicily 1001.Alphacode | 动态规划
- python堆排序
- 暑期社会实践心得
- java字符串分解 StringTokenizer用法
- java各大排序算法总结
- Vue.js——60分钟webpack项目模板快速入门【8】
- 二叉树的非递归遍历
- 《自控力》阅读笔记二
- Vue.js——vue-router 60分钟快速入门【9】
- openId导致的微信中“下单账号和支付账号不一致”原因及解决办法
- Codeforces Round #370 (Div. 2)
- Vert.x - Java 的新领土
- Aurora一些使用过程中碰到的问题