伙伴云客服论坛»论坛 S区 S软件开发 查看内容

0 评论

0 收藏

分享

常用Java排序算法详解

一、选择排序(SelectSort)
根本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置停止交换;接着对不包括第一个记录以外的其他记录停止第二次比较,得到最小的记录并与第二个记录停止位置交换;反复该过程,直到停止比较的记录只要一个为止。
  1. public class SelectSort {
  2. public static void selectSort(int[] array) {
  3. int i;
  4. int j;
  5. int temp;
  6. int flag;
  7. for (i = 0; i < array.length; i++) {
  8. temp = array[i];
  9. flag = i;
  10. for (j = i + 1; j < array.length; j++) {
  11. if (array[j] < temp) {
  12.   temp = array[j];
  13.   flag = j;
  14. }
  15. }
  16. if (flag != i) {
  17. array[flag] = array[i];
  18. array[i] = temp;
  19. }
  20. }
  21. }
  22. public static void main(String[] args) {
  23. int[] a = { 5, 1, 9, 6, 7, 2, 8, 4, 3 };
  24. selectSort(a);
  25. for (int i = 0; i < a.length; i++) {
  26. System.out.print(a[i] + " ");
  27. }
  28. }
  29. }
复制代码
二、插入排序(InsertSort)
根本原理:对于给定的一组数据,初始时假设第一个记录自成一个有序序列,其余记录为无序序列。接着从第二个记录开端,依照记录的大小依次将当前处置的记录插入到其之前的有序序列中,直至最后一个记录插入到有序序列中为止。
  1. public class InsertSort {
  2. public static void insertSort(int[] a) {
  3. if (a != null) {
  4. for (int i = 1; i < a.length; i++) {
  5. int temp = a[i];
  6. int j = i;
  7. if (a[j - 1] > temp) {
  8.   while (j >= 1 && a[j - 1] > temp) {
  9.   a[j] = a[j - 1];
  10.   j--;
  11.   }
  12. }
  13. a[j] = temp;
  14. }
  15. }
  16. }
  17. public static void main(String[] args) {
  18. int[] a = { 5, 1, 7, 2, 8, 4, 3, 9, 6 };
  19. // int[] a =null;
  20. insertSort(a);
  21. for (int i = 0; i < a.length; i++) {
  22. System.out.print(a[i] + " ");
  23. }
  24. }
  25. }
复制代码
三、冒泡排序(BubbleSort)
根本原理:对于给定的n个记录,从第一个记录开端依次对相邻的两个记录停止比较,当前面的记录大于后面的记录时,交换位置,停止一轮比较和换位后,n个记录中的最大记录将位于第n位;然后对前(n-1)个记录停止第二轮比较;反复该过程直到停止比较的记录只剩下一个为止。
  1. public class BubbleSort {
  2. public static void bubbleSort(int array[]) {
  3. int temp = 0;
  4. int n = array.length;
  5. for (int i = n - 1; i >= 0; i--) {
  6. for (int j = 0; j < i; j++) {
  7. if (array[j] > array[j + 1]) {
  8.   temp = array[j];
  9.   array[j] = array[j + 1];
  10.   array[j + 1] = temp;
  11. }
  12. }
  13. }
  14. }
  15. public static void main(String[] args) {
  16. int a[] = { 45, 1, 21, 17, 69, 99, 32 };
  17. bubbleSort(a);
  18. for (int i = 0; i < a.length; i++) {
  19. System.out.print(a[i] + " ");
  20. }
  21. }
  22. }
复制代码
四、归并排序(MergeSort)
根本原理:利用递归与分治技术将数据序列划分成为越来越小的半子表,再对半子表排序,最后再用递归方法将排好序的半子表合并成为越来越大的有序序列。对于给定的一组记录(假设共有n个记录),首先将每两个相邻的长度为1的子序列停止归并,得到n/2(向上取整)个长度为2或1的有序子序列,再将其两两归并,反复执行此过程,直到得到一个有序序列。
  1. public class MergeSort {
  2. public static void merge(int array[], int p, int q, int r) {
  3. int i, j, k, n1, n2;
  4. n1 = q - p + 1;
  5. n2 = r - q;
  6. int[] L = new int[n1];
  7. int[] R = new int[n2];
  8. for (i = 0, k = p; i < n1; i++, k++)
  9. L[i] = array[k];
  10. for (i = 0, k = q + 1; i < n2; i++, k++)
  11. R[i] = array[k];
  12. for (k = p, i = 0, j = 0; i < n1 && j < n2; k++) {
  13. if (L[i] > R[j]) {
  14. array[k] = L[i];
  15. i++;
  16. } else {
  17. array[k] = R[j];
  18. j++;
  19. }
  20. }
  21. if (i < n1) {
  22. for (j = i; j < n1; j++, k++)
  23. array[k] = L[j];
  24. }
  25. if (j < n2) {
  26. for (i = j; i < n2; i++, k++) {
  27. array[k] = R[i];
  28. }
  29. }
  30. }
  31. public static void mergeSort(int array[], int p, int r) {
  32. if (p < r) {
  33. int q = (p + r) / 2;
  34. mergeSort(array, p, q);
  35. mergeSort(array, q + 1, r);
  36. merge(array, p, q, r);
  37. }
  38. }
  39. public static void main(String[] args) {
  40. int a[] = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
  41. mergeSort(a, 0, a.length - 1);
  42. for (int j = 0; j < a.length; j++) {
  43. System.out.print(a[j] + " ");
  44. }
  45. }
  46. }
