排序算法
来源:互联网 发布:js给集合添加元素 编辑:程序博客网 时间:2024/06/06 10:21
几种常见的排序算法
要求:随机生成100个数,构成一个数组,用不同的排序方法对该数组进行排序。(用Java写的)
完整代码:
package 常见排序算法;public class Test {public static void main(String[] args) {int n = 10;int[] a = new int[n];for(int i=0;i<n;i++) {a[i] = (int)(Math.random() * 100) + 1;}System.out.println("Befor sort:");show(a);InsertSort(a);System.out.println("\nAfter InsertSort:");show(a);BiInsertSort(a);System.out.println("\nAfter BiInsertSort:");show(a);ShellSort(a);System.out.println("\nAfter ShellSort:");show(a);BubbleSort(a);System.out.println("\nAfter BubbleSort:");show(a);SelectSort(a);System.out.println("\nAfter SelectSort:");show(a);MergeSort(a,0,a.length-1); System.out.println("\nAfter MergeSort:"); show(a); QKSort(a,0,a.length-1);System.out.println("\nAfter QuickSort:");show(a);} public static void show(int[] arr) {for(int i=0;i<arr.length;i++) {System.out.print(arr[i] + " ");}}//直接插入排序public static void InsertSort(int[] a) {for(int i = 1;i<=a.length-1;i++) {if(a[i] < a[i-1]) {int temp = a[i];int j;for(j=i-1;j >=0 && temp < a[j];j--) a[j+1] = a[j];a[j+1] = temp;}}}//折半插入排序public static void BiInsertSort(int[] a) {for(int i = 1;i<=a.length-1;i++) {if(a[i] < a[i-1]) {int temp = a[i];int low = 0,high = i-1;while(low <= high) {int mid = (low+high)/2;if(a[i] < a[mid]) high = mid -1;else low = mid + 1;}for(int j=i-1;j>=low;j--) a[j+1] = a[j];a[low] = temp;}}}//希尔排序public static void ShellSort(int[] arr) {for(int d=arr.length/2;d > 0;d /= 2) {for(int i=d;i<arr.length;i++) {int temp = arr[i];int j = 0;for(j = i-d;j >= 0 && temp < arr[j];j -= d) {arr[j+d] = arr[j];}arr[j+d] = temp;}}}//冒泡排序public static void BubbleSort(int[] arr) {int flag = 1;for(int i=1;i<=arr.length-1 && flag == 1;i++) {flag = 0;for(int j=0;j<arr.length-i;j++) {if(arr[j] > arr[j+1]) {int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;flag = 1;}}}}//快速排序public static int QKpass(int[] arr,int low,int high) {int temp = arr[low];while(low < high) {if(temp < arr[high]) --high;arr[low] = arr[high];if(temp > arr[low]) ++low;arr[high] = arr[low];}arr[low] = temp;return low;}public static void QKSort(int[] arr,int low,int high) {if(low < high) {int pos = QKpass(arr,low,high);QKSort(arr,low,pos-1);QKSort(arr,pos+1,high);}}//选择排序public static void SelectSort(int[] arr) {for(int i=0;i<arr.length-1;i++) {int k = i;for(int j=i;j<arr.length-1;j++) {if(arr[j] < arr[k]) {k = j;}}if(k != i) {int temp = arr[i];arr[i] = arr[k];arr[k] = temp;}}}//归并排序public static void MergeSort(int[] arr,int left,int right) {int mid = 0;if(left < right) {mid = (left + right)/2;MergeSort(arr,left,mid);MergeSort(arr,mid+1,right);merge(arr,left,right,mid);}}public static void merge(int[] arr, int left, int right, int mid) {int[] temp = new int[arr.length];int p1 = left;int p2 = mid+1;int k = left;while(left <= mid && p2 <= right ) {if(arr[left] < arr[p2]) {temp[k++] = arr[left++];} else {temp[k++] = arr[p2++];}}while(left <= mid) {temp[k++] = arr[left++];}while(p2 <= right) {temp[k++] = arr[p2++];}while(p1 <= right) {arr[p1] = temp[p1++];}}}
1 0
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 设计一个具有GetMin功能的栈
- Redis实现高并发下的抢购、秒杀功能
- 素数打表(小模板)
- 【ES6】16.generator函数的语法 10.(1)异步操作的同步化表达
- 3天速成混合app开发_01环境部署
- 排序算法
- ZOJ3785-What day is that day?
- EventBus使用详解
- matlab kmeans 代码
- Node.js安装及环境配置之Windows篇
- .gitignore 的使用
- Hexo搭建github博客绑定顶级域名和www子域名
- JAVA OOP 第四章 接口
- Linux下安装mysql