排序算法总结

xiaoxiao2021-02-27  152

public class SortTest {     //打印数组     public void print(int[] array) {    for(int i=0;i<array.length;i++) {    System.out.print(array[i]+" "); } System.out.println(); } //以下两种方式为交换排序,第一种为冒泡排序,第二种为快速排序 //冒泡排序 public int[] bubbleSort(int[] array) {    for(int i=0;i<array.length-1;i++) {    for(int j=0;j<array.length-1-i;j++) {    if(array[j]>array[j+1]) {    int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } return array; } //快速排序,挖坑填数 private int getMiddle(int[] array,int low,int high) { int key = array[low]; while(low<high) { while(low<high && array[high]>key) { high--; } array[low] = array[high]; while(low<high && array[low]<key) { low++; } array[high] = array[low]; } array[low] = key; return low;   } public int[] quikeSort(int[] array,int low,int high) { //如果不加这个判断,递归会无法退出 if(low<high) { int mid = this.getMiddle(array,low,high); quikeSort(array,0,mid-1); quikeSort(array,mid+1,high); } return array; }      //以下两种方式为插入排序,一种是直接插入排序,另外一种是希尔排序 //直接插入排序 public int[] insertSort(int[] array) { for(int i=1;i<array.length;i++) { if(array[i]<array[i-1]) { int j; int temp = array[i]; array[i] = array[i-1]; for(j=i-1;j>=0 && array[j]>temp;j--) { array[j+1] = array[j];  } array[j] = temp; } } return array; } //希尔排序 private void shellInsertSort(int[] array,int dk) { for(int i=dk;i<array.length;i++) { if(array[i]<array[i]) { int j; int temp = array[i]; array[i] = array[i-dk]; for(j=i-dk;j>=0&&array[j]>temp;j-=dk) { array[j+dk] = array[j]; } array[j] = temp; } } } public int[] shellSort(int[] array) { int dk = array.length/2; while(dk>=1) { shellInsertSort(array,dk); dk = dk/2; } return array; } //以下两种方式为选择排序,一种是简单的选择排序,另一种为堆排序 //简单选择排序 public int[] selectSort(int[] array) { for(int i=0;i<array.length;i++) { for(int j=i+1;j<array.length;j++) { if(array[j]<array[i]) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } } return array; } //堆排序(看完二叉树之后补齐) //以下一种方式是归并排序 //归并排序 //将两个有序序列合并array[first]~array[mid],array[mid+1]~array[last] public void mergeArray(int[] a,int first,int mid,int last) {         int[] temp = new int[last-first+1]; int k = 0; int i = first; int j = mid+1; while(i<=mid && j<=last) { if(a[i]<=a[j]) { temp[k++] = a[i++]; } else { temp[k++] = a[j++]; } } while(i<=mid) { temp[k++] = a[i++]; } while(j<=last) { temp[k++] = a[j++]; } for(int n=0;n<last-first+1;n++) { a[first+n] = temp[n]; } } public int[] mergeSort(int[] array,int first,int last) { int mid = (first+last)/2; if(first<last) { mergeSort(array,first,mid); mergeSort(array,mid+1,last); mergeArray(array,first,mid,last); } return array; } //主函数 public static void main(String[] args) { int[] array = {9,8,7,6,5,4,3,2,1,0}; SortTest st = new SortTest(); int[] a = st.bubbleSort(array); int[] b = st.quikeSort(array,0,array.length-1); int[] c = st.insertSort(array); int[] d = st.shellSort(array); int[] e = st.selectSort(array); int[] f = st.mergeSort(array,0,array.length-1); st.print(a); st.print(b); st.print(c); st.print(d); st.print(e); st.print(f); } }
转载请注明原文地址: https://www.6miu.com/read-15976.html

最新回复(0)