Java复习——排序

r囧r小猫 2022-02-28 12:14 204阅读 0赞

一、冒泡排序

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N1bnNoaW5lX3JlYnJpdGg_size_16_color_FFFFFF_t_70

  1. package test;
  2. /**
  3. * 测试冒泡排序
  4. * @author 凡心所向,素履以往,生如逆旅,一苇一航
  5. *
  6. */
  7. public class maopao {
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. int a[] = {0,3,5,7,2,1,4,8,9,6};
  11. int temp = 0;
  12. for(int i=0;i<a.length-1;i++) {
  13. for(int j=0;j<a.length-1-i;j++) {
  14. if(a[j]>a[j+1]) {
  15. temp = a[j];
  16. a[j] = a[j+1];
  17. a[j+1] = temp;
  18. }
  19. }
  20. }
  21. //打印结果
  22. for(int i = 0;i<a.length;i++) {
  23. System.out.println(a[i]);
  24. }
  25. }
  26. }

二、选择排序

选择排序:第一次从 R[0] ~ R[n-1] 中选取最小值,与 R[0] 进行交换,第二次从 R[1] ~ R[n-1] 中选择最小值与R[1]交换 ………,总共通过 n-1 次,得到一个安排序码从小到大的有序序码。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N1bnNoaW5lX3JlYnJpdGg_size_16_color_FFFFFF_t_70 1

  1. package test;
  2. public class xuanze {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. int a[] = {0,3,5,7,2,1,4,8,9,6};
  6. int min_index,min;
  7. int temp = 0;
  8. for(int i = 0 ; i < a.length-1 ; i++) {
  9. min_index = i;
  10. min = a[i];
  11. for(int j=i+1;j<a.length;j++) {
  12. if(a[j]<min) {
  13. min = a[j];
  14. min_index = j;
  15. }
  16. }
  17. temp = a[i];
  18. a[i] = a[min_index];
  19. a[min_index] = temp;
  20. }
  21. for(int i=0 ;i<a.length;i++) {
  22. System.out.println(a[i]);
  23. }
  24. }
  25. }

三、插入排序

插入排序分为三种:1、插入排序法;2、谢尔排序法;3、二叉树排序法

插入排序的基本思想:把n个待排序的元素看成是一个有序表和一个无序表,开始有序表只包含一个元素,无序表包含n-1个元素,排序过程中每次从无序表中取出第一个元素,把它的排序码依次与有序码进行比较,把它插入到有序表的适当位置,使之成为新的有序表。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N1bnNoaW5lX3JlYnJpdGg_size_16_color_FFFFFF_t_70 2

  1. package paixu;
  2. /**
  3. * 插入排序
  4. * @author 凡心所向,素履以往,生如逆旅,一苇一航
  5. *
  6. */
  7. public class charu {
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. int a[] = {0,3,1,6,8,2,4,9,7,5};
  11. for(int i=1;i<a.length;i++) {
  12. int insertVal = a[i];
  13. int index = i-1;
  14. while(index>=0&&insertVal<a[index]){
  15. a[index+1] = a[index];
  16. index--;
  17. }
  18. a[index+1] = insertVal;
  19. }
  20. //输出结果
  21. for(int i = 0;i<a.length;i++) {
  22. System.out.println(a[i]);
  23. }
  24. }
  25. }

四、快速排序

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N1bnNoaW5lX3JlYnJpdGg_size_16_color_FFFFFF_t_70 3

  1. package paixu;
  2. /**
  3. * 测试快速排序
  4. * @author 凡心所向,素履以往,生如逆旅,一苇一航
  5. *
  6. */
  7. public class kuaisu {
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. int a[] = {0,3,6,8,1,4,2,5,7,9};
  11. QuickSort qs = new QuickSort();
  12. qs.kuaiSu(0, a.length-1, a);
  13. //输出结果
  14. for(int i=0;i<a.length;i++) {
  15. System.out.println(a[i]);
  16. }
  17. }
  18. }
  19. //快速排序
  20. class QuickSort{
  21. public void kuaiSu(int left,int right,int []a) {
  22. int l = left;
  23. int r = right;
  24. int privot = a[(left+right)/2];
  25. int temp = 0;
  26. while(l<r) {
  27. while(a[l]<privot) l++;
  28. while(a[r]>privot) r--;
  29. if(l>=r) break;
  30. temp = a[l];
  31. a[l] = a[r];
  32. a[r] = temp;
  33. if(a[l]==privot) --r;
  34. if(a[r]==privot) ++l;
  35. }
  36. if(l==r) {
  37. l++;
  38. r--;
  39. }
  40. if(left<r) kuaiSu(left,r,a);
  41. if(right>l) kuaiSu(l,right,a);
  42. }
  43. }

发表评论

表情:
评论列表 (有 0 条评论,204人围观)

还没有评论,来说两句吧...

相关阅读

    相关 复习八大排序

    这篇文章用来复习一下数据结构中常提到的八大排序和三大查找算法 八大排序 八大排序都有哪些? 八大排序按照种类可以分为以下几种: 1. 交换排序:冒泡,快排 2.