7種基本排序演算法--java實現
阿新 • • 發佈:2019-02-13
7種基本排序演算法有:
直接插入排序、希爾排序;直接選擇排序、堆排序;氣泡排序、快速排序;歸併排序。實現如下:
import java.util.Arrays; public class SortAlogrithm { public static void main(String[] args) { int[] array1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] array2 = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; int[] array3 = {1, 3, 4, 2, 0, 11, 1, 15, 7, 6, 5, 13, 18, 20, 12}; int[] array4 = {1, 3, 4, 2, 30, 11, 1, 15, 6, 5, 13, 18, 20, 12}; mergeSort(array1, 0, array1.length-1); mergeSort(array2, 0, array2.length-1); mergeSort(array3, 0, array3.length-1); mergeSort(array4, 0, array4.length-1); // quickSort(array1, 0, array1.length-1); // quickSort(array2, 0, array2.length-1); // quickSort(array3, 0, array3.length-1); // quickSort(array4, 0, array4.length-1); // buddleSort(array1); // buddleSort(array2); // buddleSort(array3); // buddleSort(array4); print(array1); print(array2); print(array3); print(array4); } public static void print(int[] inputs){ System.out.println(Arrays.toString(inputs)); } //直接插入排序 public static void insertSort(int[] inputs){ int size = inputs.length; for(int i=1; i<size; i++){ int temp = inputs[i]; int j=i-1; for(; j>=0&&(inputs[j]>temp); j--){ inputs[j+1] = inputs[j]; } inputs[j+1] = temp; } } //希爾排序 public static void shellSort(int[] inputs){ int size = inputs.length; int d=1; while(d < size){ d = 3*d+1; } d = (d-1)/3; while(d > 0){ for(int i=d; i<size; i++){ int temp = inputs[i]; int j = i-d; for(; j>=0&&(inputs[j]>temp); j-=d){ inputs[j+d] = inputs[j]; } inputs[j+d] = temp; } d = (d-1)/3; } } //直接選擇排序 public static void selectSort(int[] inputs){ int size = inputs.length; for(int i=0; i<size; i++){ int temp = inputs[i]; int min = temp; int minIndex = i; for(int j=i+1; j<size; j++){ if(min > inputs[j]){ min = inputs[j]; minIndex = j; } } if(minIndex != i){ inputs[i] = inputs[minIndex]; inputs[minIndex] = temp; } } } //堆排序 public static void heapSort(int[] inputs){ int size = inputs.length; for(int i=size; i>0; i--){ heapSortBuildHeap(inputs, i); heapSortSwap(inputs, i-1); } } private static void heapSortBuildHeap(int[] inputs, int end){ int size = end; int start = size/2 - 1; for(int i=start; i>=0; i--){ if(2*i+2<size){ if(inputs[2*i+1] > inputs[2*i+2]){ if(inputs[2*i+1] > inputs[i]){ int temp = inputs[2*i+1]; inputs[2*i+1] = inputs[i]; inputs[i] = temp; } }else{ if(inputs[2*i+2] > inputs[i]){ int temp = inputs[2*i+2]; inputs[2*i+2] = inputs[i]; inputs[i] = temp; } } }else{ if(inputs[2*i+1] > inputs[i]){ int temp = inputs[2*i+1]; inputs[2*i+1] = inputs[i]; inputs[i] = temp; } } } } private static void heapSortSwap(int[] inputs, int end){ int temp = inputs[end]; inputs[end] = inputs[0]; inputs[0] = temp; } //氣泡排序 public static void buddleSort(int[] inputs){ int size = inputs.length; for(int i=0; i<size; i++){ for(int j=1; j<size-i; j++){ if(inputs[j] < inputs[j-1]){ int temp = inputs[j]; inputs[j] = inputs[j-1]; inputs[j-1] = temp; } } } } //快速排序 public static void quickSort(int[] inputs, int left, int right){ if(left >= right) return; int base = inputs[left]; int baseIndex = left; int end = right; while(left < right){ while(right>left && inputs[right]>=base){ right--; } inputs[left] = inputs[right]; while(left<right && inputs[left]<=base){ left++; } inputs[right] = inputs[left]; } inputs[right] = base; quickSort(inputs, baseIndex, right-1); quickSort(inputs, right+1, end); } //歸併排序 public static void mergeSort(int[] inputs, int low, int high){ if(low >= high){ return; } int mid = (low+high)/2; mergeSort(inputs, low, mid); mergeSort(inputs, mid+1, high); merge(inputs, low, mid, high); } private static void merge(int[] inputs, int low, int mid, int high){ int[] temp = new int[high-low+1]; int i = low; int j = mid + 1; int k = 0; while(i<=mid && j<=high){ if(inputs[i] <= inputs[j]){ temp[k] = inputs[i]; i++; }else{ temp[k] = inputs[j]; j++; } k++; } while(i <= mid){ temp[k] = inputs[i]; k++; i++; } while(j <= high){ temp[k] = inputs[j]; k++; j++; } for(int m=0; m<temp.length; m++){ inputs[low+m] = temp[m]; } } }