复制代码
五、快速排序(QuickSort)
根本原理:对于一组给定的记录,通过一趟排序后,将原序列分为两部分,其中前一部分的所有记录均比后一部分的所有记录小,然后再依次对前后两部分的记录停止快速排序,递归该过程,直到序列中的所有记录均有序为止。
  1. public class QuickSort {
  2. public static void sort(int array[], int low, int high) {
  3. int i, j;
  4. int index;
  5. if (low >= high)
  6. return;
  7. i = low;
  8. j = high;
  9. index = array[i];
  10. while (i < j) {
  11. while (i < j && index <= array[j])
  12. j--;
  13. if (i < j)
  14. array[i++] = array[j];
  15. while (i < j && index > array[i])
  16. i++;
  17. if (i < j)
  18. array[j--] = array[i];
  19. }
  20. array[i] = index;
  21. sort(array, low, i - 1);
  22. sort(array, i + 1, high);
  23. }
  24. public static void quickSort(int array[]) {
  25. sort(array, 0, array.length - 1);
  26. }
  27. public static void main(String[] args) {
  28. int a[] = { 5, 8, 4, 6, 7, 1, 3, 9, 2 };
  29. quickSort(a);
  30. for (int i = 0; i < a.length; i++) {
  31. System.out.print(a[i] + " ");
  32. }
  33. }
  34. }
复制代码
六、希尔排序(ShellSort)
根本原理:先将待排序的数组元素分成多个子序列,使得每个子序列的元素个数相对减少,然后对各个子序列分别停止直接插入排序,待整个待排序序列"根本有序后",最后再对所有元素停止一次直接插入排序。
  1. public class ShellSort {
  2. public static void shellSort(int[] a) {
  3. int len = a.length;
  4. int i, j;
  5. int h;
  6. int temp;
  7. for (h = len / 2; h > 0; h = h / 2) {
  8. for (i = h; i < len; i++) {
  9. temp = a[i];
  10. for (j = i - h; j >= 0; j -= h) {
  11.   if (temp < a[j]) {
  12.   a[j + h] = a[j];
  13.   } else
  14.   break;
  15. }
  16. a[j + h] = temp;
  17. }
  18. }
  19. }
  20. public static void main(String[] args) {
  21. int a[] = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
  22. shellSort(a);
  23. for (int j = 0; j < a.length; j++) {
  24. System.out.print(a[j] + " ");
  25. }
  26. }
  27. }
复制代码
七、最小堆排序(MinHeapSort)
根本原理:对于给定的n个记录,初始时把这些记录看作一颗顺序存储的二叉树,然后将其调整为一个小顶堆,然后将堆的最后一个元素与堆顶元素停止交换后,堆的最后一个元素即为最小记录;接着讲前(n-1)个元素重新调整为一个小顶堆,再将堆顶元素与当前堆的最后一个元素停止交换后得到次小的记录,反复该过程直到调整的堆中只剩一个元素时为止,该元素即为最大记录,此时可得到一个有序序列。
  1. public class MinHeapSort {
  2. public static void adjustMinHeap(int[] a, int pos, int len) {
  3. int temp;
  4. int child;
  5. for (temp = a[pos]; 2 * pos + 1 <= len; pos = child) {
  6. child = 2 * pos + 1;
  7. if (child < len && a[child] > a[child + 1])
  8. child++;
  9. if (a[child] < temp)
  10. a[pos] = a[child];
  11. else
  12. break;
  13. }
  14. a[pos] = temp;
  15. }
  16. public static void myMinHeapSort(int[] array) {
  17. int i;
  18. int len = array.length;
  19. for (i = len / 2 - 1; i >= 0; i--) {
  20. adjustMinHeap(array, i, len - 1);
  21. }
  22. for (i = len - 1; i >= 0; i--) {
  23. int tmp = array[0];
  24. array[0] = array[i];
  25. array[i] = tmp;
  26. adjustMinHeap(array, 0, i - 1);
  27. }
  28. }
  29. public static void main(String[] args) {
  30. int[] a = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
  31. myMinHeapSort(a);
  32. for (int i = 0; i < a.length; i++) {
  33. System.out.print(a[i] + " ");
  34. }
  35. }
  36. }
复制代码
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的协助,同时也希望多多支持网站!

回复

举报 使用道具

全部回复
暂无回帖,快来参与回复吧
本版积分规则 高级模式
B Color Image Link Quote Code Smilies

零代码研究员
注册会员
主题 23
回复 24
粉丝 0
|网站地图
快速回复 返回顶部 返回列表