DevOps 九大经典排序算法归纳及JAVA 的实现｜Jooop's blog

aqing · November 10, 2019 · 8 hits

JAVA 实现

直接插入排序

 ```import java.util.; public class { public int[] insertionSort(int[] arr) { if(arr==null||arr.length==0) return null; int k; int temp; for(int i=1;i=0&&temp

希尔排序

 ```import java.util.; public class ShellSort { public int[] shellSort(int[] arr) { int gap; for (gap = arr.length/2; gap>0; gap = gap/2){ for (int i=gap; i=0 && temp

冒泡排序

 ```import java.util.; public class BubbleSort { public int[] bubbleSort(int[] arr) { if(arr==null||arr.length==0) return null; int temp = arr[0]; for(int i=0;iarr[j]){ temp=arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; }}```

快速排序

 ```import java.util.; public class QuickSort { public int[] quickSort(int[] arr) { if(arr==null) return null; sort(arr,0,arr.length-1); return arr; } public void sort(int []arr,int begin,int end){ if(begin=middle&&begin

简单选择排序

 ```import java.util.; public class SelectionSort { public int[] selectionSort(int[] arr) { if(arr==null||arr.length==0) return null; int number = 0; for(int i=0;iarr[j]){ number = j; } } int temp = arr[number]; arr[number] = arr[i]; arr[i] = temp; } return arr; }}```

堆排序

 ```import java.util.; public class HeapSort { public int[] heapSort(int[] arr) { if(arr==null || arr.length==0) return arr; buileHeap(arr); int temp = 0; for(int i=arr.length-1; i>0; i--){ temp = arr[i]; arr[i] = arr[0]; arr[0] = temp; adjustHeap(arr,0,i); } return arr; } public void buileHeap(int []arr){ for(int i=arr.length/2-1; i>=0; i--){ adjustHeap(arr,i,arr.length); } } public void adjustHeap(int []arr,int index,int length){ int max; while(index*2+1 < length){ max = index*2+1; if(index*2+2 < length){ if(arr[index*2+2] > arr[max]) max = index*2+2; } if(arr[max] <= arr[index]){ break; }else{ int temp = arr[max]; arr[max] = arr[index]; arr[index] = temp; index = max; } } }}```

归并排序

 ```import java.util.; public class MergeSort { public int[] mergeSort(int[] arr) { if(arr==null||arr.length==0) return arr; sort(arr,0,arr.length-1); return arr; } private void sort(int []arr,int left,int right){ if(left

计数排序

 ```import java.util.; public class CountingSort { public int[] countingSort(int[] arr) { if(arr==null || arr.length==0) return arr; int max = arr[0]; int min = arr[0]; for(int i=1; i0){ arr[index++] = min+i; } } return arr; }}```

基数排序

 ```import java.util.; public class RadixSort { public int[] radixSort(int[] arr) { if(arr==null || arr.length==0) return arr; int [][]bucket =new int[10][arr.length]; int []len = new int[10]; int p = 0; for(int i=1; i<1000; i=i10){ for(int j=0; j

No Reply at the moment.
You need to before reply, if you don't have an account, please first.