分享

七大排序算法(冒泡,选择,插入,二分法排序,希尔,快速,合并,堆排序)的java实现(14/8/3更新加入二分排序)

 醉人说梦 2020-04-13

冒泡排序

思路:就是每次将最大或最小的元素放到数组的最后,so easy!时间复杂度为(O(n^2))
  1. public class BubbleSort {
  2. public static void bubbleSort(int[] a) {
  3. for (int j = 1; j < a.length; j++) {
  4. for (int i = 0; i < a.length - j; i++) {
  5. if (a[i] > a[i + 1]) {
  6. int temp = a[i];
  7. a[i] = a[i + 1];
  8. a[i + 1] = temp;
  9. }
  10. }
  11. }
  12. }
  13. public static void main(String[] args) {
  14. int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,
  15. 3, 8, 31 };
  16. bubbleSort(a);
  17. for(int i = 0; i < a.length; i++){
  18. System.out.print(a[i]+" ");
  19. }
  20. }
  21. }

选择排序

思路:类似于冒泡,每次将后面最小的元素放在前面。时间复杂度为((O(n^2)))
  1. public class SelectSort {
  2. public static void selectSort(int[] a) {
  3. int min;
  4. for (int i = 0; i < a.length - 1; i++) {
  5. min = i;
  6. for (int j = min + 1; j < a.length; j++) {
  7. if (a[min] > a[j]) {
  8. int temp = a[min];
  9. a[min] = a[j];
  10. a[j] = temp;
  11. }
  12. }
  13. }
  14. }
  15. public static void main(String[] args) {
  16. int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,
  17. 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };
  18. selectSort(a);
  19. for (int i = 0; i < a.length; i++) {
  20. System.out.print(a[i] + " ");
  21. }
  22. }
  23. }

插入排序

思路:从第二个元素开始,和之前的已排好序的字数组的元素比较,找到合适的位置,然后后面的元素向后移动一位,再将该元素插入到前面合适的位置。时间复杂度为(O(n^2))
  1. public class InsertSort {
  2. public static void insertSort(int[] a) {
  3. for (int i = 1; i < a.length; i++) {
  4. int key = a[i];
  5. int j = i - 1;
  6. while (j >= 0 && a[j] > key) {
  7. a[j+1] = a[j];
  8. j--;
  9. }
  10. a[j+1] = key;
  11. }
  12. }
  13. public static void main(String[] args) {
  14. int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,
  15. 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };
  16. insertSort(a);
  17. for (int i = 0; i < a.length; i++) {
  18. System.out.print(a[i] + " ");
  19. }
  20. }
  21. }

二分法排序

思路:插入排序的多此一举,居然先用二分法查找插入位置(多此一举),然后再所有插入位置(二分法查出来的)后面的元素全部后移,太蛋疼了,插入法直接边找边后移多容易啊,这个事蛋疼的做法,下午想了一下做出来。(14、08、3)
  1. package sort;
  2. public class BinarySort {
  3. public static int binarySerch(int[] arr, int start, int end, int value) {
  4. int mid = -1;
  5. while (start <= end) {
  6. mid = (start + end) / 2;
  7. if (arr[mid] < value)
  8. start = mid + 1;
  9. else if (arr[mid] > value)
  10. end = mid - 1;
  11. else
  12. break;
  13. }
  14. if (arr[mid] < value)
  15. return mid + 1;
  16. else if (value < arr[mid])
  17. return mid;
  18. return mid + 1;
  19. }
  20. public static void binarySort(int[] arr, int start, int end) {
  21. for (int i = start + 1; i <= end; i++) {
  22. int value = arr[i];
  23. int insertLoc = binarySerch(arr, start, i - 1, value) ;
  24. for (int j = i; j > insertLoc; j--) {
  25. arr[j] = arr[j - 1];
  26. }
  27. arr[insertLoc] = value;
  28. }
  29. }
  30. public static void main(String[] args) {
  31. int[] arr = { 3, 5, 0, 8, 7, 9, 1, 2, 6, 4 };
  32. binarySort(arr, 0, arr.length - 1);
  33. for (int i = 0; i < arr.length; i++) {
  34. System.out.print(arr[i] + " ");
  35. }
  36. }
  37. }


希尔排序

思路:类似于插入排序,只是每次所取的步长为(数组的长度 /  2 / i)。时间复杂度为(n*log n)。
  1. public class ShellSort {
  2. public static void shellSort(int[] a) {
  3. for (int gap = a.length / 2; gap > 0; gap /= 2)
  4. for (int i = gap; i < a.length; i++) {
  5. int key = a[i];
  6. int j = i - gap;
  7. while (j >= 0 && a[j] > key) {
  8. a[j + gap] = a[j];
  9. j -= gap;
  10. }
  11. a[j + gap] = key;
  12. }
  13. }
  14. public static void main(String[] args) {
  15. int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,
  16. 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };
  17. shellSort(a);
  18. for (int i = 0; i < a.length; i++) {
  19. System.out.print(a[i] + " ");
  20. }
  21. }
  22. }

快速排序

