排序算法汇总
冒泡排序
思路 :俩俩交换,大的放在后面,第一次排序后最大值已在数组末尾。需要n-1次排序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class BubbleSort { public void bubbleSort (int [] array) { int temp; int isChange; int count=0 ; for (int i=0 ;i<array.length-1 ;i++){ isChange=0 ; for (int j=0 ;j<array.length-i-1 ;j++){ if (array[j]>array[j+1 ]){ temp=array[j]; array[j]=array[j+1 ]; array[j+1 ]=temp; isChange=1 ; } } if (isChange==0 ){ break ; } count++; } for (int c:array) System.out.print(c+" " ); } public static void main (String[] args) { int [] array={2 ,6 ,1 ,9 ,3 ,5 }; BubbleSort bubbleSort=new BubbleSort(); bubbleSort.bubbleSort(array); } }
选择排序
思路 :找到数组中最大的元素,与数组最后一位元素交换。当只有一个数时,则不需要选择了,因此需要n-1趟排序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class SelectSort { public void selectSort (int [] array) { int pos; for (int i=0 ;i<array.length-1 ;i++){ pos=0 ; for (int j=0 ;j<array.length-i;j++){ if (array[j]>array[pos]){ pos=j; } } int temp=array[pos]; array[pos]=array[array.length-i-1 ]; array[array.length-i-1 ]=temp; } for (int c:array) System.out.print(c+" " ); } public static void main (String[] args) { int [] array={1 ,3 ,7 ,4 ,2 }; SelectSort selectSort=new SelectSort(); selectSort.selectSort(array); } }
插入排序
思路 :将一个元素插入到已有序的数组中,在初始时未知是否存在有序的数据,因此将元素第一个元素看成是有序的 。与有序的数组进行比较,比它大则直接放入,比它小则移动数组元素的位置,找到个合适的位置插入 。当只有一个数时,则不需要插入了,因此需要n-1趟排序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class InsertSort { public void insertSort (int [] array) { for (int i=1 ;i<array.length;i++){ int temp=array[i]; int j=i-1 ; while (j>=0 &&array[j]>temp){ array[j+1 ]=array[j]; j--; } array[j+1 ]=temp; } for (int c:array) System.out.print(c+" " ); } public static void main (String[] args) { int [] array={3 ,6 ,2 ,8 ,7 }; InsertSort insertSort=new InsertSort(); insertSort.insertSort(array); } }
快速排序
思路 :在数组中找一个元素(节点),比它小的放在节点的左边,比它大的放在节点右边。一趟下来,比节点小的在左边,比节点大的在右边。不断执行这个操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 public class QuickSort { public void quickSort (int [] array,int L,int R) { int i=L; int j=R; int pivot=array[(L+R)/2 ]; while (i<j){ while (pivot<array[j]){ j--; } while (pivot>array[i]){ i++; } if (i<=j){ int temp=array[i]; array[i]=array[j]; array[j]=temp; i++; j--; } } if (L<j){ quickSort(array,L,j); } if (R>i){ quickSort(array,i,R); } } public static void main (String[] args) { int [] array={4 ,2 ,6 ,5 ,9 }; QuickSort quickSort=new QuickSort(); quickSort.quickSort(array,0 ,array.length-1 ); for (int c:array) System.out.print(c+" " ); } }
归并排序
思路 :将两个 已排好序 的数组合并 成一个有序 的数组,称之为归并排序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public class MergeSort { public static void mergeSort (int [] array,int L,int R) { if (L==R) return ; else { int M=(L+R)/2 ; mergeSort(array,L,M); mergeSort(array,M+1 ,R); merge(array,L,M+1 ,R); } } public static void merge (int [] array,int L,int M,int R) { int [] leftArray=new int [M-L]; int [] rightArray=new int [R-M+1 ]; for (int i=L;i<M;i++){ leftArray[i-L]=array[i]; } for (int i=M;i<=R;i++){ rightArray[i-M]=array[i]; } int i=0 ,j=0 ,k=L; while (i<leftArray.length&&j<rightArray.length){ if (leftArray[i]<rightArray[j]){ array[k]=leftArray[i]; k++; i++; }else { array[k]=rightArray[j]; k++; j++; } } while (i<leftArray.length){ array[k]=leftArray[i]; k++; i++; } while (j<rightArray.length){ array[k]=rightArray[j]; k++; j++; } } public static void main (String[] args) { int [] array={3 ,1 ,7 ,5 ,4 }; MergeSort ms=new MergeSort(); ms.mergeSort(array,0 ,array.length-1 ); for (int c:array) System.out.print(c+" " ); } }
堆排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public class HeapSort { public static void main (String[] args) { int [] arrays={2 ,8 ,4 ,6 ,9 }; maxheapify(arrays,arrays.length-1 ); int size=arrays.length-1 ; for (int i=0 ;i<arrays.length;i++){ int temp=arrays[0 ]; arrays[0 ]=arrays[arrays.length-1 -i]; arrays[arrays.length-1 -i]=temp; heapify(arrays,0 ,size); size--; } for (int c:arrays) System.out.print(c+" " ); } public static void heapify (int [] arrays,int currentrootnode,int size) { if (currentrootnode<size){ int left=currentrootnode*2 +1 ; int right=currentrootnode*2 +2 ; int max=currentrootnode; if (left<size){ if (arrays[max]<arrays[left]){ max=left; } } if (right<size){ if (arrays[max]<arrays[right]){ max=right; } } if (max!=currentrootnode){ int temp=arrays[max]; arrays[max]=arrays[currentrootnode]; arrays[currentrootnode]=temp; heapify(arrays,max,size); } } } public static void maxheapify (int [] arrays,int size) { for (int i=size-1 ;i>=0 ;i--){ heapify(arrays,i,size); } } }