歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java中的各種排序算法

Java中的各種排序算法

日期:2017/3/1 10:14:46   编辑:Linux編程

各種排序算法:

冒泡排序,選擇排序,插入排序,稀爾排序,快速排序,歸並排序,堆排序,桶式排序,基數排序

一、冒泡排序(Bubble Sort)
1. 基本思想:
  兩兩比較待排序數據元素的大小,發現兩個數據元素的次序相反時即進行交換,直到沒有反序的數據元素為止。
2. 排序過程:
  設想被排序的數組R[1..N]垂直豎立,將每個數據元素看作有重量的氣泡,根據輕氣泡不能在重氣泡之下的原則,從下往上掃描數組R,凡掃描到違反本原則的輕氣泡,就使其向上"漂浮",如此反復進行,直至最後任何兩個氣泡都是輕者在上,重者在下為止。

【示例】:

49 13 13 13 13 13 13 13
38 49 27 27 27 27 27 27
65 38 49 38 38 38 38 38
97 65 38 49 49 49 49 49
76 97 65 49 49 49 49 49
13 76 97 65 65 65 65 65
27 27 76 97 76 76 76 76
49 49 49 76 97 97 97 97

java代碼實現:

  1. /**
  2. * 冒泡排序:
  3. * 執行完一次內for循環後,最小的一個數放到了數組的最前面,相鄰位置之間交換
  4. * @author TSW
  5. *
  6. */
  7. public class BubbleSort {
  8. public staticvoid main(String[] args) {
  9. Integer[] intgArr = { 7, 2, 4, 3, 12,1, 9,6, 8,5, 11,10 };
  10. BubbleSort bubblesort = new BubbleSort();
  11. bubblesort.bubble(intgArr, 0, intgArr.length -1);
  12. for (Integer intObj : intgArr) {
  13. System.out.print(intObj + " ");
  14. }
  15. }
  16. /**
  17. * 排序算法的實現,對數組中指定的元素進行排序
  18. * @param array 待排序的數組
  19. * @param from 從哪裡開始排序
  20. * @param end 排到哪裡
  21. */
  22. public void bubble(Integer[] array,int from, int end) {
  23. // 需 array.length - 1 輪比較
  24. for (int k =1; k < end - from + 1; k++) {
  25. // 每輪循環中從最後一個元素開始向前起泡,直到i=k止,即i等於輪次止
  26. for (int i = end - from; i >= k; i--) {
  27. // 按照一種規則(後面元素不能小於前面元素)排序
  28. if ((array[i].compareTo(array[i -1])) < 0) {
  29. // 如果後面元素小於了(當然是大於還是小於要看比較器實現了)前面的元素,則前後交換
  30. swap(array, i, i - 1);
  31. }
  32. }
  33. }
  34. }
  35. /**
  36. * 交換數組中的兩個元素的位置
  37. * @param array 待交換的數組
  38. * @param i 第一個元素
  39. * @param j 第二個元素
  40. */
  41. public void swap(Integer[] array,int i, int j) {
  42. if (i != j) {// 只有不是同一位置時才需交換
  43. Integer tmp = array[i];
  44. array[i] = array[j];
  45. array[j] = tmp;
  46. }
  47. }
  48. }

  1. /**
  2. * 冒泡排序:
  3. * 執行完一次內for循環後,最小的一個數放到了數組的最前面,相鄰位置之間交換
  4. * @author TSW
  5. *
  6. */
  7. public class BubbleSort {
  8. public static void main(String[] args) {
  9. Integer[] intgArr = { 7, 2, 4, 3, 12, 1, 9, 6, 8, 5, 11, 10 };
  10. BubbleSort bubblesort = new BubbleSort();
  11. bubblesort.bubble(intgArr, 0, intgArr.length - 1);
  12. for (Integer intObj : intgArr) {
  13. System.out.print(intObj + " ");
  14. }
  15. }
  16. /**
  17. * 排序算法的實現,對數組中指定的元素進行排序
  18. * @param array 待排序的數組
  19. * @param from 從哪裡開始排序
  20. * @param end 排到哪裡
  21. */
  22. public void bubble(Integer[] array, int from, int end) {
  23. // 需 array.length - 1 輪比較
  24. for (int k = 1; k < end - from + 1; k++) {
  25. // 每輪循環中從最後一個元素開始向前起泡,直到i=k止,即i等於輪次止
  26. for (int i = end - from; i >= k; i--) {
  27. // 按照一種規則(後面元素不能小於前面元素)排序
  28. if ((array[i].compareTo(array[i - 1])) < 0) {
  29. // 如果後面元素小於了(當然是大於還是小於要看比較器實現了)前面的元素,則前後交換
  30. swap(array, i, i - 1);
  31. }
  32. }
  33. }
  34. }
  35. /**
  36. * 交換數組中的兩個元素的位置
  37. * @param array 待交換的數組
  38. * @param i 第一個元素
  39. * @param j 第二個元素
  40. */
  41. public void swap(Integer[] array, int i, int j) {
  42. if (i != j) {// 只有不是同一位置時才需交換
  43. Integer tmp = array[i];
  44. array[i] = array[j];
  45. array[j] = tmp;
  46. }
  47. }
  48. }

另外一種實現方式:

  1. /**
  2. * 冒泡排序:執行完一次內for循環後,最大的一個數放到了數組的最後面。相鄰位置之間交換
  3. */
  4. public class BubbleSort2 {
  5. public staticvoid main(String[] args) {
  6. int[] a = { 3,5, 9,4, 7,8, 6,1, 2 };
  7. BubbleSort2 bubble = new BubbleSort2();
  8. bubble.bubble(a);
  9. for (int num : a) {
  10. System.out.print(num + " ");
  11. }
  12. }
  13. public void bubble(int[] a) {
  14. for (int i = a.length -1; i > 0; i--) {
  15. for (int j =0; j < i; j++) {
  16. if (new Integer(a[j]).compareTo(new Integer(a[j +1])) > 0) {
  17. swap(a, j, j + 1);
  18. }
  19. }
  20. }
  21. }
  22. public void swap(int[] a,int x, int y) {
  23. int temp;
  24. temp = a[x];
  25. a[x] = a[y];
  26. a[y] = temp;
  27. }
  28. }

  1. /**
  2. * 冒泡排序:執行完一次內for循環後,最大的一個數放到了數組的最後面。相鄰位置之間交換
  3. */
  4. public class BubbleSort2 {
  5. public static void main(String[] args) {
  6. int[] a = { 3, 5, 9, 4, 7, 8, 6, 1, 2 };
  7. BubbleSort2 bubble = new BubbleSort2();
  8. bubble.bubble(a);
  9. for (int num : a) {
  10. System.out.print(num + " ");
  11. }
  12. }
  13. public void bubble(int[] a) {
  14. for (int i = a.length - 1; i > 0; i--) {
  15. for (int j = 0; j < i; j++) {
  16. if (new Integer(a[j]).compareTo(new Integer(a[j + 1])) > 0) {
  17. swap(a, j, j + 1);
  18. }
  19. }
  20. }
  21. }
  22. public void swap(int[] a, int x, int y) {
  23. int temp;
  24. temp = a[x];
  25. a[x] = a[y];
  26. a[y] = temp;
  27. }
  28. }
Copyright © Linux教程網 All Rights Reserved