思路:关键在于求出partition()函数。我给出了两种方法:1、从前到后。2、从前到中间,从后到中间。时间复杂度为(n * log n)最坏情况为
OK!show your my codes!
  1. public class QuickSort {
  2. /*public static int partition(int[] a, int p, int r) {
  3. int x = a[r];
  4. int i = p - 1;
  5. for (int j = p; j < r; j++) {
  6. if (a[j] <= x) {//如果a[j]<x就将后面a[i]后面a[i+1](值大于x)与a[j](值<a[j])进行交换
  7. i++;
  8. int temp = a[i];
  9. a[i] = a[j];
  10. a[j] = temp;
  11. }
  12. }
  13. i++;
  14. int temp = a[i];
  15. a[i] = a[r];
  16. a[r] = temp;
  17. return i;
  18. }*/
  19. public static int partition(int a[], int p, int r) {
  20. int x = a[p];
  21. int i = p;
  22. int j = r ;
  23. while (i < j) {
  24. while (a[j] >= x && i<j)
  25. j--;
  26. a[i] = a[j];//把小于X的那个数拿到前面的a【i】中
  27. while (a[i] <= x && i<j)
  28. i++;
  29. a[j] = a[i];//把大于X的那个数拿到后面的a【j】中
  30. }
  31. a[j] = x;//将X拿到分割处
  32. return j;
  33. }
  34. public static void quickSort(int[] a, int p, int r) {
  35. if (p < r) {
  36. int q = partition(a, p, r);
  37. quickSort(a, p, q-1);
  38. quickSort(a, q + 1, r);
  39. }
  40. }
  41. public static void main(String[] args) {
  42. int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,
  43. 3, 8, 31 };
  44. quickSort(a, 0, a.length-1);
  45. for (int i = 0; i < a.length; i++) {
  46. System.out.print(a[i] + " ");
  47. }
  48. }
  49. }

合并排序

思路:用分治的思想,将数组分至最小,再合并即可,不懂自己google吧!时间复杂度为(n * log n )是一个稳定的排序算法。
  1. public class MergeSort {
  2. public static void merge(int A[], int p, int q, int r) {
  3. int[] L = new int[q - p + 1];
  4. int[] R = new int[r - q];
  5. for (int i = p, j = 0; i <= q; i++, j++) {
  6. L[j] = A[i];
  7. }
  8. for (int i = q + 1, j = 0; i <= r; i++, j++) {
  9. R[j] = A[i];
  10. }
  11. int pos = p;
  12. int i = 0, j = 0;
  13. for (; i < L.length && j < R.length;) {
  14. if (L[i] <= R[j]) {
  15. A[pos++] = L[i++];
  16. } else {
  17. A[pos++] = R[j++];
  18. }
  19. }
  20. if (i < L.length) {
  21. for (; i < L.length;)
  22. A[pos++] = L[i++];
  23. } else if (j < R.length) {
  24. for (; j < R.length;)
  25. A[pos++] = R[j++];
  26. }
  27. }
  28. public static void mergeSort(int[] A, int p, int r) {
  29. if (p < r) {
  30. int q = (p + r) / 2;
  31. mergeSort(A, p, q);
  32. mergeSort(A, q + 1, r);
  33. merge(A, p, q, r);
  34. }
  35. }
  36. public static void main(String[] args) {
  37. int A[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,
  38. 3, 8, 31 };
  39. mergeSort(A, 0, A.length - 1);
  40. for (int i = 0; i < A.length; i++) {
  41. System.out.print(A[i] + " ");
  42. }
  43. }
  44. }

堆排序

思路:建立一个堆,大顶堆或者小顶堆都可以。每次将堆顶元素放到数组最后,然后堆的规模减小一个,再维持第一个元素的大顶堆性质。时间复杂度为(n * log n)。
  1. public class HeapSort {
  2. //求双亲位置
  3. static int parent(int i) {
  4. return i / 2;
  5. }
  6. //求左孩子位置
  7. static int left(int i) {
  8. return 2 * i;
  9. }
  10. //求右孩子位置
  11. static int right(int i) {
  12. return 2 * i + 1;
  13. }
  14. //维持大顶堆的性质
  15. static void maxHelpify(int[] A, int i) {
  16. int l = left(i);
  17. int r = right(i);
  18. int largest = 0;
  19. if (l <= A[0] && A[l] > A[i])
  20. largest = l;
  21. else
  22. largest = i;
  23. if (r <= A[0] && A[r] > A[largest])
  24. largest = r;
  25. if (largest != i) {
  26. int temp = A[largest];
  27. A[largest] = A[i];
  28. A[i] = temp;
  29. maxHelpify(A, largest);
  30. }
  31. }
  32. //建立大顶堆
  33. static void buildMaxHeap(int[] A){
  34. for(int i=(A.length-1)/2; i>0; i--){
  35. maxHelpify(A, i);
  36. }
  37. }
  38. //堆排序
  39. public static void heapSort(int[] A){
  40. buildMaxHeap(A);//建立大顶堆
  41. //每次把堆顶的数放到数组最后,然后把堆的大小减1,再次维持第一个数的大顶堆性质
  42. for(int i = A.length - 1; i>=2; i--){
  43. int temp = A[1];
  44. A[1] = A[i];
  45. A[i] = temp;
  46. A[0]--;
  47. maxHelpify(A, 1);
  48. }
  49. }
  50. public static void main(String[] args) {
  51. int A[] = new int[3];
  52. A[0] = A.length-1;//a[0]存放堆的大小
  53. for(int i = 1; i < A.length; i++){
  54. A[i] = (int) (Math.random()*10);
  55. }
  56. heapSort(A);
  57. for (int i = 1; i < A.length; i++) {
  58. System.out.print(A[i] + " ");
  59. }
  60. }
  61. }


其他:还有一种计数排序。

比较简单:就是将数组下标作为元素的value,特殊情况下使用。如排序N个人的年龄就可以用计数排序。将年龄看作数组的下标,定义一个大小为100的数组,将年龄与之比较,如果年龄==下标就将,该下标的value+1.时间复杂度为(N)。





















    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多