• 各种各样的排序(集合篇)-java代码


    第一个,当然是喜闻乐见的冒泡排序。

     1 public class Main {
     2 
     3     public static void main(String[] args) {
     4         // 冒泡排序;
     5         int[] a = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
     6         int t;//每次都找一个最大得数放在最后面,然后在第二个for循环中下一次得遍历次数减去1.
     7         for (int i = 0; i < 8; i++) {
     8             for (int j = 0; j < 8 - i; j++) {
     9                 if (a[j] > a[j + 1]) {
    10                     t = a[j];
    11                     a[j] = a[j + 1];
    12                     a[j + 1] = t;
    13                 }
    14             }
    15         }
    16         for (int p : a) {
    17             System.out.print(p + " ");
    18         }
    19 
    20     }
    21 
    22 }

    其次是快排。

     1 public class Main {
     2 
     3     public static void main(String[] args) {
     4         // 快速排序;
     5         int[] a = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
     6         int i = 0, j = 8;
     7         quickSort(a, i, j);
     8         for (int array : a) {//遍历数组。
     9             System.out.print(array + " ");
    10         }
    11 
    12     }
    13 
    14     public static void quickSort(int a[], int left, int right) {
    15         if (right>= left)
    16             return;
    17         int i=left,j=right;
    18         int t = a[0];
    19         while (i < j) {
    20             while (i < j && a[j] > a[i])
    21                 j--;
    22             a[j] = a[i];
    23             while (i < j && a[i] < a[j])
    24                 i++;
    25             a[i] = a[j];
    26         }
    27         a[i] = t;//t是作为一个中转站来保存数据的。
    28         quickSort(a, i+1, right);//这里相当于递归调用。
    29         quickSort(a, left, i-1);
    30     }
    31 
    32 }

     接下来是选择排序

     1 public class Main {
     2 
     3     public static void main(String[] args) {
     4         // 选择排序;
     5         int[] a = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
     6         int min,t;//选择排序是选择一个位置,把后面适合这个位置的数转移过来。
     7         for(int i=0;i<8;i++) {
     8             min=i;
     9             for(int j=i;j<9;j++) {
    10                 if(a[j]<a[min])min=j;
    11             }
    12             t=a[i];
    13             a[i]=a[min];
    14             a[min]=t;
    15         }
    16         for (int array : a) {//遍历数组。
    17             System.out.print(array + " ");
    18         }
    19 
    20     }
    21 
    22 }

     插入排序~

     1 public class Main {
     2 
     3     public static void main(String[] args) {
     4         // 插入排序;
     5         int[] a = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
     6         for (int i = 1; i < 9; i++) {
     7             int temp = a[i];
     8             int j;
     9             for (j = i; j > 0 &&temp< a[j - 1]; j--) {
    10                 a[j] = a[j - 1];
    11             }
    12             a[j] = temp;
    13         }
    14         for (int array : a) {// 遍历数组。
    15             System.out.print(array + " ");
    16         }
    17 
    18     }
    19 
    20 }

     希尔排序,好吧代码是抄的,我看不懂=.=

     1 public class Main {
     2 
     3     public static void main(String[] args) {
     4         // 希尔排序(不是崩崩崩的那个);
     5         int[] a = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
     6         int temp;
     7         for (int r = a.length / 2; r >= 1; r = r / 2) {
     8             for (int i = r; i < a.length; i++) {
     9                 temp = a[i];
    10                 int j;
    11                 j = i - r;
    12                 while (j >= 0 && temp < a[j]) {
    13                     a[j + r] = a[j];
    14                     j -= r;
    15 
    16                 }
    17                 a[j + r] = temp;
    18             }
    19         }
    20         for (int array : a) {// 遍历数组。
    21             System.out.print(array + " ");
    22         }
    23 
    24     }
    25 
    26 }

     下一个归并排序

     1 public class Main {
     2 
     3     public static void main(String[] args) {
     4         int[] a = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
     5         merge(0, a.length-1, a);
     6         for(int arry:a) {
     7             System.out.print(arry+" ");
     8         }
     9     }// 归并排序;
    10 
    11     public static void merge(int low, int high, int arr[]) {
    12         if (low < high) {
    13             int middle = (low + high) / 2;
    14             merge(low, middle, arr);
    15             merge(middle + 1, high, arr);
    16             int[] temp = new int[high - low+1];
    17             int i = low;
    18             int j = middle+1;
    19             int index = 0;
    20             while (i <= middle && j <= high) {
    21                 if (arr[i] <= arr[j]) {
    22                     temp[index] = arr[i];
    23                     index++;
    24                     i++;
    25 
    26                 } else {
    27                     temp[index] = arr[j];
    28                     index++;
    29                     j++;
    30                 }
    31             }
    32             while (j <= high) {
    33                 temp[index] = arr[j];
    34                 index++;
    35                 j++;
    36             }
    37             while (i <= middle) {
    38                 temp[index] = arr[i];
    39                 index++;
    40                 i++;
    41             }
    42             for (int k = 0; k < temp.length; k++) {
    43                 arr[k + low] = temp[k];
    44             }
    45         }
    46     }
    47 
    48 }
  • 相关阅读:
    深刻理解ajax的success和error的实质和执行过程
    再次遇到 js报错: expected expression, get ')' 或 get ';', '<'等错误?
    怎样让一行中的 文字 input输入框 按钮button它们在同一个高度上? 它们中的文字 是 垂直居中对齐
    怎样阻止input file文件域的change/onchange事件多次重复执行?
    如何在 messager/alert/confirm等消息提示框中 获取 / 设置 嵌入 html内容中的 input[type=checkbox]等的选中状态?
    异步函数造成的问题: 怎样确保异步执行的FileReader onload成功后才执行后面的语句?
    如何退出vim的宏记录模式
    bs模态框中的form获取或设置表单及其中元素用nam不能用id?
    关于git 的理解3
    关于git的理解2
  • 原文地址:https://www.cnblogs.com/Angfe/p/10919861.html
Copyright © 2020-2023  润新知